@linux1s1s
2017-01-22T16:01:48.000000Z
字数 7350
阅读 2380
AndroidExtend
2015-04
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 [,modifier,...] class_specification //指定的类和类成员被保留作为 入口
Specifies classes and class members (fields and methods) to be preserved as entry points to your code.
For example, in order to keep an application, you can specify the main class along with its main method.
In order to process a library, you should specify all publicly accessible elements.
keepclassmembers [,modifier,...] class_specification //指定的类成员被保留
Specifies class members to be preserved, if their classes are preserved as well.
For example, you may want to keep all serialization fields and methods of classes that implement the Serializable interface.
keepclasseswithmembers [,modifier,...] class_specification //指定的类和类成员被保留
Specifies classes and class members to be preserved, on the condition that all of the specified class members are present.
For example, you may want to keep all applications that have a main method, without having to list them explicitly.
keepnames class_specification //保护指定的类和类的成员的名称(如果他们不会在压缩步骤中删除)
Short for -keep,allowshrinking class_specification
Specifies classes and class members whose names are to be preserved, if they aren't removed in the shrinking phase.
For example, you may want to keep all class names of classes that implement the Serializable interface,
so that the processed code remains compatible with any originally serialized classes.
Classes that aren't used at all can still be removed. Only applicable when obfuscating.
keepclassmembernames class_specification //保护指定的类的成员的名称(如果他们不会在压缩步骤中删除)
Short for -keepclassmembers,allowshrinking class_specification
Specifies class members whose names are to be preserved, if they aren't removed in the shrinking phase.
For example, you may want to preserve the name of the synthetic class$ methods when processing a library compiled by JDK 1.2 or older,
so obfuscators can detect it again when processing an application that uses the processed library (although ProGuard itself doesn't need this).
Only applicable when obfuscating.
keepclasseswithmembernames class_specification //保护指定的类和类的成员的名称(如果他们不会在压缩步骤中删除)
Short for -keepclasseswithmembers,allowshrinking class_specification
Specifies classes and class members whose names are to be preserved,
on the condition that all of the specified class members are present after the shrinking phase.
For example, you may want to keep all native method names and the names of their classes,
so that the processed code can still link with the native library code. Native methods that aren't used at all can still be removed.
If a class file is used, but none of its native methods are, its name will still be obfuscated. Only applicable when obfuscating.
<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 *;
}
$ 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);
}
-keepclasseswithmembernames class * {
native <methods>;
}
-keep class mypackage.MyCallbackClass {
void myCallbackMethod(java.lang.String);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-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();
}
-keep class mybeans.** {
void set*(***);
void set*(int, ***);
boolean is*();
boolean is*(int);
*** get*();
*** get*(int);
}
-keepattributes *Annotation*
-keep class * implements java.sql.Driver
-printmapping out.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
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>;
}