Android 支持库(v4 和 v7)的 Proguard 规则

Proguard rules for Android support library (v4 & v7)

我在我的应用程序中使用 android 支持库 v4 和 v7。我只使用支持库中的几个小部件(不是全部)。但在最终版本 API 中,支持库中包含了 18K 个函数。 (附截图) 如何减少它们?

以下是我的 proguard 配置。

-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.**

终于我发现了我用proguard配置做的问题。看起来 proguard 会自动在我们的应用程序中添加这些规则。所以我们不需要为 android/google 包添加任何特定内容。除了以下几行,我已经删除了所有其他内容。

-optimizationpasses 5
-keepattributes SourceFile,LineNumberTable

对于所有第 3 方 jar,我们应该添加一个规则。如果第 3 方库来自 Gradle,有时他们会附带附加混淆规则。