一、概述:
android发布签名包之前,混淆编译是必须的。由于我的sdk版本较高,因此新建android项目下有proguard-project.txt和project.properties这两个文件夹。
以下是一些混淆总结:
1、如果你的项目没有其他第三方包的话,那么进行混淆很简单,只需要将project.properties文件夹下面的注释解开就行,一点区别在于:如果你是2.3之前的sdk版本,那么就用这个proguard.config=proguard.cfg
如果是之后的则为:proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt。
2、如果有第三方包的话,混淆时需要注意。
二、混淆代码:
我的项目里面proguard-android.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:
# 指明lib包的在工程中的路径 否则导出包的时候会有这样的错误 You may need to specify additional library jars (using '-libraryjars').
-libraryjars libs/achartengine-1.1.0.jar
-libraryjars libs/android-support-v4.jar
-libraryjars libs/asynchttp.jar
-libraryjars libs/pinyin4j-2.5.0.jar
# 去除在控制台中报warning警告
-ignorewarnings
-optimizationpasses 4
-dontusemixedcaseclassnames
-verbose
-dontwarn demo.**
-keep class demo.** { *;}
# 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 *;
#}
-dontwarn com.xx.bbb.**
和-keep class com.xx.bbb.** { ;}
这两个参数用来保持第三方库中的类而不乱,
将-dontwarn和-keep 结合使用,意思是保持com.xx.bbb.*这个包里面的所有类和所有方法而不混淆,
接着还叫proguard不要警告找不到com.xx.bbb.**这个包里面的类的相关引用。
三、问题的解决:
打签名包失败同时在查看控制台的时候也会发现 “You may need to specify additional library jars (using ‘-libraryjars’).” 的报错提示:
出现此错误提示是因为使用了 proguard 代码混淆器,同时在项目中引用了第三方 jar 包,但却没有在proguard 的配置文件中标明引用了的 jar 包所致。
解决方法如下:
在项目的目录下找到 proguard-project.txt 文件,将其打开后在任一空行中使用 -libraryjars 标明所有引用的第三方 jar 包。如下所示:
-libraryjars libs/achartengine-1.1.0.jar
-libraryjars libs/android-support-v4.jar
-libraryjars libs/asynchttp.jar
-libraryjars libs/pinyin4j-2.5.0.jar
四、补充:
为了apk的安全起见,还可以再用一些加固软件进行加固。