Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard rules for Android support library (v4 & v7)

I use, android support library v4 and v7 in my app. I am using only few widgets from the support library (not all of them). But in final release API, 18K functions are included from support libraries. (screenshot attached) How to reduce them?

enter image description here

Following is my proguard config.

-optimizationpasses 3

-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.preference.Preference
-keep public class com.android.vending.billing.IInAppBillingService
-keepattributes SourceFile,LineNumberTable
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.content.Context {
    public void *(android.view.View);
    public void *(android.view.MenuItem);
}
-keep public class * extends android.app.Activity
-keep public class * extends android.support.v7.app.AppCompatActivity
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.app.IntentService




-dontwarn com.google.appengine.api.urlfetch.**
-dontwarn rx.**
-dontwarn retrofit.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-keep class com.google.** { *; }
#-keep class org.apache.** { *; }
#-keep class android.support.** { *; }
-keep class com.makeramen.** { *; }
-dontwarn android.net.http.AndroidHttpClient
-dontwarn com.google.android.gms.**



# Suppress warnings if you are NOT using IAP:
-dontwarn com.google.**
#-dontwarn org.apache.**
-dontwarn com.makeramen.**
-dontwarn android.support.**


-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }

## -------------- Google play services ------------------
-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}


## --------------- For native methods ---------------------
-keepclasseswithmembernames class * {
    native <methods>;
}

# ------------- To maintain custom components names that are used on layouts XML ---------------
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}


## ---------- keep generic information ---------------
-keepattributes Signature,*Annotation*,EnclosingMethod
-keep class sun.misc.Unsafe { *; }


## -------------- keep custom components --------------------
-keep public class * extends android.view.View {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keep public class * extends android.view.View {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}


## ------------ We want to keep methods in Activity that could be used in the XML attribute onClick -------------
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}



## ---------- For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -------------
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}


##------------ To keep parcelable classes (to serialize - deserialize objects to sent through Intents) -------------
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}



## ------------ Keep the R.java file ---------------
-keepclassmembers class **.R$* {
    public static <fields>;
}



##---------------Begin: proguard configuration for Bean class  ----------
#-keep public class com.nektapp.datamodels.**{ *; }
#-keep public class com.nektapp.matcher.model.**{ *;}

##---------- Keep external jars from libs folder -----------------


-libraryjars libs


##-libraryjars libs/universal-image-loader-1.9.3.jar
##-libraryjars libs/dropbox-sync-sdk-android.jar
##-libraryjars libs/gson-2.3.1.jar
##-libraryjars libs/HockeySDK-3.0.2.jar
##-libraryjars libs/jackson-core-asl-1.9.7.jar
##-libraryjars libs/jackson-mapper-asl-1.9.7.jar
##-libraryjars libs/logback-android-1.0.10-2.jar
##-libraryjars libs/slf4j-api-1.7.5.jar


##-dontwarn org.codehaus.jackson.**
 ##-keep class org.codehaus.jackson.** { *; }

-dontwarn ch.qos.logback.**
-keep class ch.qos.logback.** { *; }

-dontwarn org.slf4j.**
-keep class org.slf4j.** { *; }

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
    public void set*(***);
    public *** get*();
    <fields>;
}

# Amazon AWS S3 ----------------------
# Class names are needed in reflection
-keepnames class com.amazonaws.**
# Request handlers defined in request.handlers
-keep class com.amazonaws.services.**.*Handler
# The following are referenced but aren't required to run
-dontwarn com.fasterxml.jackson.**
#-dontwarn org.apache.commons.logging.**
# Android 6.0 release removes support for the Apache HTTP client
#-dontwarn org.apache.http.**
# The SDK has several references of Apache HTTP client
-dontwarn com.amazonaws.http.**
-dontwarn com.amazonaws.metrics.**
# Amazon AWS S3 ----------------------

# Support design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

# App compat
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.app.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
    public <init>(android.content.Context);
}
-keep class android.support.v7.widget.RoundRectDrawable { *; }

# picasso
-dontwarn com.squareup.okhttp.**
like image 663
Aram Avatar asked Sep 29 '16 14:09

Aram


1 Answers

Finally I found out the problem I made with the proguard config. Looks like proguard automatically add these rules in our app. So we dont need to add anything specific for android/google packages. Except the following lines, I have removed everything else.

-optimizationpasses 5
-keepattributes SourceFile,LineNumberTable

For all the 3rd party jars, we are supposed to add a rule. If the 3rd party library comes from Gradle, sometimes they have attached proguard rules along with it.

like image 195
Aram Avatar answered Sep 28 '22 00:09

Aram