android - 通过gradle导入时运行时找不到Kotlin反射实现

标签 android maven gradle kotlin

我正在尝试在我的 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/

相关文章:

android - 没有Gradle或Maven的情况下如何使用Android Studio 1.0?

Java共享(交叉)依赖管理

java - 使用 Spock 框架运行 Android 单元测试

android - cordova-plugin-ionic-webview - 自定义方案不适用于 Android

java - 将 dialogflow 依赖项添加到我的 android 应用程序后出现重复类错误

java - Maven在尝试连接到Cloudera URL以获取CDH Jar位置时显示错误

java - [错误] : cannot find symbol variable log when building with maven and lombok

java - 生成 “build”文件夹下的文件,不应对其进行编辑

android - 为什么我无法获取添加的单选按钮或编辑文本的值?

java - 在 Android 中复制并粘贴文件