proguard是一个java代码混淆的工具,通过proguard,别人即使反编译你的apk包,也只会看到一些让人很难看懂的代码,从而达到保护代码的作用。 下面具体说一说怎么样让SDK2.3下的proguard.cfg文件起作用,先来看看android-sdk-windows\tools\lib\proguard.cfg的内容: [size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
| [size=1em][size=1em]-optimizationpasses 5
[size=1em]-dontusemixedcaseclassnames
[size=1em]-dontskipnonpubliclibraryclasses
[size=1em]-dontpreverify
[size=1em]-verbose
[size=1em]-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
[size=1em]-keep public class * extends android.app.Activity
[size=1em]-keep public class * extends android.app.Application
[size=1em]-keep public class * extends android.app.Service
[size=1em]-keep public class * extends android.content.BroadcastReceiver
[size=1em]-keep public class * extends android.content.ContentProvider
[size=1em]-keep public class * extends android.app.backup.BackupAgentHelper
[size=1em]-keep public class * extends android.preference.Preference
[size=1em]-keep public class com.android.vending.licensing.ILicensingService
[size=1em]-keepclasseswithmembernames class * {
[size=1em] native <methods>;
[size=1em]}
[size=1em]-keepclasseswithmembernames class * {
[size=1em] public <init>(android.content.Context, android.util.AttributeSet);
[size=1em]}
[size=1em]-keepclasseswithmembernames class * {
[size=1em] public <init>(android.content.Context, android.util.AttributeSet, int);
[size=1em]}
[size=1em]-keepclassmembers enum * {
[size=1em] public static **[] values();
[size=1em] public static ** valueOf(java.lang.String);
[size=1em]}
[size=1em]-keep class * implements android.os.Parcelable {
[size=1em] public static final android.os.Parcelable$Creator *;
[size=1em]}
|
从脚本中可以看到,混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、 ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService, 并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等。(详细信息请参考<proguard_path>/examples中的例子及注释。) 让proguard.cfg起作用的做法很简单,就是在eclipse自动生成的default.properties文件中加上一句“proguard.config=proguard.cfg”就可以了 完整的default.properties文件应该如下: [size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
| [size=1em][size=1em]# This file is automatically generated by Android Tools.
[size=1em]# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
[size=1em]#
[size=1em]# This file must be checked in Version Control Systems.
[size=1em]#
[size=1em]# To customize properties used by the Ant build system use,
[size=1em]# "build.properties", and override values to adapt the script to your
[size=1em]# project structure.
[size=1em]# Project target.
[size=1em]target=android-9
[size=1em]proguard.config=proguard.cfg
|
大功告成,正常的编译签名后就可以防止代码被反编译了。反编译经过代码混淆的apk得到的代码应该类似于下面的效果,是很难看懂的:
如果您使用的是2.3之前的SDK版本也没关系,把上面的proguard.cfg文件复制一份放到项目中,然后进行相同的操作即可
|