引用Kotlin类时的ClassNotFoundException

我试着开始将基于Java的multidexed项目迁移到Kotlin,但是每当应用程序尝试引用Kotlin类时,我都会得到ClassNotFoundException。 奇怪的是,有时相同的代码有时不工作。 有计算机产生工作apk和其他人没有。 有时候重建就足以解决问题。

我试图谷歌它,但我只找到这个线程,但没有解决方案。 这个东西是否适用于其他人或者没有任何multidexed Kotlin项目呢?

我已经尝试了AS 2.3.3和AS 3.0.0-beta6,2.3.3 android插件,kotoin版本1.1.50, 没有proguard

buildscript { ext.kotlin_version = '1.1.50' ext.android_plugin_version = '2.3.3' ext.support_lib_version = '25.3.1' ext.play_services_version = '11.0.4' repositories { jcenter() maven { url "https://maven.google.com" } } dependencies { classpath "com.android.tools.build:gradle:$android_plugin_version" classpath 'me.tatarka:gradle-retrolambda:3.2.5' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:3.0.0' classpath 'com.google.firebase:firebase-plugins:1.1.1' } } allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } } apply plugin: "com.android.application" apply plugin: "me.tatarka.retrolambda" apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: "com.google.firebase.firebase-perf" repositories { mavenCentral() jcenter() maven { url "https://jitpack.io" } } android { signingConfigs { debug { ... } } compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.my.app" signingConfig signingConfigs.debug minSdkVersion 14 targetSdkVersion 25 versionCode 13 versionName "2.0.1.b8face5" vectorDrawables.useSupportLibrary = true multiDexEnabled true testInstrumentationRunner "com.my.app.TestRunner" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } //this is because instabug uses rxjava 1 and now we have both 1 and 2 on the classpath packagingOptions { exclude "META-INF/rxjava.properties" } buildTypes { debug { applicationIdSuffix ".debug" minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } release { } } } dependencies { compile fileTree(include: "*.jar", dir: "libs") // Force usage of support annotations in the test app, since it is internally used by the runner module. androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.1") { exclude module: "support-annotations" } androidTestCompile("com.android.support.test.espresso:espresso-contrib:2.2.1") { // Necessary to avoid version conflicts exclude group: "com.android.support", module: "appcompat" exclude group: "com.android.support", module: "support-v4" exclude group: "com.android.support", module: "support-annotations" exclude module: "recyclerview-v7" } compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile "com.android.support:appcompat-v7:$support_lib_version" compile "com.android.support:recyclerview-v7:$support_lib_version" compile "com.android.support:design:$support_lib_version" compile "com.android.support:cardview-v7:$support_lib_version" compile "com.google.android.gms:play-services-auth:$play_services_version" compile "com.google.android.gms:play-services-identity:$play_services_version" compile "com.google.android.gms:play-services-location:$play_services_version" compile "com.google.android.gms:play-services-maps:$play_services_version" compile "com.google.firebase:firebase-core:$play_services_version" compile "com.google.firebase:firebase-crash:$play_services_version" compile "com.google.firebase:firebase-perf:$play_services_version" compile "com.google.code.gson:gson:2.8.1" compile "com.google.maps.android:android-maps-utils:0.4" compile "com.squareup.retrofit2:retrofit:2.3.0" compile "com.squareup.retrofit2:converter-gson:2.3.0" compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0" compile "io.reactivex.rxjava2:rxandroid:2.0.1" compile "io.reactivex.rxjava2:rxjava:2.1.3" compile "com.squareup.okhttp3:okhttp-urlconnection:3.8.1" compile "com.facebook.stetho:stetho:1.5.0" compile "com.facebook.stetho:stetho-okhttp3:1.5.0" compile "com.squareup.picasso:picasso:2.5.2" compile "com.jakewharton:butterknife:8.2.1" compile "com.annimon:stream:1.1.8" compile "com.github.lawloretienne:quickreturn:0.0.1" compile "com.github.chrisbanes:PhotoView:1.2.6" compile "fr.baloomba:viewpagerindicator:2.4.2" compile "com.github.bluejamesbond:textjustify-android:2.1.6" compile "com.turingtechnologies.materialscrollbar:lib:10.1.4" compile "com.github.PhilJay:MPAndroidChart:v3.0.1" compile "com.android.support:multidex:1.0.1" compile "com.bugsnag:bugsnag-android:3.9.0" //TODO check periodically whether they upgraded to rxjava 2 compile "com.instabug.library:instabug:4.2.11" testCompile "junit:junit:4.12" androidTestCompile "com.android.support:support-annotations:$support_lib_version" androidTestCompile "com.android.support.test:runner:0.5" androidTestCompile "com.android.support.test:rules:0.5" annotationProcessor "com.jakewharton:butterknife-compiler:8.2.1" compile "com.google.dagger:dagger:2.5" annotationProcessor "com.google.dagger:dagger-compiler:2.5" provided "javax.annotation:jsr250-api:1.0" } apply plugin: "com.google.gms.google-services" 

如果您使用的是Kotlin Java 8,那么您也应该使用Kotlin jre8。

  compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" 

最后我能解决这个问题! 这个异常是由RetroLambda错误引起的,在我更新到3.7.0之后,我感激地消失了。 构建是非确定性的,因为它看起来不会干净一切。 在手动删除应用程序和根模块中的构建文件夹后,结果变得确定。