user6694745
user6694745

Reputation:

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.

Upvotes: 4

Views: 5091

Answers (2)

ianhanniballake
ianhanniballake

Reputation: 199880

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.

Upvotes: 12

gsb
gsb

Reputation: 5640

Check out this project on Github that has proguard rules for popular libraries.

Upvotes: 2

Related Questions