android 混淆机制,project.properties,proguard-project.txt或者旧版proguard.cfg

时间:2022-08-24 04:38:58

首先记住几个问题,

一、为什么要加入混淆机制?

     防止Apk被反编译后,很容易被其他开发者看懂,

    混淆机制的本质是什么?

    把原来有具体含义的类名,变量名,方法名,修改成让人看不懂的名字,例如方法名getUserName(),混淆后方法名变更为a或者b或者其他。

二、如何混淆代码?
 Android创建项目工程时会生成两个文件,project.properties,proguard-project.txt。旧版本是proguard.cfg

1,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-14

# 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
其实就是把前面的#号取消就可以了,当你取消这个注释后,此时签名打包的时候已经对你的项目进行了全局混淆,注意这里的说明,是全局混淆。

如果你的项目没有具体的要求可以按照上面的去完成,但是如果你的项目使用了第三方的jar包,或者library之类引用,或者一些实体类,gson解释之类的,

那么你此时签名一定会出现错误,

因为,此行指定了混淆代码的配置文件,是/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android.txt。如果想优化你的代码,配置文件换成/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android-optimize.txt。
所以此时把代码替换为下面这句

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
下面这个是proguard-project.txt文件内的内容,

如果有一些代码不能被混淆(这就是全局混淆后,签名打包失败的原因),比如你的项目里面添加了so文件,或者集成了淘宝广告,微信分享,qq登录之类的,需要调用里面的方法,调用JNI访问so文件的方法或者qq登录的方法,再导出apk时可能会报错,也可能不会报错,但是在运行apk的时候一定会出现错误,这个时候就需要到proguard-project.txt这个文件了。

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}
举个简单的例子,我的项目集成了qq登录和微信分享功能,按照微信和qq的开放平台内的集成文档添加混淆代码就可以了。

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}
##--这里是qq登录
-keep class com.tencent.open.TDialog$*
-keep class com.tencent.open.TDialog$* {*;}
-keep class com.tencent.open.PKDialog
-keep class com.tencent.open.PKDialog {*;}
-keep class com.tencent.open.PKDialog$*
-keep class com.tencent.open.PKDialog$* {*;}

##--这里是微信分享和微信朋友圈
-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}

这里添加的意思大致可以理解成: 保护指定的类文件和类的成员

当然还有更多的方法可以参照这个链接点击打开链接