Android 混淆代码详解及实例
为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤:
1.大家也许都注意到新建一个工程会看到项目下边有这样proguard-project.txt一个文件,这个对混淆代码很重要,如果你不小心删掉了,没关系,从其他地方拷贝一个过来
2.最重要的就是在proguard-project.txt添加混淆的申明了:
a.把所有你的jar包都申明进来,例如:
-libraryjarslibs/apns_1.0.6.jar
-libraryjarslibs/armeabi/libBaiduMapSDK_v2_3_1.so
-libraryjarslibs/armeabi/liblocSDK4.so
-libraryjarslibs/baidumapapi_v2_3_1.jar
-libraryjarslibs/core.jar
-libraryjarslibs/gesture-imageview.jar
-libraryjarslibs/gson-2.0.jar
-libraryjarslibs/infogracesound.jar
-libraryjarslibs/locSDK_4.0.jar
-libraryjarslibs/ormlite-android-4.48.jar
-libraryjarslibs/ormlite-core-4.48.jar
-libraryjarslibs/universal-image-loader-1.9.0.jar
b.将你不需要混淆的部分申明进来,因为有些类经过混淆会导致程序编译不通过,如下:
-keeppublicclass*extendsandroid.app.Fragment
-keeppublicclass*extendsandroid.app.Activity
-keeppublicclass*extendsandroid.app.Application
-keeppublicclass*extendsandroid.app.Service
-keeppublicclass*extendsandroid.content.BroadcastReceiver
-keeppublicclass*extendsandroid.content.ContentProvider
-keeppublicclass*extendsandroid.app.backup.BackupAgentHelper
-keeppublicclass*extendsandroid.preference.Preference
-keeppublicclass*extendsandroid.support.v4.**
-keeppublicclasscom.android.vending.licensing.ILicensingService
--以上都是API里边的类,最好都要避免混淆
有些很特殊的,例如百度地图,你需要添加以下申明:
-keepclasscom.baidu.**{*;}
-keepclassvi.com.gdi.bgl.android.**{*;}
根据我的经验,一般model最好避免混淆(model无关紧要,不混淆也没多大关系)如:
-keepclasscom.bank.pingan.model.**{*;}
下面在贴上关于Umeng分享统计的避免混淆的申明
-dontwarnandroid.support.v4.**
-dontwarnorg.apache.commons.net.**
-dontwarncom.tencent.**
-keepclasseswithmembernamesclass*{
native<methods>;
}
-keepclasseswithmembernamesclass*{
public<init>(android.content.Context,android.util.AttributeSet);
}
-keepclasseswithmembernamesclass*{
public<init>(android.content.Context,android.util.AttributeSet,int);
}
-keepclassmembersenum*{
publicstatic**[]values();
publicstatic**valueOf(java.lang.String);
}
-keepclass*implementsandroid.os.Parcelable{
publicstaticfinalandroid.os.Parcelable$Creator*;
}
-keepclasseswithmembersclass*{
public<init>(android.content.Context);
}
-dontshrink
-dontoptimize
-dontwarncom.google.android.maps.**
-dontwarnandroid.webkit.WebView
-dontwarncom.umeng.**
-dontwarncom.tencent.weibo.sdk.**
-dontwarncom.facebook.**
-keepenumcom.facebook.**
-keepattributesExceptions,InnerClasses,Signature
-keepattributes*Annotation*
-keepattributesSourceFile,LineNumberTable
-keeppublicinterfacecom.facebook.**
-keeppublicinterfacecom.tencent.**
-keeppublicinterfacecom.umeng.socialize.**
-keeppublicinterfacecom.umeng.socialize.sensor.**
-keeppublicinterfacecom.umeng.scrshot.**
-keeppublicclasscom.umeng.socialize.*{*;}
-keeppublicclassjavax.**
-keeppublicclassandroid.webkit.**
-keepclasscom.facebook.**
-keepclasscom.umeng.scrshot.**
-keeppublicclasscom.tencent.**{*;}
-keepclasscom.umeng.socialize.sensor.**
-keepclasscom.tencent.mm.sdk.openapi.WXMediaMessage{*;}
-keepclasscom.tencent.mm.sdk.openapi.**implementscom.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject{*;}
-keepclassim.yixin.sdk.api.YXMessage{*;}
-keepclassim.yixin.sdk.api.**implementsim.yixin.sdk.api.YXMessage$YXMessageData{*;}
-keeppublicclass[your_pkg].R$*{
publicstaticfinalint*;
}
3.以上工作完成,混淆工作就完成了一大半了,最后需要做的就是在project.properties文件中加上你的混淆文件申明了,如下:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
4.OK,最后一步,打签名包测试,如果有问题,仔细看下Log也许有得类不能混淆,那么你得加入到proguard-project.txt文件中
-------以上就是混淆代码的全过程了
最后贴上proguard-project.txt的全部代码:
#ToenableProGuardinyourproject,editproject.properties #todefinetheproguard.configpropertyasdescribedinthatfile. # #AddprojectspecificProGuardruleshere. #Bydefault,theflagsinthisfileareappendedtoflagsspecified #in${sdk.dir}/tools/proguard/proguard-android.txt #YoucanedittheincludepathandorderbychangingtheProGuard #includepropertyinproject.properties. # #Formoredetails,see #http://developer.android.com/guide/developing/tools/proguard.html #Addanyprojectspecifickeepoptionshere: #IfyourprojectusesWebViewwithJS,uncommentthefollowing #andspecifythefullyqualifiedclassnametotheJavaScriptinterface #class: #-keepclassmembersclassfqcn.of.javascript.interface.for.webview{ #public*; #} -optimizationpasses5 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontpreverify -verbose -optimizations!code/simplification/arithmetic,!field/*,!class/merging/* -keepattributes*Annotation* -keepattributesSignature -libraryjarslibs/apns_1.0.6.jar -libraryjarslibs/armeabi/libBaiduMapSDK_v2_3_1.so -libraryjarslibs/armeabi/liblocSDK4.so -libraryjarslibs/baidumapapi_v2_3_1.jar -libraryjarslibs/core.jar -libraryjarslibs/gesture-imageview.jar -libraryjarslibs/gson-2.0.jar -libraryjarslibs/infogracesound.jar -libraryjarslibs/locSDK_4.0.jar -libraryjarslibs/ormlite-android-4.48.jar -libraryjarslibs/ormlite-core-4.48.jar -libraryjarslibs/universal-image-loader-1.9.0.jar -keepclasscom.baidu.**{*;} -keepclassvi.com.gdi.bgl.android.**{*;} -keeppublicclass*extendsandroid.app.Fragment -keeppublicclass*extendsandroid.app.Activity -keeppublicclass*extendsandroid.app.Application -keeppublicclass*extendsandroid.app.Service -keeppublicclass*extendsandroid.content.BroadcastReceiver -keeppublicclass*extendsandroid.content.ContentProvider -keeppublicclass*extendsandroid.app.backup.BackupAgentHelper -keeppublicclass*extendsandroid.preference.Preference -keeppublicclass*extendsandroid.support.v4.** -keeppublicclasscom.android.vending.licensing.ILicensingService -keepclasscom.google.gson.stream.**{*;} -keepclasscom.google.gson.examples.android.model.**{*;} -keepclasscom.uuhelper.Application.**{*;} -keepclassnet.sourceforge.zbar.**{*;} -keepclasscom.google.android.gms.**{*;} -keepclasscom.bank.pingan.model.**{*;} -keeppublicclass*extendscom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper -keeppublicclass*extendscom.j256.ormlite.android.apptools.OpenHelperManager -keepclasscom.android.vending.licensing.ILicensingService -keepclassandroid.support.v4.**{*;} -keepclassorg.apache.commons.net.**{*;} -keepclasscom.tencent.**{*;} -keepclasscom.umeng.**{*;} -keepclasscom.umeng.analytics.**{*;} -keepclasscom.umeng.common.**{*;} -keepclasscom.umeng.newxp.**{*;} -keepclasscom.j256.ormlite.**{*;} -keepclasscom.j256.ormlite.android.**{*;} -keepclasscom.j256.ormlite.field.**{*;} -keepclasscom.j256.ormlite.stmt.**{*;} -dontwarnandroid.support.v4.** -dontwarnorg.apache.commons.net.** -dontwarncom.tencent.** -keepclasseswithmembernamesclass*{ native<methods>; } -keepclasseswithmembernamesclass*{ public<init>(android.content.Context,android.util.AttributeSet); } -keepclasseswithmembernamesclass*{ public<init>(android.content.Context,android.util.AttributeSet,int); } -keepclassmembersenum*{ publicstatic**[]values(); publicstatic**valueOf(java.lang.String); } -keepclass*implementsandroid.os.Parcelable{ publicstaticfinalandroid.os.Parcelable$Creator*; } -keepclasseswithmembersclass*{ public<init>(android.content.Context); } -dontshrink -dontoptimize -dontwarncom.google.android.maps.** -dontwarnandroid.webkit.WebView -dontwarncom.umeng.** -dontwarncom.tencent.weibo.sdk.** -dontwarncom.facebook.** -keepenumcom.facebook.** -keepattributesExceptions,InnerClasses,Signature -keepattributes*Annotation* -keepattributesSourceFile,LineNumberTable -keeppublicinterfacecom.facebook.** -keeppublicinterfacecom.tencent.** -keeppublicinterfacecom.umeng.socialize.** -keeppublicinterfacecom.umeng.socialize.sensor.** -keeppublicinterfacecom.umeng.scrshot.** -keeppublicclasscom.umeng.socialize.*{*;} -keeppublicclassjavax.** -keeppublicclassandroid.webkit.** -keepclasscom.facebook.** -keepclasscom.umeng.scrshot.** -keeppublicclasscom.tencent.**{*;} -keepclasscom.umeng.socialize.sensor.** -keepclasscom.tencent.mm.sdk.openapi.WXMediaMessage{*;} -keepclasscom.tencent.mm.sdk.openapi.**implementscom.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject{*;} -keepclassim.yixin.sdk.api.YXMessage{*;} -keepclassim.yixin.sdk.api.**implementsim.yixin.sdk.api.YXMessage$YXMessageData{*;} -keeppublicclass[your_pkg].R$*{ publicstaticfinalint*; }
2015-05-25更新
1.依赖工程的jar申明:
以下是邮件发送的实例
-libraryjars../KlowerBase/libs/mail-activation.jar
-libraryjars../KlowerBase/libs/mail-additionnal.jar
-libraryjars../KlowerBase/libs/mail.jar
2.解决邮件发送时报的以异常
-keepclassjavax.mail.**{*;} -keepclasscom.sun.mail.**{*;} -keepclassjavax.activation.**{*;} -keepclassorg.apache.harmony.**{*;} -keepclassjava.security.**{*;}
#解决:can'tfindreferencedclassjava.beans.Beans -dontwarnorg.apache.commons.collections.BeanMap -dontwarnjava.beans.** #解决:can'tfindreferencedclassjavax.security.sasl.Sasl -dontwarncom.sun.mail.imap.protocol.**
通过本文希望能帮助Android开发应用使用混淆代码的朋友,谢谢大家对本站的支持!