kotlin.jvm.KotlinReflectionNotSupportedError:在运行时找不到Kotlin反射实现。确保您有kotlin-reflect.jar


82

当我编译时出现上述错误。我的gradle文件如下:-

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0 rc2"

    defaultConfig {
        applicationId "package.name"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 6
        versionName "2.0"
    }

    buildTypes {
        debug {
            minifyEnabled false
            signingConfig signingConfigs.debug
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:$support_version"
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.anko:anko-sdk15:$anko_version"
    compile "org.jetbrains.anko:anko-support-v4:$anko_version"
    compile "com.android.support:recyclerview-v7:$support_version"
}

buildscript {
    ext.kotlin_version = '1.0.1-2'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}

我的项目如下

buildscript {
    ext.support_version = '23.2.1'
    ext.kotlin_version = '1.0.1'
    ext.anko_version = '0.8.3'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0-alpha3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我想念任何东西吗?

Answers:


155

根据错误,我假设:

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

在您dependencies的标准库之外。


2
谢谢!compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"kotlinlang.org/docs/reference/using-gradle.html中
Elye

也许不太明显:(1)必须是双引号,而不是单引号,否则$ kotlin_version不会扩展,(2)根据kotlinlang.org/docs/reference/…,如果> = 1.1,则可以省略:$ kotlin_version .2。但这对我来说似乎是行不通的(我的$ kotlin_version当前为1.2.10);错误消息表明它构造了伪造的url,例如https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-reflect//kotlin-reflect-.jar。因此,我仍在此答案中使用食谱。
唐·哈奇

4
值得一提的implementation是,现在是以下方面的首选指令compileimplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
kip2

有没有针对此依赖的保护规则?
三一

1

对我来说,问题不是具有相同版本的kotlin,我是我的模块和lib,所以我不得不强迫它们使用相同的版本

configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin') {
                details.useVersion versions.kotlin
            }
        }
    }}

1

我通过在build.gradle中添加它来解决此问题

implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.