Android_混淆_常用规则(2)

1,Proguard手册
很多人不知道,Proguard其实是有官方手册的,而且就在Studio的SDK目录下,我的位置:
     D:\study_adt_studio\sdk\tools\proguard\docs

初步打开index.html --> Manual --> Uasge(如图)



2,常用混淆规则(原文+注释)

考虑到手册比较多,因此搬移了一些常用的贴在blog里。具体可打开开发手册自己查找

1) 诠释:Ignoring warnings,忽略警告;

-dontwarn [class_filter]
        Specifies not to warn about unresolved references and other important problems at all. 
The optional filter is a regular expression; ProGuard doesn't print warnings about classes with 
matching names. Ignoring warnings can be dangerous. For instance, if the unresolved classes 
or class members are indeed required for processing, the processed code will not function properly.
Only use this option if you know what you're doing!

2)诠释:保持,类和类的成员,不被删除和重命名

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

3)诠释:保持,类和类的成员,不被重命名

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

4)诠释:保持,类的成员,不被删除和重命名

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

5)诠释:保持,类的成员,不被重命名

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

6)诠释:保持,类和类的成员(指定的成员存在),不被删除和重命名

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

7)诠释:保持,类和类的成员(指定的成员存在),不被重命名

-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.
Key的总结有表格
Keep总结From being removed or renamed
(不被删除和重命名)
From being renamed
(不被重命名)
Classes and class members
(类和类的成员)
-keep-keepnames
Class members only
(类的成员)
-keepclassmembers-keepclassmembernames
Classes and class members, if class members present
(类和类的成员,若成员都存在)
-keepclasseswithmembers-keepclasseswithmembernames
PS:第二栏尽量别用,原因:只有被直接引用的,才会被系统认为不可删除。例如:set get方法,get默认方法会被删除

8)诠释:保持,某一类成员变量,不被混淆

-keepattributes [attribute_filter]
    Specifies any optional attributes to be preserved. The attributes can be specified with one or 
more -keepattributes directives. The optional filter is a comma-separated list of attribute names. 
Attribute names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator. 
Typical optional attributes are Exceptions, Signature, Deprecated, SourceFile, SourceDir, LineNumberTable,
LocalVariableTable, LocalVariableTypeTable, Synthetic, EnclosingMethod, RuntimeVisibleAnnotations, 
RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, 
and AnnotationDefault. The InnerClasses attribute name can be specified as well, referring to the 
source name part of this attribute. For example, you should at least keep the Exceptions, InnerClasses, 
and Signature attributes when processing a library. You should also keep the SourceFile and LineNumberTable
attributes for producing useful obfuscated stack traces. Finally, you may want to keep annotations 
if your code depends on them. Only applicable when obfuscating.


3  混淆通配符匹配规则

使用于类的部分:
*      匹配类名中的任何部分,但不包含额外的包名
**     匹配类名中的任何部分,并且可以包含额外的包名
extends   匹配继承某个基类的类
implement 匹配实现某个接口的类
$     指内部类 

使用于成员的部分:
<init>    匹配任何构造器
<fields>   匹配任何字段名
<methods>  匹配任何方法
*      匹配任意长度字符,但不包含,分隔符'.'
**     匹配任意长度字符,并且包含,分隔符'.'
***    匹配任意参数类型
%      匹配任何基础类型的类型名
...    匹配任意长度的任意类型的参数

4,常用混淆规则案例

1)不混淆某个类

-keep class com.yline.MyClass
-keep class com.yline.MyClass{
*;
};

2)不混淆某个包下所有的类

-keep class com.lippi.recorder.utils.** {
    *;
}

3)不混淆某个类的所有子类

-keep public class * extends android.app.Activity { 
   *; 
}

4)不混淆某个接口的实现

#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#保持 Serializable 不被混淆
-keep class * implements java.io.Serializable{ public *;}
5)不混淆 native方法
#不混淆 native 方法不被混淆
-keepclasseswithmembernames class * {
     native <methods>;
}

6)不混淆自定义View

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

7)不混淆枚举类型

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

以上是介绍混淆的规则,而具体如何书写Proguard文件,下一篇:常用混淆(3)








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值