Reputation: 2678
I added a Kotlin file that contains three classes to an Android Studio project originally written in Java. The file compiles fine within Android Studio, and the project builds until I try to get a Java class to interact with the Kotlin class. Android Studio gives me the following error:
Java compiler:
[path]/[project]
app/src/main/java
[project].jav
error: cannot find symbol class Quiz_abTime
When I added the file, Android Studio complained about gradle configuration, but I ran the configuration and it says that all modules that use Kotlin are configured.
Note that I have already tried invalidating the cache and restarting Android Studio, so that isn't the problem.
Here are my build.gradle
files. The module first:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.61'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Now the app's build.gradle
:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 26
defaultConfig {
applicationId [application name deleted for SO]
minSdkVersion 16
targetSdkVersion 26
versionCode 6
versionName "2.0.5"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-vector-drawable:26.1.0'
implementation 'com.android.support:appcompat-v7:26.1.0' // where X.X.X version
implementation 'com.github.yukuku:ambilwarna:2.0.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
mavenCentral()
}
Here's another issue that I include only because it may be related. In my main application, I have the following:
import android.support.v7.app.ActionBar;
Curiously, ActionBar
is highlighted in red, and it says it Cannot resolve symbol
ActionBar`. There are several others highligted like this; this started after Android Studio asked for an upgrade. However, at least this builds; building fails only when I try to access the Kotlin class from a Java class.
I've done Kotlin with Java before in Android Studio and this wasn't a problem, but it Can anyone tell me where to look for issues that are preventing the Java compiler from seeing the Kotlin class?
Upvotes: 0
Views: 2343
Reputation: 8067
In my case, I forgot to add a dependency to my Java project. The compiler was complaining about Kotlin files.
implementation 'androidx.core:core-ktx:1.9.0'
Upvotes: 1
Reputation: 2678
In this case it turns out that Android Studio didn't prepend the Kotlin file with an important line:
package [packagename]
The moment I added that, it compiled and ran beautifully. I thought this was something AS would do automatically, but apparently not -- or at least not in this case, for whatever reason. The issues with other symbols it can't find are apparently unrelated.
Upvotes: 0