Getting an error “cannot find symbol” while trying to compile a Kotlin-Java mixed project with Gradle

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?

1 Like

Failed to reproduce with a dummy project following your build config. Can you please share some sample project to reproduce?

I’m concerned too, and I’m able to reproduce it on Kotlin 1.4.x (worked fine on 1.3.x) : here’s a POC.

https://youtrack.jetbrains.com/issue/KT-42264

The problem is triggered when calling a Kotlin method returning an inline classes from Java:

inline class Baz(val value: Double)

interface Foo {
   fun get(value: Int): Baz
}
// Bar.java
class Bar {

    private final Foo foo;

    private Bar(@NonNull Foo foo) {
        this.foo = foo;
    }

    private double baz(int value) {
        return foo.get(value); // error: cannot find symbol
    }
}

POC.zip (56.8 KB)

1 Like