gpt4 book ai didi

gradle - Kotlin 总是告诉我 KotlinReflectionNotSupportedError

转载 作者:行者123 更新时间:2023-12-03 05:39:29 24 4
gpt4 key购买 nike

我刚刚开始从 Java 转换为 Kotlin。我有一个问题:当我反射(reflect)以下内容时:XXClass::class.java
或其他任何东西。他们不要在 JAR 中工作 ,但在 IDE 中工作正常。

我正在使用 gradle :module:buildgradle :module:jar生成 JAR 文件。生成后总是告诉我KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath .但是我已经将它们添加到依赖项中,并且它们在 IDE 中运行良好。

这些是我的 gradle 文件的一部分(请注意,这是一个 kotlin 应用程序模块,而不是 Android 模块):

// (Module level)
apply plugin: 'kotlin'
sourceCompatibility = 1.8
dependencies {
// Others...
// I've already added reflect and stdlib
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

jar {
manifest {
// ...
}

// To include dependencies to build a fat jar
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}

// Include reflect and stdlib
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
javaParameters = true
noReflect = false
noStdlib = false
}
}

// (Project level)
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
// ...
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

谁能帮我一把?非常感谢。

最佳答案

问题出在这个脚本 block 中

        configurations.compile.collect { 
it.isDirectory() ? it : zipTree(it)
}

您已经使用 包含了依赖项 ( kotlin-reflect )实现 配置,但从 收集依赖项编译配置。如果您将配置从实现更改为编译,则此 block 将起作用。自 编译已弃用,您可以尝试这样的方法来收集您对 jar 的依赖项
        compileJava.classpath.collect {
it.isDirectory() ? it : zipTree(it)
}

看到这个 answer

关于gradle - Kotlin 总是告诉我 KotlinReflectionNotSupportedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54043705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com