Application class not found in Android after using ProGuard - android

My application worked well, without any errors, so far. But lately I've began using ProGuard to obfuscate the code now I see the following error report in the Google Play developer console:
java.lang.RuntimeException: Unable to instantiate application MYPACKAGE.MyApp: java.lang.ClassNotFoundException: MYPACKAGE.MyApp
at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3950)
at android.app.ActivityThread.access$1300(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: MYPACKAGE.MyApp
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Instrumentation.newApplication(Instrumentation.java:945)
at android.app.LoadedApk.makeApplication(LoadedApk.java:477)
MyApp is the custom application class, obviously. There's only one error report on that issue so far, although there are 10,000+ active installations. Does that mean I shouldn't bother with that problem or could it be a serious issue? How can I resolve this?

try add this lines:
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
to proguard.cfg file

ProGuard simply replaces class names by obfuscated class names. If the processed code works on one device (or 10,000+ devices), it should work on all devices.
Moreover, if you disassemble the processed application, you'll see that mypackage.MyApp is still there, with its original name. The default integration of ProGuard in the Android build takes care of keeping activities, services, etc, without any need for additional -keep options.
In short, this is some sort of fluke, most probably unrelated to obfuscation. Simply reinstalling the application may help.

Seems like your class names are missing in proguard's obfuscation configuration. Add the following to your proguard.cfg with the missing class names.
-keep public class your_missing_class_full_name

Related

What are the recommended proguard settings for android support library?

I am using android-support-library-v7 in my project and of course I would like to use progurad to minimize and obfuscate my code.
The problem is that if I use proguard I get errors similar to this:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.project/org.example.project.ActivityMain}: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.h.a(Unknown Source)
at android.support.v7.preference.h.b(Unknown Source)
at org.exampple.project.ActivityMain.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
... 11 more Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getConstructor(Class.java:431)
... 22 more
PreferenceCategory is used in my preferences.xml file and I guess it is accessed via reflection. Proguard if probably removes this method (or class) if it is not referenced in code when shrinking. And even if not it surely obfuscates the name.
Google in itßs usual fashion provides absolutely no documentation on the subject.
So, what are the optimum settings to achieve good shrinking and good obfuscation.
There are some similar questions on Stack Overflow, but they boil down to:
-keep class android.support.v7.** { *; }
which cleary defeats the purpose of shrinking and obfuscating.
Trying to randomly guess the settings or by try and error method is extremely time consuming.
The Support Library uses the consumerProguardFiles feature to automatically include the appropriate ProGuard if you're using Gradle, meaning you don't need to manually include anything.
Looking at the ProGuard file for preferences-v7 (stored in the proguard.txt file within the AAR), it contains the following lines:
# Preference objects are inflated via reflection
-keep public class android.support.v7.preference.Preference {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keep public class * extends android.support.v7.preference.Preference {
public <init>(android.content.Context, android.util.AttributeSet);
}
Which covers the exact method that it says you are missing (as PreferenceCategory indirectly extends Preference). Check to make sure you are using the full Gradle dependency.
Check out this project on Github that has proguard rules for popular libraries.

Rare Android Unable to Instantiate Application Exception

I'm seeing the following exception under Crashes in the dev console from time to time.
Whats puzzling about it is that google maps shows up in the call stack as if its calling or creating my app class - very confusing. Whats further weird is the "MyApp-2.apk" in the call stack - where it appends the "-2".
I do include the android.maps.jar file in the libs.
I would like to know how to recreate this if possible and how to prevent this from happening?
java.lang.RuntimeException: Unable to instantiate application com.MyApp.MyCustomApp:
java.lang.ClassNotFoundException: Didn't find class "com.MyApp.MyCustomApp"
on path: /system/framework/com.google.android.maps.jar:/data/app/com.MyApp-2.apk
at android.app.LoadedApk.makeApplication(LoadedApk.java:503)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4405)
at android.app.ActivityThread.access$1400(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.MyApp.MyCustomApp" on path: /system/framework/com.google.android.maps.jar:/data/app/com.MyApp-2.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Instrumentation.newApplication(Instrumentation.java:967)
at android.app.LoadedApk.makeApplication(LoadedApk.java:498)
... 11 more
Manifest Application Tag:
<application
android:name=".MyApp"
android:hardwareAccelerated="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme" >
I'm using these JARS: android-support-v4-r2-googlemaps.jar + android-support-v4.jar
The prime reason why a Class within your application would be present on your test devices but missing at runtime on some others would be if your class has a dependency on some external feature which is missing on those devices. For example, if you used some Google API which is not part of AOSP and so missing on a plain AOSP build, or a vendor build which does not license these, then your class will be pruned at install time as its dependencies cannot be resolved.
A common reason for your app finding its way into such circumstances might be someone running an inconsistently assembled "custom ROM" on their device.

Android dexguard java.lang.IllegalArgumentException: Class class doesn't support metadata registration

I am using dexguard for obfuscation.I have implemented google contacts sync in my project.when i call the contact service class,this error is coming.i dint find the right solution for the error.i have gone through the mapping file also but i dint get any solution in mapping file.
I have used many options like keep class and keepclassmembers.but still i get the same issue.
ContactsService contactsService = new ContactsService("MYAPPLICATION_NAME");
I got the erro when i call the above class.this is google contact service class.
below is the error log.
java.lang.IllegalArgumentException: Class class o.ڹ doesn't support metadata registration.
at o.ԁ.ˊ(:153)
at o.ԁ.ˊ(:121)
at o.ԁ.ˊ(:384)
at o.Ϋ.ˊ(:533)
at o.Ϋ.ˋ(:455)
at o.Ϋ.ˊ(:42)
at o.গ.ˋ(:144)
at o.ܪ.ˊ(:130)
at o.ᓷ.<init>(:536)
at o.ᓶ.<init>(:201)
at o.ᓶ.<init>(:174)
at o.ᓼ.<init>(:89)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.buzzboard.contacts.fragment.AddEditContactFragment.initContactsService(:1020)
at com.buzzboard.contacts.fragment.AddEditContactFragment.onCreateView(:244)
at android.support.v4.app.Fragment.performCreateView(:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(:1126)
at android.support.v4.app.BackStackRecord.run(:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: registerMetadata [class o.ԁ]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getDeclaredMethod(Class.java:640)
at o.ԁ.ˊ(:139)
please help me.i have keep the some of google contact services classes also.
Looking at the source code and the documentation of com.google.gdata.model.MetadataRegistry, it seems some classes are expected to contain a static method 'registerMetadata', which is then accessed through reflection. Similarly, com.google.gdata.model.Element seems to expect a field 'KEY'. Since ProGuard or DexGuard can't know this, you need to preserve such fields and methods with their original names:
-keepclassmembers class * extends com.google.gdata.model.Element {
public static com.google.gdata.model.ElementKey KEY;
public static void registerMetadata(com.google.gdata.model.MetadataRegistry);
}

Getting Facebook com.facebook.FacebookGraphObjectException: Factory can't proxy method error

I am getting this exception. Please find below the complete stack trace.
com.facebook.FacebookGraphObjectException: Factory can't proxy method: public abstract java.lang.String com.junobe.android.junowallet.SNS.u.a()
at com.facebook.model.GraphObject$Factory.verifyCanProxyClass(SourceFile:290)
at com.facebook.model.GraphObject$Factory.createGraphObjectProxy(SourceFile:216)
at com.facebook.model.GraphObject$Factory.access$0(SourceFile:215)
at com.facebook.model.GraphObject$Factory$GraphObjectProxy.proxyGraphObjectMethods(SourceFile:583)
at com.facebook.model.GraphObject$Factory$GraphObjectProxy.invoke(SourceFile:521)
at $Proxy0.cast()
at com.facebook.Response.getGraphObjectAs(SourceFile:124)
at com.junobe.android.junowallet.SNS.FacebookOpenGraphController.handleResponse(SourceFile:568)
at com.junobe.android.junowallet.SNS.FacebookOpenGraphController.access$6(SourceFile:555)
at com.junobe.android.junowallet.SNS.FacebookOpenGraphController$3.onPostExecute(SourceFile:522)
at com.junobe.android.junowallet.SNS.FacebookOpenGraphController$3.onPostExecute(SourceFile:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3685)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(NativeStart.java)
I am not getting this issue on all devices or all times. Identified this from crashlytics.
I have a hunch that it may be some proguard - obfuscation issue on Android.
I have added the following lines to in the proguard file (As per facebook Documentation):
-keep class com.facebook.** { *; }
-keepattributes Signature
Is there something I am missing .. Any assistance or solution would be welcome.
The only thing that I found that worked for me was to not obfuscate classes that extended facebook classes. I had a bunch of files in one package so for all those files I just excluded them by adding this to my proguard-project.txt.
-keep class com.classes.extending.facebook.** { *; }

What could be causing a ClassNotFoundException?

So I've been working on an app lately which runs fine on most devices. However, sometimes the app crashes at start up with the following error:
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.mypackage.app/com.mypackage.app.activities.MainPager}:
java.lang.ClassNotFoundException: com.mypackage.app.activities.MainPager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException:
com.mypackage.app.activities.MainPager
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1986)
... 12 more
I get that this is because it can't find the class called MainPager. But how is that even possible? I mean, the class should be there.. I put it there myself :) . Why on earth is it not finding it?
EDIT: I think the cause may be that in my manifest I have android:package="com.mypackage.app" defined, but that my launch activity is in the package com.mypackage.app.activities. Currently testing this theory. Still weird that it only happens on some devices, though.
This solution won't work, since I then can't update the APK in Google Play (has to be the same package name in the manifest).
Since ADT update to revision 22 (May 2013) you have to check "Android Private Libraries" check box in Project -> Properties -> Java Build Path -> Order and Export in Eclipse for your older projects to get rid of this exception ...
in the manifest you add Activity name like this
android:name=".activities.MainPager"
because you have given package name
android:package="com.mypackage.app"
Now I am using ART runtime on Nexus 5, and I am getting ClassNotFound for some apps.
You can safely ignore those if that's the case for your users.

Categories

Resources