首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过gradle导入时,在运行时找不到Kotlin反射实现

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

Stack Overflow用户
提问于 2018-12-14 22:11:43
回答 1查看 2.3K关注 0票数 1

我试着在我的android项目中使用反射,但我得到了

代码语言:javascript
复制
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页面

项目级别:

代码语言:javascript
复制
// 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
}

模块级:

代码语言:javascript
复制
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'
}

我愿意运行的简单测试

代码语言:javascript
复制
class Reflections {

    class A(val property: Int)

    @Test
    fun javaGetter() {
        println(
            A::property.getter.returnType
        )
    }

}

我已经在Stack上检查了其他问题,但没有找到答案。

我做错了什么,我错过了什么?

EN

回答 1

Stack Overflow用户

发布于 2020-02-08 20:43:07

尝试在顶层gradle文件中插入它,例如

代码语言:javascript
复制
dependencies {
    classpath "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53781355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档