当前位置:操作系统 > 安卓/Android >>

android Application的防反编译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 * 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 易做图yze correctly and it might remove code that it thinks is not used, but your application actually needs. Some examples include:

a class that is referenced only in the 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 <MyClass>
 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

 
3.App签名
签名主要用到的是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 my_signed.apk
如果出现的是CN=Android Debug,说明是调试密钥生成签名的apk

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

 

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,