android代码混淆和常见问题总结

转自:http://blog.csdn.net/Zengyangtech/article/details/6127600


由于各种反编译工具的泛滥,作为Android程序员在2.3版本以前只能通过手动添加proguard来实现代码混淆

 

proguard这个工具是一个java代码混淆的工具

 

在2.3版本的sdk中 我们可以看到在android-sdk-windows/tools/下面多了一个proguard文件夹

google已经把proguard技术放在了android sdk里面 可以通过正常的编译方式也能实现代码混淆了

 

可以看见新建一个工程里面有default.properties和proguard.cfg

 

默认的default.properties代码如下

[c-sharp] view plain copy
  1. # This file is automatically generated by Android Tools.  
  2. # Do not modify this file -- YOUR CHANGES WILL BE ERASED!  
  3. #  
  4. # This file must be checked in Version Control Systems.  
  5. #  
  6. # To customize properties used by the Ant build system use,  
  7. # "build.properties", and override values to adapt the script to your  
  8. # project structure.  
  9. # Project target.  
  10. target=android-9  
 

我们可以看到proguard.cfg已经帮我们写好了优化代码脚本

[c-sharp] view plain copy
  1. -optimizationpasses 5  
  2. -dontusemixedcaseclassnames  
  3. -dontskipnonpubliclibraryclasses  
  4. -dontpreverify  
  5. -verbose  
  6. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  
  7. -keep public class * extends android.app.Activity  
  8. -keep public class * extends android.app.Application  
  9. -keep public class * extends android.app.Service  
  10. -keep public class * extends android.content.BroadcastReceiver  
  11. -keep public class * extends android.content.ContentProvider  
  12. -keep public class com.android.vending.licensing.ILicensingService  
  13. -keepclasseswithmembernames class * {  
  14.     native <methods>;  
  15. }  
  16. -keepclasseswithmembernames class * {  
  17.     public <init>(android.content.Context, android.util.AttributeSet);  
  18. }  
  19. -keepclasseswithmembernames class * {  
  20.     public <init>(android.content.Context, android.util.AttributeSet, int);  
  21. }  
  22. -keepclassmembers enum * {  
  23.     public static **[] values();  
  24.     public static ** valueOf(java.lang.String);  
  25. }  
  26. -keep class * implements android.os.Parcelable {  
  27.   public static final android.os.Parcelable$Creator *;  
  28. }  
 

从脚本中可以看到,混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService

 

并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等。(详细信息请参考<proguard_path>/examples中的例子及注释。)

 

接下来 按照google帮助文档里说的

[c-sharp] view plain copy
  1. To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the proguard.config property in the <project_root>/default.properties file. The path can be an absolute path or a path relative to the project's root.  
 

所以我们修改default.properties file

加上一句

proguard.config=proguard.cfg

如下

[c-sharp] view plain copy
  1. # This file is automatically generated by Android Tools.  
  2. # Do not modify this file -- YOUR CHANGES WILL BE ERASED!  
  3. #  
  4. # This file must be checked in Version Control Systems.  
  5. #  
  6. # To customize properties used by the Ant build system use,  
  7. # "build.properties", and override values to adapt the script to your  
  8. # project structure.  
  9. # Project target.  
  10. target=android-9  
  11. proguard.config=proguard.cfg  
 

 

然后正常的编译签名即可

 

然后用Android Tools生成一个发布的apk即可

 

然后用反编译工具查看dex文件

最后导出反编译之后的混淆代码如下图

 

 

 

是不是很轻松加愉快!希望各位程序员都能保护好自己的Android代码!


常见问题:

问题 1:

R类反射混淆,找不到资源ID

转自:http://liangzg.iteye.com/blog/1311718

R类反射提取属性后,对项目混淆,R类下的资源ID也会被混淆,这时当我再通过未混淆前提取的属性来匹配资源ID,后果当然就是找不资源,程序报错,一切Over!

 

这里提供一种解决方案,是在本人最近一个网游社区游戏项目中使用的。

 

简单提下混淆:

在Android的项目下有一个default.properties文件,项目默认是不开启混淆,如需要开启混淆只需要添加

以下设定:

  1. proguard.config = proguard.cfg  

 

解决方案参考:

在Proguard.cfg方件中添加以下设定

  1. -keep class **.R$* {  
  2. *;  
  3. }  

 

 

如果大伙还有其它更好的方法,喜欢贴出来,一起交流

如果上面的不好用,可以使用下面这个。

通过了反射机制得到资源引用文件R.java,但是在开发者通过proguard等混淆/优化工具处理apk时,proguard可能会将R.java删除,如果遇到这个问题,请在proguard配置文件中添加keep命令如:

1
2
3
4
-keep public class [package name].R$*{
     public static final int *;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值