proguard混淆配置参数

来源:互联网 发布:大数据迁移 编辑:程序博客网 时间:2024/06/08 17:21
 ProGuard官方网站

1.语法

# 指定代码的压缩级别(0~7)。
-optimizationpasses 5

# 是否使用大小写混合。
-dontusemixedcaseclassnames

# 跳过库文件中非公开类的处理,来加快ProGuard的处理速度。
# 但是如果有公开的类继承了某非公开类,那么此条命令将会导致错误。
-skipnonpubliclibraryclasses

# 指定不去忽略非公共的库类。默认设置
-dontskipnonpubliclibraryclasses

# 指定不去忽略包可见的库类的成员。默认情况下是跳过的,因为程序中不会引用它们,有些情况下人们编写
# 的代码与类库中的类在同一个包下,并且对包中内容加以引用,此时需要加入此条声明。
-dontskipnonpubliclibraryclassmembers

# 混淆时是否做预校验。
-dontpreverify

# 混淆时是否记录日志
-verbose

# 混淆时所采用的算法。
-optimizations {algorithm}
e.g. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

# 从给定的文件中读取配置参数。
-include {filename}

# 指定基础目录为以后相对的档案名称。
-basedirectory {directoryname}

# 指定要处理的应用程序jar,war,ear和目录。
-injars {class_path}

# 指定处理完后要输出的jar,war,ear和目录的名称。
-outjars {class_path}

# 指定要处理的应用程序jar,war,ear和目录所需要的程序库文件。
-libraryjars {classpath}    
e.g. -libraryjars libs/android-support-v4.jar

忽略错误
# 忽略所有警告。
-ignorewarnings

# 不要警告找不到这个包里面的类的相关引用。
-dontwarn {class_specification}
e.g. -dontwarn com.test.**

保留选项
# 保护指定的类文件和类的成员。
-keep {Modifier} {class_specification}    
e.g. ①保留某个类名不被混淆
    -keep public class com.bean.UserBean
    ②保留类及其所有成员不被混淆
    -keep public class com.bean.UserBean { *;}
    ③只保留类名及其部分成员不被混淆
    -keep public class com.ebt.app.common.bean.Customer { 
        static final<fields>;
        private void get*();
     }

# 保护指定类的成员,如果此类受到保护他们会保护的更好。
-keepclassmembers {modifier} {class_specification}

# 保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在。
-keepclasseswithmembers {class_specification}    
e.g. 保留类及其所有成员不被混淆
    -keepclasseswithmembers class com.bean.UserBean {
        <init>;#匹配所有构造函数
        <fields>;#匹配所有成员
        <methods>;#匹配所有方法
     }

# 保护指定的类和类的成员的名称(如果他们不会压缩步骤中删除)。
-keepnames {class_specification}

# 保护指定的类的成员的名称(如果他们不会压缩步骤中删除)。
-keepclassmembernames {class_specification}

# 保护指定的类和类的成员的名称,如果所有指定的类成员出席(在压缩步骤之后)。
-keepclasseswithmembernames {class_specification}

# 列出类和类的成员-keep选项的清单,标准输出到给定的文件。
-printseeds {filename}

压缩
# 不压缩输入的类文件。
-dontshrink
-printusage {filename}
-whyareyoukeeping {class_specification}

优化
# 不优化输入的类文件。
-dontoptimize

# 优化时假设指定的方法,没有任何副作用。
-assumenosideeffects {class_specification}

# 优化时允许访问并修改有修饰符的类和类的成员。
-allowaccessmodification

混淆
# 不混淆输入的类文件。
-dontobfuscate

-printmapping {filename}

# 重用映射增加混淆。
-applymapping {filename}

# 使用给定文件中的关键字作为要混淆方法的名称。
-obfuscationdictionary {filename}

# 混淆时应用侵入式重载。
-overloadaggressively

# 确定统一的混淆类的成员名称来增加混淆。
-useuniqueclassmembernames

# 重新包装所有重命名的包并放在给定的单一包中。
-flattenpackagehierarchy {package_name}

# 重新包装所有重命名的类文件中放在给定的单一包中。
-repackageclass {package_name}

# 混淆时不会产生形形色色的类名。
-dontusemixedcaseclassnames

# 保护给定的可选属性,例如Exceptions(异常), LineNumberTable(保留行号), 
# *Annotation*(注解), LocalVariableTable(局部变量), SourceFile(源文件), 
# Deprecated(废弃), Synthetic(合成), Signature(泛型), and InnerClasses(内部类)。
-keepattributes {attribute_name,...}   
e.g. -keepattributes Exceptions,InnerClasses,*Annotation*,Signature,LineNumberTable

# 设置源文件中给定的字符串常量。
-renamesourcefileattribute {string}


2.用法

以-keep为例
# 保留UserBean类名不被混淆
-keep class com.bean.UserBean
 
# 保留UserBean类及其所有成员不被混淆
-keep class com.bean.UserBean { *;}
 
# 保留继承Application类名不被混淆
-keep class * extends android.app.Application 
 
# 保留继承Application类名及其所有成员不被混淆
-keep class * extends android.app.Application { *;}
 
# 保留实现OnClickListener类名不被混淆
-keep class * implements android.view.View.OnClickListener

3.修饰符
①类、接口、枚举等
Every classname must be fully qualified, e.g. java.lang.String. Inner classes are separated by a dollar sign "$", e.g. java.lang.Thread$State. Class names may be specified as regular expressions containing the following wildcards:

?    matches any single character in a class name, but not the package separator. For example, "mypackage.Test?" matches "mypackage.Test1" and "mypackage.Test2", but not "mypackage.Test12".

*    matches any part of a class name not containing the package separator. For example, "mypackage.*Test*" matches "mypackage.Test" and "mypackage.YourTestApplication", but not "mypackage.mysubpackage.MyTest". Or, more generally, "mypackage.*" matches all classes in "mypackage", but not in its subpackages.

**    matches any part of a class name, possibly containing any number of package separators. For example, "**.Test" matches all Test classes in all packages except the root package. Or, "mypackage.**" matches all classes in "mypackage" and in its subpackages.

For additional flexibility, class names can actually be comma-separated lists of class names, with optional ! negators, just like file name filters. This notation doesn't look very Java-like, so it should be used with moderation.

For convenience and for backward compatibility, the class name * refers to any class, irrespective of its package.

②构造函数、成员变量、方法
Fields and methods are specified much like in Java, except that method argument lists don't contain argument names (just like in other tools like javadoc and javap). The specifications can also contain the following catch-all wildcards:
<init>    matches any constructor.
<fields>    matches any field.
<methods>    matches any method.
*   matches any field or method.
Note that the above wildcards don't have return types. Only the <init> wildcard has an argument list.
Fields and methods may also be specified using regular expressions. Names can contain the following wildcards:
?    matches any single character in a method name.
*    matches any part of a method name.
Types in descriptors can contain the following wildcards:
%   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.
Note that the ?, *, and ** wildcards will never match primitive types. Furthermore, only the *** wildcards will match array types of any dimension. For example, "** get*()" matches "java.lang.Object getObject()", but not "float getFloat()", nor "java.lang.Object[] getObjects()".