Android混淆打包

ProGuard是一个SourceForge上非常知名的开源项目。官网网址是:http://proguard.sourceforge.net/。

Java的字节码一般是非常容易反编译的。为了很好的保护Java源代码,我们往往会对编译好的class文件进行混淆处理。ProGuard的主要作用就是混淆。当然它还能对字节码进行缩减体积、优化等,但那些对于我们来说都算是次要的功能。

Android Eclipse开发环境与ProGuard

在Android 2.3以前,混淆Android代码只能手动添加proguard来实现代码混淆,非常不方便。而2.3以后,Google已经将这个工具加入到了SDK的工具集里。具体路径:SDK\tools\proguard。当创建一个新的Android工程时,在工程目录的根路径下,会出现一个proguard的配置文件proguard.cfg。也就是说,我们可以通过简单的配置,在我们的elipse工程中直接使用ProGuard混淆Android工程。 集成的ADT现在创建一个新的Android工程时,在工程目录的根路径下、会有一个proguard-project.txt。其实这个文件跟proguard.cfg是一样的。混淆打包的一些配置写在这个文件里就行了。

搜了下Proguard混淆打包相关信息,搜来搜去都说的都大同小异,只要懂得了基本的一些使用方法,哪些混淆,哪些不混淆,最常见的就是过滤掉一些android需要注册的一些组件不混淆,第三方包也不需要混淆,因为有的第三方包已经混淆过了,大致的按照自己需要混淆的需求,写个基本配置就行了。在这里我做了个基本的综合、proguard配置如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-optimizationpasses 7
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,! class /merging/*
  
  
-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
  
  
-keepclasseswithmembernames class * {
     native <METHODS>;
}
  
-keepclasseswithmembers class * {
     public <INIT>(android.content.Context, android.util.AttributeSet);
}
  
-keepclasseswithmembers class * {
     public <INIT>(android.content.Context, android.util.AttributeSet, int );
}
  
-keepclassmembers class * extends android.app.Activity { 
     public void *(android.view.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*(...);
}
  
-keepclassmembers enum * {
     public static **[] values();
     public static ** valueOf(java.lang.String);
}
  
-keep class * implements android.os.Parcelable {
     public static final android.os.Parcelable$Creator *;
}
  
-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 void writeObject(java.io.ObjectOutputStream);
     private void readObject(java.io.ObjectInputStream);
     java.lang.Object writeReplace();
     java.lang.Object readResolve();
}
  
-keepattributes Signature
  
-keepattributes *Annotation*
  
-keep class **.R$* { *; }
  
  
-libraryjars  libs/android-support-v4.jar
-dontwarn android.support.v4.**    
-keep class android.support.v4.** { *; }  
-keep interface android.support.v4.** { *; }
-keep public class * extends android.support.v4.** 
-keep public class * extends android.app.Fragment


它主要保留了继承自Activity、Application、Service、BroadcastReceiver、ContentProvider、BackupAgentHelper、Preference和ILicensingService的子类。因为这些子类,都是可能被外部调用的。

另外,它还保留了含有native方法的类、构造函数从xml构造的类(一般为View的子类)、枚举类型中的values和valueOf静态方法、继承Parcelable的跨进程数据类和实现Serializable对象序列化。

若有其他第三方包,可依依添加不混淆配置就行了。可根据要求,去个性配置proguard混淆就行了。


最后记住把根目录路径下的project.properties文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties" , and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
  
# Project target.
target=android- 17


proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

红色的那句注释(去掉“#”号)的打开放在最下面,就可以签名混淆打包apk去了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值