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

时间:2018-12-14 14:11:43

标签: 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上的其他问题,但是运气不好。

我在做错什么,我在想什么?

1 个答案:

答案 0 :(得分:0)

尝试将其插入顶级gradle文件,例如

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}