Java-Kotlin混合Gradle项目编译报‘cannot find symbol’错误求助
Let's work through this issue together—your Java classes can't find the Kotlin TaskType enum because Gradle isn't compiling your Kotlin code before the Java code, or there's a misconfiguration in how the Kotlin plugin is set up. Here's how to fix it step by step:
1. Fix Plugin Version References & Application
First, your buildscript block uses $kotlinVersion and $shadowVersion before loading the gradle.properties file, which means those variables are undefined at that point. This prevents the Kotlin and Shadow plugins from being correctly resolved. Let's fix that:
Update the buildscript section to load properties first:
buildscript { repositories { mavenCentral() } // Load properties before referencing version variables def projectProps = new Properties() file("../../gradle.properties").withInputStream { projectProps.load(it) } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${projectProps.getProperty('kotlinVersion')}" // Note: You're missing `shadowVersion` in your gradle.properties—add it there (e.g., shadowVersion=7.1.2) classpath "com.github.jengelman.gradle.plugins:shadow:${projectProps.getProperty('shadowVersion')}" } }
Then simplify your plugin application—no need for conditional checks, just explicitly apply the plugins (the Kotlin JVM plugin plays nicely with the Java plugin automatically):
plugins { id 'java' id 'org.jetbrains.kotlin.jvm' id 'com.github.johnrengelman.shadow' }
2. Ensure Java Compilation Depends on Kotlin Compilation
By default, the Kotlin plugin should set up a dependency where compileJava runs after compileKotlin, but sometimes custom configurations break this. Let's enforce it explicitly:
tasks.compileJava.dependsOn tasks.compileKotlin
This guarantees that your Kotlin classes are compiled to bytecode before the Java compiler tries to reference them.
3. Simplify Source Sets (Optional but Recommended)
The Kotlin JVM plugin already defaults to using src/main/kotlin for Kotlin files and src/main/java for Java files. Your custom sourceSets configuration is redundant and could cause conflicts—feel free to remove it:
// Remove this block entirely // sourceSets { // main.kotlin.srcDirs += 'src/main/kotlin' // main.java.srcDirs += 'src/main/java' // }
4. Verify Kotlin Standard Library Dependency
Your dependencies block already includes kotlin-stdlib-jdk8 with the correct version (matching your Kotlin plugin version from gradle.properties), so that's good—no changes needed here.
5. Clean & Rebuild
After making these changes, wipe old build artifacts and rebuild from scratch to ensure no cached issues:
./gradlew clean build
Quick Additional Checks
- Double-check package names: Make sure your
TaskTypeenum is in the same package as your Java class, or that you've added the correctimportstatement for it in your Java code. - Check for typos: Ensure the enum name and value (
TaskType.REVERSE) are spelled exactly the same in both Kotlin and Java (case-sensitive!).
Following these steps should resolve the "cannot find symbol" error and let your Java classes properly reference your Kotlin code.
内容的提问来源于stack exchange,提问作者Daybreak

