android APK防止反编译:
在android2.3之后的版本新建项目中会自动生成proguard.cfg和project.properties文件,proguard.cfg文件是混淆java代码的配置文件,里面对不需要混淆代码的类文件进行配置过滤,project.properties文件里设置android项目对应的版本和proguard.cfg的路径。
1.贴上自动生成的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 * 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);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
<-----------系统默认proguard.cfg说明--------------------->
For some situations, the default configurations in the proguard.cfg
file will suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove code that it thinks is not used, but your application actually needs. Some examples include:
AndroidManifest.xml
file(AndroidManifest.xml文件中的引用类)从上面的配置代码可以看出对Activity,Application,Service,BroadcastReceiver,ContentProvider,BackupAgentHelper,Preference,ILicensingService的子类不做代码混淆处理(这些类可能被其他应用或系统应用调用)。混淆之后应用出现如ClassNotFoundException异常,可以在此文件中添加过滤混淆代码:
-keep public class <MyClass>
Android Developers官方资料:http://developer.android.com/tools/help/proguard.html
2.在project.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 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-10
文件最后添加proguard.config=proguard.cfg(混淆配置路径),proguard.cfg也可以放到别的路径,比如放到工程根目录下的path/to/proguard.cfg,这时应设置对应的绝对路径proguard.config=/path/to/proguard.cfg,具体相关配置参考Android Developers官方文档资料:http://developer.android.com/tools/help/proguard.html
3.App签名(Android Developers官方文档资料:http://developer.android.com/tools/publishing/app-signing.html#setup)
签名主要用到的是JDK中提供的签名工具keytool(路径:your jdk path/bin/keytool)
keytool生成key.keystore签名文件的命令参数
Keytool Option | Description |
---|---|
-genkey |
获得密钥对(私有密钥和公共密钥) |
-v |
启用详细输出 |
-alias <alias_name> |
别名 |
-keyalg <alg> |
使用生成密钥的加密算法 |
-keysize <size> |
密钥长度 |
-dname <name> |
创建密钥的描述 |
-keypass <password> |
密钥密码 |
-validity <valdays> |
密钥有效期 Note:推荐使用大于等于10000 |
-keystore <keystore-name>.keystore |
输出生成密钥的.keystore文件保存路径 |
-storepass <password> |
密钥库密码,与-keypass对应 |
keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000执行过程中有一些提示输入信息,输入完成后.keystore文件生成完毕。
4.导出混淆的签名应用
右击项目:Android Tools--------------------->Export sined application package
选择上一步生成的签名文件(.keystore)和输入密钥库密码,点击下一步,选择填写Alias别名和密钥密码----->导出APK对应的路径-------->finish
到处截屏以后贴上。
查看验证APK签名:同样是JDK工具jarsigner
jarsigner -verify -verbose -certs my_signed.apk
如果出现的是CN=Android Debug,说明是调试密钥生成签名的apk
这样一个混淆代码后的签名APK制作完毕。
Android技术开发讨论群:113713443,欢迎喜欢Android,学习Android的朋友加入。