java.lang.NoClassDefFoundError: Failed resolution of: App class on Lenovo K4 Note running android 6, API 23

java.lang.NoClassDefFoundError: Failed resolution of: App class on Lenovo K4 Note running android 6, API 23

GroupingHelper 是一个 class 在应用程序本身的帮助程序包中定义的,并非来自第三方库。尽管如此,在运行时仍未找到 GroupingHelper,我遇到了以下崩溃:

    --------- beginning of crash
05-08 18:18:33.181 24111-24111/com.pkg.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pkg.app, PID: 24111
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/pkg/app/helpers/GroupingHelper$$Lambda[=10=];
        at com.pkg.app.helpers.GroupingHelper.<init>(GroupingHelper.java:21)
        at com.pkg.app.activities.TransactionActivity.<init>(TransactionActivity.java:35)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pkg.app.helpers.GroupingHelper$$Lambda[=10=]" on path: DexPathList[[zip file "/data/app/com.pkg.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.pkg.app-1/lib/arm64, /data/app/com.pkg.app-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.pkg.app.helpers.GroupingHelper.<init>(GroupingHelper.java:21) 
        at com.pkg.app.activities.TransactionActivity.<init>(TransactionActivity.java:35) 
        at java.lang.Class.newInstance(Native Method) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1072) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5776) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
        Suppressed: java.lang.NoClassDefFoundError: com.pkg.app.helpers.GroupingHelper$$Lambda[=10=]
        at dalvik.system.DexFile.defineClassNative(Native Method)
        at dalvik.system.DexFile.defineClass(DexFile.java:226)
        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
        at dalvik.system.DexPathList.findClass(DexPathList.java:338)
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
                ... 16 more
        Suppressed: java.lang.ClassNotFoundException: com.pkg.app.helpers.GroupingHelper$$Lambda[=10=]
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 15 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

到目前为止,仅在 Lenovo K4 Note 运行 Android 6.0(API 级别 23)上观察到崩溃,并且在测试的其他 android 设备上运行良好。我将 multidexEnabled 设置为 true。以下是我的应用 build.gradle 的片段:

compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
    applicationId "com.pkg.app"
    minSdkVersion 21
    targetSdkVersion 27
    multiDexEnabled true

    versionCode 7
    versionName "1.6"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
compileOptions {
    incremental true
    targetCompatibility 1.8
    sourceCompatibility 1.8
}

dexOptions {
    javaMaxHeapSize '4g'
}

问题是 com.pkg.app.helpers.GroupingHelper$$Lambda$0 未找到并且不是 GroupingHelper class 本身。我错误地解释了崩溃。

问题出在 Google 的 Guava 库版本上。我开始相信 'com.google.guava:guava:22.0-android' 是 Android <= API 23 级的正确库。对于 API 24+ 级,'com.google.guava:guava:22.0' 工作正常。

所以,建议我们使用'com.google.guava:guava:22.0-android'虽然gradle一直在抄袭说'com.google.guava:guava:22.0'是较新的版本。