Kotlin Coroutines - 启用了ProGuard的ClassNotFoundException

时间:2017-12-31 15:51:38

标签: android proguard kotlinx.coroutines

我有一个用Kotlin编写的已发布应用程序,该应用程序使用Coroutines库,主要与Retrofit2一起用于API请求。我正在使用另一个库Kotlin Coroutines for Retrofit,它将Coroutines和Retrofit2联系在一起 在我的最新版本中,为了减小APK大小,我设置了minifyEnabled true并按照我使用的库的指示添加了ProGuard规则。

问题:
Google Play控制台正在通知我有关以下设备崩溃的信息:BQ Aquaris U Plus,三星Galaxy Grand Neo Plus,RCA 7 Voyager。该应用程序适用于大多数设备。

堆栈跟踪:

java.lang.RuntimeException: 
    at kotlinx.coroutines.experimental.DispatchTask.run (CoroutineDispatcher.kt:127)
    at android.os.Handler.handleCallback (Handler.java:751)
    at android.os.Handler.dispatchMessage (Handler.java:95)
    at android.os.Looper.loop (Looper.java:154)
    at android.app.ActivityThread.main (ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
Caused by: java.util.ServiceConfigurationError: 
    at java.util.ServiceLoader.fail (ServiceLoader.java:225)
    at java.util.ServiceLoader.-wrap1 (ServiceLoader.java)
    at java.util.ServiceLoader$LazyIterator.next (ServiceLoader.java:366)
    at java.util.ServiceLoader$1.next (ServiceLoader.java:448)
    at kotlinx.coroutines.experimental.CoroutineExceptionHandlerKt.handleCoroutineException (CoroutineExceptionHandler.kt:97)
    at kotlinx.coroutines.experimental.StandaloneCoroutine.onCancellation (Builders.kt:176)
    at kotlinx.coroutines.experimental.JobSupport.completeUpdateState$kotlinx_coroutines_core (Job.kt:730)
    at kotlinx.coroutines.experimental.JobSupport.updateState$kotlinx_coroutines_core (Job.kt:671)
    at kotlinx.coroutines.experimental.JobSupport.makeCompleting$kotlinx_coroutines_core (Job.kt:1040)
    at kotlinx.coroutines.experimental.AbstractCoroutine.resumeWithException (AbstractCoroutine.kt:51)
    at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume (CoroutineImpl.kt:53)
    at kotlinx.coroutines.experimental.DispatchTask.run (CoroutineDispatcher.kt:123)
Caused by: java.lang.ClassNotFoundException: 
    at java.lang.Class.classForName (Native Method)
    at java.lang.Class.forName (Class.java:400)
    at java.util.ServiceLoader$LazyIterator.next (ServiceLoader.java:364)
Caused by: java.lang.ClassNotFoundException: 
    at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:312)

我的app/build.gradle文件:

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

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "il.ronmad.speedruntimer"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 26
        versionName '3.1'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
    productFlavors {
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += "src/test/kotlin"
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support:support-v4:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.jaredrummler:colorpicker:1.0.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.20'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.8.2'
    implementation 'com.google.guava:guava:23.6-android'
    testImplementation 'junit:junit:4.12'
    testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    androidTestImplementation 'com.android.support:support-annotations:27.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test:rules:1.0.1'
}
repositories {
    jcenter()
}

我的proguard-rules.pro文件:

-keepattributes SourceFile,LineNumberTable
-renamesourcefileattribute SourceFile

-dontobfuscate
-dontoptimize
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keep class il.ronmad.speedruntimer.** { *; }

# Gson
-dontwarn sun.misc.**
-keep class com.google.gson.examples.android.model.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Coroutines
-dontwarn org.jetbrains.kotlinx.**
-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

# Retrofit
-dontnote retrofit2.Platform
-dontwarn retrofit2.Platform$Java8
-dontwarn okio.**
-dontwarn retrofit2.**
-dontwarn javax.annotation.**

# Guava
-dontwarn com.google.**
-dontwarn javax.inject.**
-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn java.lang.ClassValue
-dontwarn com.google.j2objc.annotations.Weak
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**

-keep class com.google.common.collect.Lists {
    public static ** cartesianProduct(**);
}

我尝试添加以下ProGuard规则,但无济于事:

-keep class org.jetbrains.kotlinx.**
-keep class ru.gildor.coroutines.**

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:2)

请添加以下 Proguard 规则,以使其在发布模式下正常工作。

-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

引用来自Kotline Co-routine

编辑:上面的参考文献无效

现在的正确解决方案如下

-keepnames class kotlinx.** { *; }