android4.0
昨天一下午都在搞关于怎样做android代码混淆的问题,其中遇到了不少的问题,关于我是怎样去做代码混淆的拿来与大家分享,还有也会从其他人的博客中真理一些信息过来,作为总结作用。
1、在android4.0版本中,代码混淆的设置会更简单,打开project.properties文件,会看到内容偏下方位置
有这段代码
# 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
根据上面一行的英文翻译,就知道只要将下面的一行解注释就可以实现代码混淆,在一些博客中也看到说这句话是不带优化功能的,
proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
这样写可以,本人没有试过,待验证。
2、只要注释就可以了吗?答案当然是否定的,知识之后我们可以尝试导出签名的apk,这时就可能会导出失败,可以具体查看一下导出失败的原因,网上百度或谷歌一下看看,而我建议先不忙着导出apk查看错误,而是先配置proguard-project.txt文件,在这里面将源码中的一部分代码不做混淆。
# 指定代码的压缩级别 -optimizationpasses 5 -dontusemixedcaseclassnames # 是否混淆第三方jar -dontskipnonpubliclibraryclasses -dontpreverify -keepattributes SourceFile,LineNumberTable -verbose -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* #根据自己应用的情况而定,第三方jar包用多少写多少 -libraryjars libs/acra-4.5.0.jar -libraryjars libs/activation.jar -libraryjars libs/additionnal.jar -libraryjars libs/android-support-v4.jar -libraryjars libs/gson-2.2.4.jar -libraryjars libs/mail.jar
-ignorewarnings #ACRA specifics # we need line numbers in our stack traces otherwise they are pretty useless -renamesourcefileattribute SourceFile -keepattributes SourceFile,LineNumberTable # ACRA needs "annotations" so add this... -keepattributes *Annotation* #忽略其中关于继承系统相关的类,这些是绝对不可以做混淆的 -keep public class * extends android.app.Fragment -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 * extends android.support.v4.** -keep public class com.google.vending.licensing.ILicensingService -keep public class com.android.vending.licensing.ILicensingService
#应用的model类是不推荐做混淆,否则会在数据传输与解析中出错 -keep public class com.huabo.launcher.model.*
#关于引用的第三方jar包,这里需要注意,有些不值仅仅使用<span style="font-family: Arial;">-libraryjars就可以解决的</span> -keep class org.acra.** { *; }
#测试中遇到问题,gson解析会报错,需加上这几句
-keepattributes Signature -keep class com.google.gson.stream.** { *; } -keep class com.google.gson.examples.android.model.** { *; }
#以下内容来自引用 的sdk,<span style="font-family: Arial;">tools/proguard/proguard-android.txt的内容,可以复制过来使用</span> # Optimization is turned off by default. Dex does not like code run # through the ProGuard optimize and preverify steps (and performs some # of these optimizations on its own). -dontoptimize # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames class * { native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View { void set*(***); *** get*(); } # We want to keep methods in Activity that could be used in the XML attribute onClick -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -keepclassmembers class **.R$* { public static <fields>; } -keepclassmembers class mypackage.** { *; } # The support library contains references to newer platform versions. # Don't warn about those in case this app is linking against an older # platform version. We know about them, and they are safe. -dontwarn android.support.**
3、补充
百度地图混淆
-libraryjars libs/baidumapapi_v2_1_3.jar -keep class com.baidu.** { *; } -keep class vi.com.gdi.bgl.android.**{*;}
微信混淆
-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*; } -keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*; }
微博混淆:
-dontwarn com.weibo.sdk.android.WeiboDialog -dontwarn android.net.http.SslError -dontwarn android.webkit.WebViewClient -keep public class android.net.http.SslError{ *; } -keep public class android.webkit.WebViewClient{ *; } -keep public class android.webkit.WebChromeClient{ *; } -keep public interface android.webkit.WebChromeClient$CustomViewCallback { *; } -keep public interface android.webkit.ValueCallback { *; } -keep class * implements android.webkit.WebChromeClient { *; }
actionbarsherlock:
-keep class android.support.v4.app.** { *; } -keep interface android.support.v4.app.** { *; } -keep class com.actionbarsherlock.** { *; } -keep interface com.actionbarsherlock.** { *; }
关于后面加的这几个,本人没有测试过,如果您在使用过程中遇到什么问题,还希望能够纠正我的这篇博客,这样我可以即使修改,以便大家共同学习。
4、我们在混淆成功后,编译apk若运行出现问题,建议调试时将提示出错的代码注释掉,在进行尝试,确认问题后,配置那部分的代码不混淆即可。当然也不要盲目的这么做,需要具体分析分析在确认问题是否真的出在那部分代码上面,还是漏配置了什么东东!
最后愿每个努力的程序猿先生都有一个美好的未来!