I’m working on a Java-Kotlin mixed project (not using android). When I try to compile project, I get “cannot find symbol” error wherever I refer to Kotlin class from Java class. I followed instructions in the Kotlin documention and also tried solutions in other questions, but it doesn’t work.
I’ve put all Java files in src/main/java, and all Kotlin files in src/main/kotlin.
Below is build.gradle
I’m using.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "com.github.jengelman.gradle.plugins:shadow:$shadowVersion"
}
}
plugins {
id 'java'
}
if (!project.getPlugins().hasPlugin('org.jetbrains.kotlin.jvm')) {
apply plugin: 'org.jetbrains.kotlin.jvm'
}
if (!project.getPlugins().hasPlugin('com.github.johnrengelman.shadow')) {
apply plugin: 'com.github.johnrengelman.shadow'
}
def properties = new Properties()
new File(project.projectDir, '../../gradle.properties').withInputStream() { properties.load(it) }
group = properties.getProperty('group')
version = properties.getProperty('version')
sourceCompatibility = properties.getProperty('sourceCompatibility')
compileJava.options.encoding = 'UTF-8'
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/java'
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs = ['-Xjvm-default=compatibility']
jvmTarget = '1.8'
}
}
jar {
finalizedBy shadowJar
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile project(':Compatibility')
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:' + properties.getProperty('kotlinVersion')
compile 'com.google.guava:guava:' + properties.getProperty('guavaVersion')
compile 'com.google.code.gson:gson:' + properties.getProperty('gsonVersion')
compileOnly 'org.jetbrains:annotations:' + properties.getProperty('annotationsVersion')
}
gradle.properties
:
group=foo.bar
version=1.0.0
sourceCompatibility=1.8
kotlinVersion=1.4.10
guavaVersion=29.0-jre
gsonVersion=2.8.6
annotationsVersion=20.0.0
How can I solve this problem?