我正在尝试在我的 android 项目中使用反射,但我得到了
kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection
implementation is not found at runtime. Make sure you have kotlin-
reflect.jar in the classpath
奇怪的是,它仅在我通过 gradle 导入 kotlin-reflect 时才起作用。如果我手动将 .jar 添加到 libs 目录,一切正常。
jar 来自完全相同的 Maven 页面,我从那里获取编译行
项目级别:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
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
}
}
allprojects {
repositories {
mavenCentral()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块级别:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//I tried many variants of adding the library - the commented lines
implementation fileTree(dir: 'libs', include: ['*.jar'])
//testImplementation "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11'
testImplementation "org.jetbrains.kotlin:kotlin-test:1.3.11"
//compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.11'
//compile "org.jetbrains.kotlin:kotlin-reflect"
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.13-beta-1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
我愿意运行的简单测试
class Reflections {
class A(val property: Int)
@Test
fun javaGetter() {
println(
A::property.getter.returnType
)
}
}
我已经检查了 Stack 上的其他问题,但没有答案。
我做错了什么,我错过了什么?
最佳答案
尝试将其插入顶级 gradle 文件,例如
dependencies {
classpath "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
关于android - 通过gradle导入时运行时找不到Kotlin反射实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53781355/