ProGuard使用笔记

最近要混淆一部分jar包, 找到了ProGuard, 开始想省事直接用GUI, 结果看的很凌乱, 网上也是配置项的内容居多, 最终还是选择用配置项.
官方文档http://proguard.sourceforge.net/

错误总结:http://sd7y.iteye.com/blog/2047896

使用配置项的命令 java   -jar proguard.Jar  @配置文件
如java   -jar proguard.Jar  @aaa

下面是对源代码中的boilerplate.pro进行的一个注释, 算是一个配置文件的模板样例吧, E文不好, 如注释有误请指正
# Keep - Applications. Keep all application classes, along with their 'main' methods.
# 保留所有公共类中的main方法
-keepclasseswithmembers public class * {
    public static void main(java.lang.String[]);
}

# Keep - Applets. Keep all extensions of java.applet.Applet.
# 保留所有继承自java.appent.Applet的类
-keep public class * extends java.applet.Applet

# Keep - Servlets. Keep all extensions of javax.servlet.Servlet.
# 保留所有继承自javax.servlet.Servlet的类
-keep public class * extends javax.servlet.Servlet

# Keep - Midlets. Keep all extensions of javax.microedition.midlet.MIDlet.
# 保留所有继承自javax.microedition.midlet.MIDlet的类
-keep public class * extends javax.microedition.midlet.MIDlet

# Keep - Xlets. Keep all extensions of javax.tv.xlet.Xlet.
# 保留所有继承自javax.tv.xlet.Xlet的类
-keep public class * extends javax.tv.xlet.Xlet

# Keep - Library. Keep all public and protected classes, fields, and methods.
# 保留所有public和protected的类, 属性和方法
-keep public class * {
    public protected <fields>;
    public protected <methods>;
}

# Also keep - Enumerations. Keep the special static methods that are required in enumeration classes.
# 保留枚举中的指定的静态方法
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Also keep - Serialization code. Keep all fields and methods that are used for serialization.
# 保留所有序列化相关内容
-keepclassmembers class * extends java.io.Serializable {
    static final long serialVersionUID;
    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();
}

# Also keep - BeanInfo classes. Keep all implementations of java.beans.BeanInfo.
# 保留实现了java.beans.BeanInfo接口的类
-keep class * implements java.beans.BeanInfo

# Also keep - Bean classes. Keep all specified classes, along with their getters and setters.
# 保留get/set/is方法
-keep class * {
    void set*(***);
    void set*(int,***);

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

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

# Also keep - Database drivers. Keep all implementations of java.sql.Driver.
# 保留实现了java.sql.Driver接口的类
-keep class * implements java.sql.Driver

# Also keep - Swing UI L&F. Keep all extensions of javax.swing.plaf.ComponentUI, along with the special 'createUI' method.
# 保留继承自javax.swing.plaf.ComponentUI类中的createUI方法
-keep class * extends javax.swing.plaf.ComponentUI {
    public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent);
}

# Also keep - RMI interfaces. Keep all interfaces that extend the java.rmi.Remote interface, and their methods.
# 保留继承自java.rmi.Remote的全部接口及方法
-keep interface * extends java.rmi.Remote {
    <methods>;
}

# Also keep - RMI implementations. Keep all implementations of java.rmi.Remote,
# including any explicit or implicit implementations of Activatable, with their
# two-argument constructors.
# 保留实现了java.rmi.Remote接口, 包括Activatable任何显式或隐式实现(在方法有两个参数的时候)
-keep class * implements java.rmi.Remote {
    <init>(java.rmi.activation.ActivationID,java.rmi.MarshalledObject);
}

# Keep names - Native method names. Keep all native class/method names.
# 保留任何类中的native方法
-keepclasseswithmembernames class * {
    native <methods>;
}

# Keep names - _class method names. Keep all .class method names. This may be
# useful for libraries that will be obfuscated again with different obfuscators.
# 保留所有方法名以class结尾的方法, 这可能导致已被混效过的将被再次混淆
-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String,boolean);
}

# Remove - System method calls. Remove all invocations of System methods without side effects whose return values are not used.
# 删除所有调用了java.lang.System中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.System {
    public static long currentTimeMillis();
    static java.lang.Class getCallerClass();
    public static int identityHashCode(java.lang.Object);
    public static java.lang.SecurityManager getSecurityManager();
    public static java.util.Properties getProperties();
    public static java.lang.String getProperty(java.lang.String);
    public static java.lang.String getenv(java.lang.String);
    public static java.lang.String mapLibraryName(java.lang.String);
    public static java.lang.String getProperty(java.lang.String,java.lang.String);
}

# Remove - Math method calls. Remove all invocations of Math methods without side effects whose return values are not used.
# 删除所有调用了java.lang.Math中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.Math {
    public static double sin(double);
    public static double cos(double);
    public static double tan(double);
    public static double asin(double);
    public static double acos(double);
    public static double atan(double);
    public static double toRadians(double);
    public static double toDegrees(double);
    public static double exp(double);
    public static double log(double);
    public static double log10(double);
    public static double sqrt(double);
    public static double cbrt(double);
    public static double IEEEremainder(double,double);
    public static double ceil(double);
    public static double floor(double);
    public static double rint(double);
    public static double atan2(double,double);
    public static double pow(double,double);
    public static int round(float);
    public static long round(double);
    public static double random();
    public static int abs(int);
    public static long abs(long);
    public static float abs(float);
    public static double abs(double);
    public static int max(int,int);
    public static long max(long,long);
    public static float max(float,float);
    public static double max(double,double);
    public static int min(int,int);
    public static long min(long,long);
    public static float min(float,float);
    public static double min(double,double);
    public static double ulp(double);
    public static float ulp(float);
    public static double signum(double);
    public static float signum(float);
    public static double sinh(double);
    public static double cosh(double);
    public static double tanh(double);
    public static double hypot(double,double);
    public static double expm1(double);
    public static double log1p(double);
}

# Remove - Number method calls. Remove all invocations of Number methods without side effects whose return values are not used.
# 删除所有调用了[在java.lang包中继承自java.lang.Number]中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.* extends java.lang.Number {
    public static java.lang.String toString(byte);
    public static java.lang.Byte valueOf(byte);
    public static byte parseByte(java.lang.String);
    public static byte parseByte(java.lang.String,int);
    public static java.lang.Byte valueOf(java.lang.String,int);
    public static java.lang.Byte valueOf(java.lang.String);
    public static java.lang.Byte decode(java.lang.String);
    public int compareTo(java.lang.Byte);

    public static java.lang.String toString(short);
    public static short parseShort(java.lang.String);
    public static short parseShort(java.lang.String,int);
    public static java.lang.Short valueOf(java.lang.String,int);
    public static java.lang.Short valueOf(java.lang.String);
    public static java.lang.Short valueOf(short);
    public static java.lang.Short decode(java.lang.String);
    public static short reverseBytes(short);
    public int compareTo(java.lang.Short);

    public static java.lang.String toString(int,int);
    public static java.lang.String toHexString(int);
    public static java.lang.String toOctalString(int);
    public static java.lang.String toBinaryString(int);
    public static java.lang.String toString(int);
    public static int parseInt(java.lang.String,int);
    public static int parseInt(java.lang.String);
    public static java.lang.Integer valueOf(java.lang.String,int);
    public static java.lang.Integer valueOf(java.lang.String);
    public static java.lang.Integer valueOf(int);
    public static java.lang.Integer getInteger(java.lang.String);
    public static java.lang.Integer getInteger(java.lang.String,int);
    public static java.lang.Integer getInteger(java.lang.String,java.lang.Integer);
    public static java.lang.Integer decode(java.lang.String);
    public static int highestOneBit(int);
    public static int lowestOneBit(int);
    public static int numberOfLeadingZeros(int);
    public static int numberOfTrailingZeros(int);
    public static int bitCount(int);
    public static int rotateLeft(int,int);
    public static int rotateRight(int,int);
    public static int reverse(int);
    public static int signum(int);
    public static int reverseBytes(int);
    public int compareTo(java.lang.Integer);

    public static java.lang.String toString(long,int);
    public static java.lang.String toHexString(long);
    public static java.lang.String toOctalString(long);
    public static java.lang.String toBinaryString(long);
    public static java.lang.String toString(long);
    public static long parseLong(java.lang.String,int);
    public static long parseLong(java.lang.String);
    public static java.lang.Long valueOf(java.lang.String,int);
    public static java.lang.Long valueOf(java.lang.String);
    public static java.lang.Long valueOf(long);
    public static java.lang.Long decode(java.lang.String);
    public static java.lang.Long getLong(java.lang.String);
    public static java.lang.Long getLong(java.lang.String,long);
    public static java.lang.Long getLong(java.lang.String,java.lang.Long);
    public static long highestOneBit(long);
    public static long lowestOneBit(long);
    public static int numberOfLeadingZeros(long);
    public static int numberOfTrailingZeros(long);
    public static int bitCount(long);
    public static long rotateLeft(long,int);
    public static long rotateRight(long,int);
    public static long reverse(long);
    public static int signum(long);
    public static long reverseBytes(long);
    public int compareTo(java.lang.Long);

    public static java.lang.String toString(float);
    public static java.lang.String toHexString(float);
    public static java.lang.Float valueOf(java.lang.String);
    public static java.lang.Float valueOf(float);
    public static float parseFloat(java.lang.String);
    public static boolean isNaN(float);
    public static boolean isInfinite(float);
    public static int floatToIntBits(float);
    public static int floatToRawIntBits(float);
    public static float intBitsToFloat(int);
    public static int compare(float,float);
    public boolean isNaN();
    public boolean isInfinite();
    public int compareTo(java.lang.Float);

    public static java.lang.String toString(double);
    public static java.lang.String toHexString(double);
    public static java.lang.Double valueOf(java.lang.String);
    public static java.lang.Double valueOf(double);
    public static double parseDouble(java.lang.String);
    public static boolean isNaN(double);
    public static boolean isInfinite(double);
    public static long doubleToLongBits(double);
    public static long doubleToRawLongBits(double);
    public static double longBitsToDouble(long);
    public static int compare(double,double);
    public boolean isNaN();
    public boolean isInfinite();
    public int compareTo(java.lang.Double);

    public byte byteValue();
    public short shortValue();
    public int intValue();
    public long longValue();
    public float floatValue();
    public double doubleValue();

    public int compareTo(java.lang.Object);
    public boolean equals(java.lang.Object);
    public int hashCode();
    public java.lang.String toString();
}

# Remove - String method calls. Remove all invocations of String methods without side effects whose return values are not used.
# 删除所有调用了java.lang.String中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.String {
    public static java.lang.String copyValueOf(char[]);
    public static java.lang.String copyValueOf(char[],int,int);
    public static java.lang.String valueOf(boolean);
    public static java.lang.String valueOf(char);
    public static java.lang.String valueOf(char[]);
    public static java.lang.String valueOf(char[],int,int);
    public static java.lang.String valueOf(double);
    public static java.lang.String valueOf(float);
    public static java.lang.String valueOf(int);
    public static java.lang.String valueOf(java.lang.Object);
    public static java.lang.String valueOf(long);
    public boolean contentEquals(java.lang.StringBuffer);
    public boolean endsWith(java.lang.String);
    public boolean equalsIgnoreCase(java.lang.String);
    public boolean equals(java.lang.Object);
    public boolean matches(java.lang.String);
    public boolean regionMatches(boolean,int,java.lang.String,int,int);
    public boolean regionMatches(int,java.lang.String,int,int);
    public boolean startsWith(java.lang.String);
    public boolean startsWith(java.lang.String,int);
    public byte[] getBytes();
    public byte[] getBytes(java.lang.String);
    public char charAt(int);
    public char[] toCharArray();
    public int compareToIgnoreCase(java.lang.String);
    public int compareTo(java.lang.Object);
    public int compareTo(java.lang.String);
    public int hashCode();
    public int indexOf(int);
    public int indexOf(int,int);
    public int indexOf(java.lang.String);
    public int indexOf(java.lang.String,int);
    public int lastIndexOf(int);
    public int lastIndexOf(int,int);
    public int lastIndexOf(java.lang.String);
    public int lastIndexOf(java.lang.String,int);
    public int length();
    public java.lang.CharSequence subSequence(int,int);
    public java.lang.String concat(java.lang.String);
    public java.lang.String replaceAll(java.lang.String,java.lang.String);
    public java.lang.String replace(char,char);
    public java.lang.String replaceFirst(java.lang.String,java.lang.String);
    public java.lang.String[] split(java.lang.String);
    public java.lang.String[] split(java.lang.String,int);
    public java.lang.String substring(int);
    public java.lang.String substring(int,int);
    public java.lang.String toLowerCase();
    public java.lang.String toLowerCase(java.util.Locale);
    public java.lang.String toString();
    public java.lang.String toUpperCase();
    public java.lang.String toUpperCase(java.util.Locale);
    public java.lang.String trim();
}

# Remove - StringBuffer method calls. Remove all invocations of StringBuffer methods without side effects whose return values are not used.
# 删除所有调用了java.lang.StringBuffer中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.StringBuffer {
    public java.lang.String toString();
    public char charAt(int);
    public int capacity();
    public int codePointAt(int);
    public int codePointBefore(int);
    public int indexOf(java.lang.String,int);
    public int lastIndexOf(java.lang.String);
    public int lastIndexOf(java.lang.String,int);
    public int length();
    public java.lang.String substring(int);
    public java.lang.String substring(int,int);
}

# Remove - StringBuilder method calls. Remove all invocations of StringBuilder methods without side effects whose return values are not used.
# 删除所有调用了java.lang.StringBuilder中指定方法, 但是没有使用其返回值的语句
-assumenosideeffects public class java.lang.StringBuilder {
    public java.lang.String toString();
    public char charAt(int);
    public int capacity();
    public int codePointAt(int);
    public int codePointBefore(int);
    public int indexOf(java.lang.String,int);
    public int lastIndexOf(java.lang.String);
    public int lastIndexOf(java.lang.String,int);
    public int length();
    public java.lang.String substring(int);
    public java.lang.String substring(int,int);
}

# Remove debugging - Throwable_printStackTrace calls. Remove all invocations of Throwable.printStackTrace().
# 删除debug信息, 移除所有调用了Throwable.printStackTrace()方法的语句
-assumenosideeffects public class java.lang.Throwable {
    public void printStackTrace();
}

# Remove debugging - Thread_dumpStack calls. Remove all invocations of Thread.dumpStack().
# 删除debug信息, 移除所有调用了Thread.dumpStack()方法的语句
-assumenosideeffects public class java.lang.Thread {
    public static void dumpStack();
}

# Remove debugging - All logging API calls. Remove all invocations of the logging API whose return values are not used.
# 删除所有调用java.util.logging的方法
-assumenosideeffects public class java.util.logging.* {
    <methods>;
}

# Remove debugging - All Log4j API calls. Remove all invocations of the Log4j API whose return values are not used.
# 删除所有调用org.apache.log4j.**的方法
-assumenosideeffects public class org.apache.log4j.** {
    <methods>;
}



#不混淆public和protected的属性和方法
-keep public class * {  
    public protected <fields>;  
    public protected <methods>;  
}

#无混淆的基本语句:
#确定统一的混淆类的成员名称来增加混淆
-useuniqueclassmembernames
#不压缩输入的类文件
-dontshrink
#混淆时不会产生形形色色的类名(不会改变类名?)
-dontusemixedcaseclassnames
#不优化输入的类文件
-dontoptimize
#忽略警告
-ignorewarnings

-verbose

#org包内的不警告
-dontwarn org.*

#保留Exceptions(抛出异常), InnerClasses(内部类), Signature(签名),Deprecated(弃用?), 
#SourceFile(源文件), LineNumberTable(行号?), *Annotation*(注释), EnclosingMethod(匿名方法)
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
#保留公共类
-keep public class *
#保留org包
-keep class org.* {*; }  
#保留android的R文件?
-keep class **.R$* {  
 *;  
}

-keep class **.$* {  
 *;  
}

-keepclasseswithmembernames class * {
    public <methods>;
    public <fields>;
}

-keepclasseswithmembernames class * {
    public <methods>;
    private <methods>;
    protected <methods>;
    public <fields>;
    private <fields>;
    protected <fields>;
}


GUI使用方法(建议使用配置文件方式):
在命令行中输入java -jar proguardgui.jar, 打开界面如下
  • ProGuard(首页)可加载已有的配置文件
  • ProGuard使用笔记_第1张图片
  • input/output(输入输出配置)
  • ProGuard使用笔记_第2张图片
  • Shrinking(压缩代码),可以删除一些无用代码
  • Obfuscator(混淆)
  • ProGuard使用笔记_第3张图片
    ProGuard使用笔记_第4张图片
  • Optimization(性能优化),用处不大
  • Information(信息参数)
  • ProGuard使用笔记_第5张图片
  • Process(处理)执行操作, 保存配置等


附上网上找来的选项解释
ProGuard的作用:

1.创建紧凑的代码文档是为了更快的网络传输,快速装载和更小的内存占用.
2.创建的程序和程序库很难使用反向工程.
3.所以它能删除来自源文件中的没有调用的代码
4.充分利用java6的快速加载的优点来提前检测和返回java6中存在的类文件.

#参数:
#--------------------------------------------------------------
#从给定的文件中读取配置参数
-include{filename}
#指定基础目录为以后相对的档案名称
-basedirectory{directoryname}
#指定要处理的应用程序jar,war,ear和目录
-injars{class_path}
#指定处理完后要输出的jar,war,ear和目录的名称
-outjars{class_path}
#指定要处理的应用程序jar,war,ear和目录所需要的程序库文件
-libraryjars{classpath}
#指定不去忽略非公共的库类。
-dontskipnonpubliclibraryclasses
#指定不去忽略包可见的库类的成员。
-dontskipnonpubliclibraryclassmembers

#保留选项:
#--------------------------------------------------------------
#保护指定的类文件和类的成员
-keep{Modifier}{class_specification}
#保护指定类的成员,如果此类受到保护他们会保护的更好
-keepclassmembers{modifier}{class_specification}
#保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在。
-keepclasseswithmembers{class_specification}
#保护指定的类和类的成员的名称(如果他们不会压缩步骤中删除)
-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
#保护给定的可选属性,例如LineNumberTable,LocalVariableTable,SourceFile,
#Deprecated,Synthetic,Signature,andInnerClasses.
-keepattributes{attribute_name,...}
#设置源文件中给定的字符串常量
-renamesourcefileattribute{string}



参考文档:

你可能感兴趣的:(ProGuard)