android Application的防反编…

http://www.myexception.cn/android/1061001.html

androidApplication的防反编译Proguard和应用签名

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 * extendsandroid.content.BroadcastReceiver
-keep public class * extendsandroid.content.ContentProvider
-keep public class * extendsandroid.app.backup.BackupAgentHelper
-keep public class * extendsandroid.preference.Preference
-keep public classcom.android.vending.licensing.ILicensingService


-keepclasseswithmembernames class * {
    native ;
}


-keepclasseswithmembers class * {
    public(android.content.Context, android.util.AttributeSet);
}


-keepclasseswithmembers class * {
    public(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 finalandroid.os.Parcelable$Creator *;
}

<-----------系统默认proguard.cfg说明--------------------->

 

For some situations, the default configurations inthe proguard.cfg filewill suffice. However, many situations are hard for ProGuard toanalyze correctly and it might remove code that it thinks is notused, but your application actually needs. Some examplesinclude:

  • a class that is referenced only inthe AndroidManifest.xml file(AndroidManifest.xml文件中的引用类
  • a method called from JNI(JNI调用的方法)
  • dynamically referenced fields and methods(动态引用的字段和方法)
<-----------系统默认 proguard.cfg说明--------------------- >

从上面的配置代码可以看出对Activity,Application,Service,BroadcastReceiver,ContentProvider,BackupAgentHelper,Preference,ILicensingService的子类不做代码混淆处理(这些类可能被其他应用或系统应用调用)。混淆之后应用出现如ClassNotFoundException异常,可以在此文件中添加过滤混淆代码:

-keep public class <</span>MyClass>

 

AndroidDevelopers官方资料: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 systemedit
# "ant.properties", and override values to adapt the script toyour
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncommentthis (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,具体相关配置参考AndroidDevelopers官方文档资料:http://developer.android.com/tools/help/proguard.html


3.App签名(AndroidDevelopers官方文档资料: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 别名
-keyalg 使用生成密钥的加密算法
-keysize 密钥长度
-dname

创建密钥的描述

-keypass

密钥密码

-validity

密钥有效期

Note:推荐使用大于等于10000

-keystore .keystore 输出生成密钥的.keystore文件保存路径
-storepass

密钥库密码,与-keypass对应

有了以上命令参数,下面执行命令进行应用签名(以下为例):

 

 

keytool -genkey -v -keystore my-release-key.keystore
-alias alias_name -keyalg RSA -keysize 2048 -validity 10000
执行过程中有一些提示输入信息,输入完成后.keystore文件生成完毕。

 


4.导出混淆的签名应用

右击项目:Android Tools--------------------->Export sined applicationpackage

选择上一步生成的签名文件(.keystore)和输入密钥库密码,点击下一步,选择填写Alias别名和密钥密码----->导出APK对应的路径-------->finish

到处截屏以后贴上。

查看验证APK签名:同样是JDK工具jarsigner

 
   
jarsigner -verify -verbose -certs my_signed.apk

 

如果出现的是CN=AndroidDebug,说明是调试密钥生成签名的apk

这样一个混淆代码后的签名APK制作完毕。

你可能感兴趣的:(android Application的防反编…)