做为Android应用开发者,不得不面对一个尴尬的局面,就是本身辛辛苦苦开发的应用能够被别人很轻易的就反编译出来。 java
Google彷佛也发现了这个问题,从SDK2.3开始咱们能够看到在android-sdk-windows\tools\下面多了一个proguard文件夹 android
proguard是一个java代码混淆的工具,经过proguard,别人即便反编译你的apk包,也只会看到一些让人很难看懂的代码,从而达到保护代码的做用。 windows
下面具体说一说怎么样让SDK2.3下的proguard.cfg文件起做用,先来看看android-sdk-windows\tools\lib\proguard.cfg的内容: app
02 |
-dontusemixedcaseclassnames |
03 |
-dontskipnonpubliclibraryclasses |
06 |
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* |
08 |
-keep public class * extends android.app.Activity |
09 |
-keep public class * extends android.app.Application |
10 |
-keep public class * extends android.app.Service |
11 |
-keep public class * extends android.content.BroadcastReceiver |
12 |
-keep public class * extends android.content.ContentProvider |
13 |
-keep public class * extends android.app.backup.BackupAgentHelper |
14 |
-keep public class * extends android.preference.Preference |
15 |
-keep public class com.android.vending.licensing.ILicensingService |
17 |
-keepclasseswithmembernames class * { |
21 |
-keepclasseswithmembernames class * { |
22 |
public <init>(android.content.Context, android.util.AttributeSet); |
25 |
-keepclasseswithmembernames class * { |
26 |
public <init>(android.content.Context, android.util.AttributeSet, int); |
29 |
-keepclassmembers enum * { |
30 |
public static **[] values(); |
31 |
public static ** valueOf(java.lang.String); |
34 |
-keep class * implements android.os.Parcelable { |
35 |
public static final android.os.Parcelable$Creator *; |
从脚本中能够看到,混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、 ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService, eclipse
并保留了全部的Native变量名及类名,全部类中部分以设定了固定参数格式的构造函数,枚举等等。(详细信息请参考<proguard_path>/examples中的例子及注释。) ide
让proguard.cfg起做用的作法很简单,就是在eclipse自动生成的default.properties文件中加上一句“proguard.config=proguard.cfg”就能够了 函数
完整的default.properties文件应该以下: 工具
01 |
# This file is automatically generated by Android Tools. |
02 |
# Do not modify this file -- YOUR CHANGES WILL BE ERASED! |
04 |
# This file must be checked in Version Control Systems. |
06 |
# To customize properties used by the Ant build system use, |
07 |
# "build.properties", and override values to adapt the script to your |
12 |
proguard.config=proguard.cfg |
大功告成,正常的编译签名后就能够防止代码被反编译了。反编译通过代码混淆的apk获得的代码应该相似于下面的效果,是很难看懂的:
若是您使用的是2.3以前的SDK版本也不要紧,把上面的proguard.cfg文件复制一份放到项目中,而后进行相同的操做便可 ui