适用于Android支持库(v4& v7)的Proguard规则 [英] Proguard rules for Android support library (v4 & v7)

查看:87
本文介绍了适用于Android支持库(v4& v7)的Proguard规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

以下是我的proguard配置.

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

推荐答案

最后,我发现了使用proguard配置产生的问题.看起来Proguard会在我们的应用程序中自动添加这些规则.因此,我们不需要添加任何特定于android/google软件包的内容.除了以下几行,我删除了所有其他内容.

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

对于所有第三方罐子,我们应该添加一条规则.如果第三方库来自Gradle,则有时他们会附带Proguard规则.

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.

这篇关于适用于Android支持库(v4&amp; v7)的Proguard规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆