[关闭]
@linux1s1s 2017-01-22T08:01:48.000000Z 字数 7350 阅读 2228

Android Proguard

AndroidExtend 2015-04


Introduction

ProGuard typically reads the input jars (or wars, ears, zips, or directories). It then shrinks, optimizes, obfuscates, and preverifies them. Optionally, multiple optimization passes can be performed, each typically followed by another shrinking step. ProGuard writes the processed results to one or more output jars (or wars, ears, zips, or directories). The input may contain resource files, whose names and contents can optionally be updated to reflect the obfuscated class names.

keep option

Fields and methods

<init>      matches any constructor.  所有构造器
<fields>    matches any field.    所有字段
<methods>   matches any method.   所有方法
*           matches any field or method.  所有字段和方法

Example:

keep继承自View的所有公共类
keep传参是Context的构造器(注意必须是全包类名,即使是元类型)

- 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);
    public void set*(...);
}

keep 所有带有R.内部类的 公共静态字段(变量)

- keepclassmembers class **.R$* {                                          
    public static <fields>;
}

比如一般的R类如下:

public final class R {
    public static final class anim {
        public static final int m_hide_bottom_bar_panel = 0x7f040006;
        public static final int m_hide_center_bar_panel = 0x7f040007;
        public static final int m_hide_left_bar_panel = 0x7f040008;
        public static final int m_hide_right_bar_panel = 0x7f040009;
        public static final int m_hide_top_bar_panel = 0x7f04000a;
        public static final int m_popup_enter = 0x7f04000b;
        public static final int m_popup_exit = 0x7f04000c;
        public static final int m_show_bottom_bar_panel = 0x7f04000d;
        public static final int m_show_center_bar_panel = 0x7f04000e;
        public static final int m_show_left_bar_panel = 0x7f04000f;
        public static final int m_show_right_bar_panel = 0x7f040010;
        public static final int m_show_top_bar_panel = 0x7f040011;
    }
}

keep 所有本地方法的名称

- keepclasseswithmembernames class * {                                      
    native <methods>;
}

keep 所有public 类中的public和protected字段和方法

- keep public class * {                                                     
    public protected *;
}

wildcard

$   inner class 
!  a file name can be preceded by an exclamation    For example, "!**.gif,images/**" matches all files in the images directory, except gif files.
%   matches any primitive type ("boolean", "int", etc, but not "void").                     匹配基本类型
?   matches any single character in a class name.                                           通配单个字符(类名称)
*   matches any part of a class name not containing the package separator.                  通配多个字符(类全名称,不包含包分隔符)
**  matches any part of a class name, possibly containing any number of package separators. 通配多个字符(类全名称,包含包分隔符)基本元类型
*** matches any type (primitive or non-primitive, array or non-array).                      通配多个字符(类全名称,包含包分隔符)基本元类型和多维数组
... matches any number of arguments of any type.                                            通配参数(不限类型和个数)

Example:

** get*()  matchs  java.lang.Object getObject()  NOT matchs float getFloat()  java.lang.Object[] getObjects()

*** get*()  matchs  java.lang.Object getObject() float getFloat()  java.lang.Object[] getObjects()

-keep class mybeans.** {
    void set*(***);
    void set*(int, ***);

    boolean is*(); 
    boolean is*(int);

    *** get*();
    *** get*(int);
}

Where should use keep

Processing native methods

-keepclasseswithmembernames class * {
        native <methods>;
}

Processing callback methods

-keep class mypackage.MyCallbackClass {
    void myCallbackMethod(java.lang.String);
}

Processing enumeration classes

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

Processing serializable classes

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    !private <fields>;
    !private <methods>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

Processing bean classes

-keep class mybeans.** {
    void set*(***);
    void set*(int, ***);

    boolean is*(); 
    boolean is*(int);

    *** get*();
    *** get*(int);
}

Processing annotations

-keepattributes *Annotation*

Processing database drivers

-keep class * implements java.sql.Driver

Producing useful obfuscated stack traces

-printmapping out.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

A complete Android application

These options shrink, optimize, and obfuscate all public activities, services, broadcast receivers, and content providers from the compiled classes and external libraries:

-injars      bin/classes
-injars      libs
-outjars     bin/classes-processed.jar
-libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-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.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

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

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注