You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java-Kotlin混合Gradle项目编译报‘cannot find symbol’错误求助

Fixing "cannot find symbol" When Referencing Kotlin Classes from Java in a Gradle Project

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.

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 TaskType enum is in the same package as your Java class, or that you've added the correct import statement 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:07:54