引用Kotlin类时,ClassNotFoundException



我尝试开始迁移基于Java的多智能项目到Kotlin,但是只要应用程序试图引用Kotlin类,我就会获得ClassNotFoundException。奇怪的是,相同的代码有时有时可以起作用。有些计算机会产生工作APK,而其他计算机则没有。有时,重建足以解决问题。

我已经尝试使用Google Google,但是我只找到了此线程,但是没有解决方案。这件事是否适合其他所有人,或者还没有任何多重kotlin项目吗?

我已经尝试使用AS 2.3.3和AS 3.0.0-Beta6,2.3.3 Android Plugun,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"

最后,我能够解决这个问题!例外是由Retrolambda错误引起的,值得庆幸的是,在我更新为3.7.0之后,它消失了。该构建是非确定的,因为Gradle Clean似乎并没有删除所有内容。在我手动删除应用程序和根模块中的构建文件夹后,结果变得确定性。

如果您使用的是Java 8,则还应使用Kotlin JRE8。

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

最新更新