在 Java 中使用 Kotlin class:找不到符号
Using Kotlin class in Java: Cannot find symbol
我发现 与 Android 类似的问题,但我使用普通 Java 和 Maven 作为构建工具。我认为最好post一个新问题。
我创建了一个 Kotlin class,我试图将其从 Java class 引用为 MyKotlinClass.class
。 Maven 构建失败,而 IntelliJ Idea 中的编译工作正常。我已经将 Kotlin 插件添加到 maven:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
然而这并没有帮助:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project app: Compilation failure
[ERROR] MyClassLinkingKotlin.java:[100,40] error: cannot find symbol
line/column 恰好指代符号 MyKotlinClass.class
。即使这样使用也会失败:
System.err.println(MyKotlinClass.class)
您的 Maven 配置添加了 Kotlin 编译器插件,但没有调整 Java 编译器插件的执行,因此 Java 编译器在 Kotlin 编译器之后运行。因此,Java 编译器在 Kotlin 之前运行,并且看不到 Kotlin 编译的 类.
下面是显示混合语言项目正确配置的片段(摘自 the documentation):
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我 运行 遇到了同样的错误,但是 pom 设置正确。我的问题是我刚刚使用 Intellij 将 Java class 转换为 Kotlin class,这将 Kotlin 文件留在了 src/main/java
.
我的解决方案是创建一个 src/main/kotlin
并将我的 Kotlin class 移到那里,并将我的 Java 文件留在 src/main/java
中。但是您确实需要@yole 的回答显示的 Maven 设置。
如果您正在使用 Android Studio 并且已经通过设置 -> 插件 -> Kotlin 添加了 Kotlin 插件,那么可能是您还没有将 Gradle 的其余部分设置为使用科特林。这是来自 https://medium.com/@elye.project/setup-kotlin-for-android-studio-1bffdf1362e8 的片段:
第 1 步:在 Android Studio 中设置 Kotlin 插件
Android Studio → Preferences… →Plugins → Browse Repository → type
“Kotlin” in search box → install
第 2 步:将 Kotlin 类路径添加到项目 Build.Gradle
buildscript {
ext.kotlin_version = "1.1.1"
ext.supportLibVersion = "25.3.0"
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
第 3 步:添加 Kotlin 库并在您的模块中应用 Kotlin 插件 Build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
// ... various gradle setup
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
如果有人使用 Plain Java 项目 Gradle
而不是 Maven
,请按如下方式更改模块 gradle 文件:
假设您的模块中有以下插件:
apply plugin: 'java-library'
如果你想要 kotlin 插件,那么在 java 插件之前添加 kotlin 插件:
apply plugin: 'kotlin'
apply plugin: 'java-library'
原因是
Your configuration adds the Kotlin compiler plugin, but doesn't adjust
the Java compiler plugin execution so that the Java compiler runs
after the Kotlin compiler. Therefore, the Java compiler runs before
Kotlin, and doesn't see Kotlin-compiled classes.
正如 在答案中所解释的那样
我发现
我创建了一个 Kotlin class,我试图将其从 Java class 引用为 MyKotlinClass.class
。 Maven 构建失败,而 IntelliJ Idea 中的编译工作正常。我已经将 Kotlin 插件添加到 maven:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
然而这并没有帮助:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project app: Compilation failure
[ERROR] MyClassLinkingKotlin.java:[100,40] error: cannot find symbol
line/column 恰好指代符号 MyKotlinClass.class
。即使这样使用也会失败:
System.err.println(MyKotlinClass.class)
您的 Maven 配置添加了 Kotlin 编译器插件,但没有调整 Java 编译器插件的执行,因此 Java 编译器在 Kotlin 编译器之后运行。因此,Java 编译器在 Kotlin 之前运行,并且看不到 Kotlin 编译的 类.
下面是显示混合语言项目正确配置的片段(摘自 the documentation):
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我 运行 遇到了同样的错误,但是 pom 设置正确。我的问题是我刚刚使用 Intellij 将 Java class 转换为 Kotlin class,这将 Kotlin 文件留在了 src/main/java
.
我的解决方案是创建一个 src/main/kotlin
并将我的 Kotlin class 移到那里,并将我的 Java 文件留在 src/main/java
中。但是您确实需要@yole 的回答显示的 Maven 设置。
如果您正在使用 Android Studio 并且已经通过设置 -> 插件 -> Kotlin 添加了 Kotlin 插件,那么可能是您还没有将 Gradle 的其余部分设置为使用科特林。这是来自 https://medium.com/@elye.project/setup-kotlin-for-android-studio-1bffdf1362e8 的片段:
第 1 步:在 Android Studio 中设置 Kotlin 插件
Android Studio → Preferences… →Plugins → Browse Repository → type “Kotlin” in search box → install
第 2 步:将 Kotlin 类路径添加到项目 Build.Gradle
buildscript {
ext.kotlin_version = "1.1.1"
ext.supportLibVersion = "25.3.0"
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
第 3 步:添加 Kotlin 库并在您的模块中应用 Kotlin 插件 Build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
// ... various gradle setup
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
如果有人使用 Plain Java 项目 Gradle
而不是 Maven
,请按如下方式更改模块 gradle 文件:
假设您的模块中有以下插件:
apply plugin: 'java-library'
如果你想要 kotlin 插件,那么在 java 插件之前添加 kotlin 插件:
apply plugin: 'kotlin'
apply plugin: 'java-library'
原因是
Your configuration adds the Kotlin compiler plugin, but doesn't adjust the Java compiler plugin execution so that the Java compiler runs after the Kotlin compiler. Therefore, the Java compiler runs before Kotlin, and doesn't see Kotlin-compiled classes.
正如