java.lang.VerifyError: Rejecting class that attempts to sub-class erroneous class

7.3k views Asked by At

While applying proguard to application m getting following verify error, I have seen other variants of verify errors but the following seems bit different, Proguard version: 5.3.3, Following MainApplication class extends android.app.Application and crash occurs at super.onCreate(); of onCreate() method

and in my proguard m having

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgent

,also I have mentioned

-dontshrink -dontoptimize

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapplication, PID: 18598
    java.lang.VerifyError: Rejecting class com.myapplication.MainApplication that attempts to sub-type erroneous class ak (declaration of 'com.myapplication.MainApplication' appears in /data/app/com.myapplication-v4oPXfQv5kNLX1oUA9GwUw==/base.apk)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1102)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1087)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:983)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5715)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.VerifyError: Rejecting class ak that attempts to sub-type erroneous class en (declaration of 'ak' appears in /data/app/com.myapplication-v4oPXfQv5kNLX1oUA9GwUw==/base.apk)
        at java.lang.Class.newInstance(Native Method) 
        at android.app.Instrumentation.newApplication(Instrumentation.java:1102) 
        at android.app.Instrumentation.newApplication(Instrumentation.java:1087) 
        at android.app.LoadedApk.makeApplication(LoadedApk.java:983) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5715) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
     Caused by: java.lang.VerifyError: Verifier rejected class en: void en.<init>(android.content.Context) failed to verify: void en.<init>(android.content.Context): [0xA] thrown class Precise Reference: bjp not instanceof ThrowableVerifier rejected class en: void en.attachBaseContext(android.content.Context) failed to verify: void en.attachBaseContext(android.content.Context): [0x7] thrown class Precise Reference: bjp not instanceof Throwable

With analyze apk I can see following is not obfuscated without proguard

.class public Lcom/myapplication/MainApplication;
.super Landroid/app/Application;
.source "MainApplication.java"

but same was obfuscated once I apply proguard,

.class public Lcom/myapplication/MainApplication;
.super Lak;
.source "SourceFile"

correct me if m wrong, with following

-keep public class * extends android.app.Application

it must not obfuscate application classes, why does above happen..?

3

There are 3 answers

0
A.Mamode On

I had the exactly same error after resolving a lot of conflicts and rebases. I just cleaned the project and rebuilt it. It was working again like a charm.

2
Martin Zeitler On

When looking at the ContextWrapper ...it is being stated, that:

Known direct subclasses

Application, BackupAgent, ContextThemeWrapper, IsolatedContext, MutableContextWrapper, RenamingDelegatingContext, Service

... of which not all are declared in your ProGuard -keep configuration.

you would need to add a rule alike the one below, which is a different approach, than adding all the individual sub-classes explicitly by their name... the public keyword is rather optional there, because those private/protected methods do not really need to be obfuscated, simply because the SDK sources package is publicly available for download. adding the -verbose switch into the configuration always makes ProGuard complain about "what to do", always with a hyperlink to the user manual.

-keep public class * extends android.content.ContextWrapper {public *;}

uploading mapping.txt to Crashlytics helps, when dealing with crashes in obfuscated builds.

0
Andrew G On

In my case, my application didn't have a transitive dependency that was present in a library. After adding this dependency to the Gradle build script, everything is working fine.