What are the recommended proguard settings for android support library? - android

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.

Related

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);
}

Genson with Android - Proguard config

I have a problem with proguard config in Android Project.
I'm using Genson to parse incoming JSON data.
It is fast and there is no need for extra configuration or deserializers, because on the Server-side there is also Genson.
Everything works fine in debug mode, but in release, with proguard it doesn't.
Unfortunately I have some error during runtime:
FATAL EXCEPTION: main
Process: com.es.mobile.meedy, PID: 16650
java.lang.UnsupportedOperationException: Couldn't find parameter at 0 from type interface com.owlike.genson.Converter , you should first locate the parameterized type, expand it and then use typeOf.
at com.owlike.genson.reflect.TypeUtil.typeOf(Unknown Source)
at com.owlike.genson.GensonBuilder.withConverters(Unknown Source)
at com.mypackage.f.k.a(Unknown Source)
at com.a.a.b.n.a(Unknown Source)
at com.a.a.i.run(Unknown Source)
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:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
It happens even if datamodel is in keep class. I tried all configrations with keep class, classmembers, etc. What else can i do?
EDIT
What else do I know:
I tried keep class with all classes in my project.
I have keep class with genson (-keep class com.owlike.genson.** { *; })
Instanceof with my class shows that it is instance of com.owlike.genson.Converter
The solution to the problem was to add -keepattributes Signature
If annotations are being used then this option should be enabled too -keepattributes *Annotation*
Also the application class being ser/de by Genson must also be provided:
-keep class com.mypackage.model.** { *; }
-keep class com.owlike.genson.*{ *; }
Add that line to your ProGuard configuration.
-keep [,modifier,...] class_specification
Specifies classes and class members (fields and methods) to be preserved as entry points to your
code. For example, in order to keep an application, you can specify
the main class along with its main method. In order to process a
library, you should specify all publicly accessible elements.

Android - Proguard error with static String

I'm running ProGuard for my release build and trying to optimize it as much as possible. The only custom rules I've added so far are Serialization and Facebook ones. I'm running my release build now and I'm getting a stack trace and what it maps back to is really throwing me off. Here's my stack trace:
java.lang.NullPointerException
E/AndroidRuntime(10842): at com.myapp.android.myapp.dh.a(Unknown Source)
E/AndroidRuntime(10842): at android.support.v4.app.Fragment.b(Unknown Source)
E/AndroidRuntime(10842): at android.support.v4.app.w.a(Unknown Source)
E/AndroidRuntime(10842): at android.support.v4.app.p.onCreatePanelMenu(Unknown Source)
E/AndroidRuntime(10842): at android.support.v7.a.g.a(Unknown Source)
E/AndroidRuntime(10842): at android.support.v7.a.m.a(Unknown Source)
E/AndroidRuntime(10842): at android.support.v7.a.g.onCreatePanelMenu(Unknown Source)
In mapping, com.myapp.android.myapp.dh.a is:
com.myapp.android.myapp.LocalFragment -> com.myapp.android.myapp.dh:
java.lang.String USER_ACCOUNT -> a
In my actual code it's this:
public static String USER_ACCOUNT = "com.myapp.android.myapp.LocalFragment.user_account";
Now this constant is used in multiple places throughout this Fragment, but it should never be null. Anyone seen a problem like this before? Does Proguard do weird stuff with statics?
Edit: This error is thrown and the app crashes basically as soon as I take the action that would load this Fragment.
EDIT AGAIN
After a bit more investigation, I was wrong about what a was. a was that Static member, but a is also a method. It's the onCreate method. I'm also seeing this error in logcat before the NPE is thrown in onCreate:
W/SupportMenuInflater(10842): Cannot instantiate class: android.support.v7.widget.SearchView
W/SupportMenuInflater(10842): java.lang.NoSuchMethodException: <init> [class android.content.Context]
W/SupportMenuInflater(10842): at java.lang.Class.getConstructorOrMethod(Class.java:472)
W/SupportMenuInflater(10842): at java.lang.Class.getConstructor(Class.java:446)
W/SupportMenuInflater(10842): at android.support.v7.internal.view.e.a(Unknown Source)
W/SupportMenuInflater(10842): at android.support.v7.internal.view.e.a(Unknown Source)
W/SupportMenuInflater(10842): at android.support.v7.internal.view.c.a(Unknown Source)
W/SupportMenuInflater(10842): at android.support.v7.internal.view.c.inflate(Unknown Source)
W/SupportMenuInflater(10842): at com.myapp.android.myapp.dh.a(Unknown Source)
You have a problem with proguard obfuscating the support libraries, or specifically, the SearchView component that you probably try to initiate in your fragment. To avoid this, you need to add few exclusions to proguard configuration file (proguard.cfg).
-keep class android.support.v7.widget.** { *; }
-keep interface android.support.v7.widget.** { *; }
This will keep all classes and interfaces inside the widget package of the support library from being obfuscated.

ActionBarSherlock 4.4: Activity icon resource not correct

I'm using the newest ActionBarSherlock release (4.4) and many of my Android 2 users experience a fatal crash while the sherlock library tries to load the activity icon.
I tried cleaning the project but the bug keeps appearing (I suspect the generated R file for the library is incorrect?). I found some topics covering this crash but no working solutions.
My project exists out of the ActionBarSherlock library which is used by another library project containing some Activities. A few other Android projects use the library project with the Activities in it. I don't think this could be the problem, could it?
Important thing to notice is that the app works fine on a emulator running Android 2.x. I'm also using proguard.
Could the crash have something to do with the loadLogoFromManifest method?
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/src/com/actionbarsherlock/internal/ResourcesCompat.java#L112
Devices and Android versions affected so far
Optimus L3 (e0)
Galaxy Pocket (GT-S5300)
Galaxy Y (GT-S5360)
Android 2.3.3 - 2.3.7
Proguard
-keepattributes SourceFile,LineNumberTable
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keepattributes *Annotation*
-keep public class * extends com.actionbarsherlock.app.SherlockListFragment
-keep public class * extends com.actionbarsherlock.app.SherlockFragment
-keep public class * extends com.actionbarsherlock.app.SherlockDialogFragment
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
Stacktrace
java.lang.RuntimeException: Unable to start activity ComponentInfo{packagehere/activityhere}: android.view.InflateException: Binary XML file line #32: Error inflating class com.actionbarsherlock.internal.widget.ActionBarView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
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(Native Method)
Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class com.actionbarsherlock.internal.widget.ActionBarView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:1010)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:902)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:836)
at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:261)
at someactivity.onCreate(BibleMain.java:128)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: android.content.res.Resources$NotFoundException: File from drawable resource ID #0x7f020091
at android.content.res.Resources.loadDrawable(Resources.java:1714)
at android.content.res.Resources.getDrawable(Resources.java:581)
at com.actionbarsherlock.internal.widget.ActionBarView.(ActionBarView.java:191)
... 27 more
Caused by: java.io.FileNotFoundException:
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:406)
at android.content.res.Resources.loadDrawable(Resources.java:1706)
... 29 more

Application class not found in Android after using ProGuard

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

Categories

Resources