3

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!

rayworks
  • 741
  • 9
  • 15
  • 3
    Could you take a look at https://issuetracker.google.com/129241209#comment11, to see if you have some similar Kotlin code with annotations. If not, this could be the same as https://issuetracker.google.com/124546328, for which we have been looking for reproductions for a while. We have seen a few reports of the indexes in the dex file beeing incorrect. If possible could you share the failing apk with sgjesse@google.com, so I can check the exact section where the ids are wrong, and then hopefully we can work together on locating the bug. – sgjesse Jun 06 '19 at 09:45
  • @sgjesse Thanks for the reply. Yes, my project is mixing Java and Kotlin. After checking the ticket 129241209, I found in the Kotlin code, there are only a few simple annotation classes (e.g. JvmOverloads) in use but no duplicate annotation defined. So I will send you the failing apk. – rayworks Jun 06 '19 at 15:04
  • @sgjesse Any progress for this bug? – rayworks Jun 17 '19 at 15:17
  • Sorry for taking so long to respond. Hope you are still interested in helping track down this issue. I have responded to your previous email. – sgjesse Jul 01 '19 at 14:17
  • https://stackoverflow.com/a/57156980/7254873 – Sumit Shukla Jul 27 '19 at 04:35

2 Answers2

1

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.

user1608385
  • 609
  • 8
  • 17
  • Thanks for the answer! 'android.enableR8 = false', that's exactly the previous settings of my project. However I want to take advantage of R8 which is supposed to be compatible with Proguard. – rayworks Jun 22 '19 at 14:43
  • No need to add `useProguard = true` to build.gradle, only `android.enableR8 = false` suffice – Mahdi Javaheri Oct 05 '19 at 10:19
1

This is an issue in both R8 and D8, which is tracked in https://issuetracker.google.com/136549275.

I will update this answer when the issue has been resolved.

sgjesse
  • 3,793
  • 14
  • 17