App crashed when R8 enabled with existing proguard settings

Viewed 2517

I got a RuntimeException on the release build when R8 enabled together with the existing proguard configuration. Checked "Failure to verify dex file Out-of-order annotation_element name_idx" with no luck, I haven't set -overloadaggressively anywhere. Also tried with ProGuard disabled according to "Android/java: Transition / Migration from ProGuard to R8?", still it doesn't work.

The build environment as following:

Android Studio : 3.4.1
com.android.tools.build:gradle:3.4.1

android.enableR8=true
android.enableD8=true
android.enableDesugar=true

The crash log:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.your.pkg, PID: 25969
java.lang.RuntimeException: Unable to instantiate application com.your.pkg.YourApplication: java.lang.ClassNotFoundException: Didn't find class "com.your.pkg.YourApplication" on path: DexPathList[[zip file "/data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk"],nativeLibraryDirectories=[/data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/lib/arm64, /data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:1069)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5889)
    at android.app.ActivityThread.access$1100(ActivityThread.java:202)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1665)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:198)
    at android.app.ActivityThread.main(ActivityThread.java:6729)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.your.pkg.YourApplication" on path: DexPathList[[zip file "/data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk"],nativeLibraryDirectories=[/data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/lib/arm64, /data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50)
    at android.app.Instrumentation.newApplication(Instrumentation.java:1121)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:1061)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5889) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:202) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1665) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:198) 
    at android.app.ActivityThread.main(ActivityThread.java:6729) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
    Suppressed: java.io.IOException: Failed to open dex files from /data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk because: Failure to verify dex file '/data/app/com.your.pkg-GEdf7lpr_eG0NIkHShudhA==/base.apk': Out-of-order entry types: 21a8 then 21a8
    at dalvik.system.DexFile.openDexFileNative(Native Method)
    at dalvik.system.DexFile.openDexFile(DexFile.java:354)
    at dalvik.system.DexFile.<init>(DexFile.java:101)
    at dalvik.system.DexFile.<init>(DexFile.java:75)
    at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
    at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
    at dalvik.system.DexPathList.<init>(DexPathList.java:164)
    at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
    at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
    at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
    at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
    at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
    at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
    at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
    at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727)
    at android.app.LoadedApk.getClassLoader(LoadedApk.java:810)
    at android.app.LoadedApk.getResources(LoadedApk.java:1032)
    at android.app.ContextImpl.createAppContext(ContextImpl.java:2357)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5792)

Thanks in advance!

2 Answers

I struggled with this for some time where Proguard was previously working fine but when R8 was introduced as the default code shrinker, it stripped needed resources from my app. I finally reverted to using old school Proguard with the following lines:

In in gradle.properties add:
android.enableR8 = false

In build.gradle add:
useProguard = true

It is important to modify both files, otherwise R8 will automatically be enabled in Android Studio 3.4+

UPDATE: 10/6/2019: Android 3.5 does not respect the 2 directives above and uses R8 to shrink code if minify is set to true in Gradle. That is a shame because R8 strips drawable resources from an app that are not in the drawable folder. If your app uses a resource proxy or stores drawables outside of the Res directory, R8 breaks the app. I assume that the issue would apply to other resources besides drawables, though I have not tested how other resources might be affected.

Related