关于ProGuard代码混淆

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

在2.3版本的sdk中 我们可以看到在android-sdk-windows/tools/下面多了一个proguard文件夹,google已经把proguard技术放在了android sdk里面 可以通过正常的编译方式也能实现代码混淆了。

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

默认的default.properties代码如下

# 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 use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9

我们可以看到proguard.cfg已经帮我们写好了优化代码脚本
-optimizationpasses 5
-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 com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}


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

并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等。


所以我们修改default.properties file
proguard.config=proguard.cfg

如下
# 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 use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9
proguard.config=proguard.cfg


正常编译签名即可

主要摘自http://www.pin5i.com/showtopic-android-2.3-code-obfuscation-proguard.html


猜你喜欢

转载自luozheng216.iteye.com/blog/1609309