android混淆

时间:2022-11-29 14:57:41


为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤:

1. 大家也许都注意到新建一个工程会看到项目下边有这样proguard-project.txt一个文件,这个对混淆代码很重要,如果你不小心删掉了,没关系,从其他地方拷贝一个过来

2. 最重要的就是在proguard-project.txt添加混淆的申明了:

 a. 把所有你的jar包都申明进来,例如:
 -libraryjars libs/apns_1.0.6.jar
 -libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so
 -libraryjars libs/armeabi/liblocSDK4.so
-libraryjars libs/baidumapapi_v2_3_1.jar
-libraryjars libs/core.jar
-libraryjars libs/gesture-imageview.jar
-libraryjars libs/gson-2.0.jar
-libraryjars libs/infogracesound.jar
-libraryjars libs/locSDK_4.0.jar
-libraryjars libs/ormlite-android-4.48.jar
-libraryjars libs/ormlite-core-4.48.jar
-libraryjars libs/universal-image-loader-1.9.0.jar

b. 将你不需要混淆的部分申明进来,因为有些类经过混淆会导致程序编译不通过,如下:

-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.android.vending.licensing.ILicensingService

--以上都是API里边的类,最好都要避免混淆

有些很特殊的,例如百度地图,你需要添加以下申明:

-keep class com.baidu.** { *; } 
-keep class vi.com.gdi.bgl.android.**{*;}

根据我的经验,一般model最好避免混淆(model无关紧要,不混淆也没多大关系)如:

-keep class com.bank.pingan.model.** { *; }
下面在贴上关于Umeng分享统计的避免混淆的申明

-dontwarn android.support.v4.**  
-dontwarn org.apache.commons.net.** 
-dontwarn com.tencent.** 

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context);
}

-dontshrink
-dontoptimize
-dontwarn com.google.android.maps.**
-dontwarn android.webkit.WebView
-dontwarn com.umeng.**
-dontwarn com.tencent.weibo.sdk.**
-dontwarn com.facebook.**

-keep enum com.facebook.**
-keepattributes Exceptions,InnerClasses,Signature
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable

-keep public interface com.facebook.**
-keep public interface com.tencent.**
-keep public interface com.umeng.socialize.**
-keep public interface com.umeng.socialize.sensor.**
-keep public interface com.umeng.scrshot.**

-keep public class com.umeng.socialize.* {*;}
-keep public class javax.**
-keep public class android.webkit.**

-keep class com.facebook.**
-keep class com.umeng.scrshot.**
-keep public class com.tencent.** {*;}
-keep class com.umeng.socialize.sensor.**

-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}

-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}

-keep class im.yixin.sdk.api.YXMessage {*;}
-keep class im.yixin.sdk.api.** implements im.yixin.sdk.api.YXMessage$YXMessageData{*;}

-keep public class [your_pkg].R$*{
    public static final int *;
}

3.以上工作完成,混淆工作就完成了一大半了,最后需要做的就是在project.properties文件中加上你的混淆文件申明了,如下:

   proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

4. OK, 最后一步,打签名包测试,如果有问题,仔细看下Log也许有得类不能混淆,那么你得加入到proguard-project.txt文件中

-------以上就是混淆代码的全过程了

 

最后贴上proguard-project.txt的全部代码:

  


[java]  ​​view plain​​ ​​cop​​



  1. # To enable ProGuard in your project, edit project.properties  
  2. # to define the proguard.config property as described in that file.  
  3. #  
  4. # Add project specific ProGuard rules here.  
  5. # By default, the flags in this file are appended to flags specified  
  6. # in ${sdk.dir}/tools/proguard/proguard-android.txt  
  7. # You can edit the include path and order by changing the ProGuard  
  8. # include property in project.properties.  
  9. #  
  10. # For more details, see  
  11. #   http://developer.android.com/guide/developing/tools/proguard.html  
  12.   
  13. # Add any project specific keep options here:  
  14.   
  15. # If your project uses WebView with JS, uncomment the following  
  16. # and specify the fully qualified class name to the JavaScript interface  
  17. # class:  
  18. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {  
  19. #   public *;  
  20. #}  
  21. -optimizationpasses 5  
  22. -dontusemixedcaseclassnames  
  23. -dontskipnonpubliclibraryclasses  
  24. -dontpreverify  
  25. -verbose  
  26. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  
  27.   
  28. -keepattributes *Annotation*  
  29. -keepattributes Signature  
  30.   
  31. -libraryjars libs/apns_1.0.6.jar  
  32. -libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so  
  33. -libraryjars libs/armeabi/liblocSDK4.so  
  34. -libraryjars libs/baidumapapi_v2_3_1.jar  
  35. -libraryjars libs/core.jar  
  36. -libraryjars libs/gesture-imageview.jar  
  37. -libraryjars libs/gson-2.0.jar  
  38. -libraryjars libs/infogracesound.jar  
  39. -libraryjars libs/locSDK_4.0.jar  
  40. -libraryjars libs/ormlite-android-4.48.jar  
  41. -libraryjars libs/ormlite-core-4.48.jar  
  42. -libraryjars libs/universal-image-loader-1.9.0.jar  
  43.   
  44. -keep class com.baidu.** { *; }   
  45. -keep class vi.com.gdi.bgl.android.**{*;}  
  46.   
  47. -keep public class * extends android.app.Fragment    
  48. -keep public class * extends android.app.Activity  
  49. -keep public class * extends android.app.Application  
  50. -keep public class * extends android.app.Service  
  51. -keep public class * extends android.content.BroadcastReceiver  
  52. -keep public class * extends android.content.ContentProvider  
  53. -keep public class * extends android.app.backup.BackupAgentHelper  
  54. -keep public class * extends android.preference.Preference  
  55. -keep public class * extends android.support.v4.**  
  56. -keep public class com.android.vending.licensing.ILicensingService  
  57.   
  58. -keep class com.google.gson.stream.** { *; }  
  59. -keep class com.google.gson.examples.android.model.** { *; }  
  60. -keep class com.uuhelper.Application.** { *; }  
  61. -keep class net.sourceforge.zbar.** { *; }  
  62. -keep class com.google.android.gms.** { *; }   
  63.   
  64. -keep class com.bank.pingan.model.** { *; }  
  65.   
  66. -keep public class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper  
  67. -keep public class * extends com.j256.ormlite.android.apptools.OpenHelperManager  
  68.    
  69. -keep class com.android.vending.licensing.ILicensingService  
  70. -keep class android.support.v4.** { *; }    
  71. -keep class org.apache.commons.net.** { *; }    
  72. -keep class com.tencent.** { *; }    
  73.    
  74. -keep class com.umeng.** { *; }    
  75. -keep class com.umeng.analytics.** { *; }    
  76. -keep class com.umeng.common.** { *; }    
  77. -keep class com.umeng.newxp.** { *; }    
  78.    
  79. -keep class com.j256.ormlite.** { *; }    
  80. -keep class com.j256.ormlite.android.** { *; }    
  81. -keep class com.j256.ormlite.field.** { *; }    
  82. -keep class com.j256.ormlite.stmt.** { *; }   
  83.   
  84. -dontwarn android.support.v4.**    
  85. -dontwarn org.apache.commons.net.**   
  86. -dontwarn com.tencent.**    
  87.   
  88. -keepclasseswithmembernames class * {  
  89. native <methods>;  
  90. }  
  91.   
  92. -keepclasseswithmembernames class * {  
  93. public <init>(android.content.Context, android.util.AttributeSet);  
  94. }  
  95.   
  96. -keepclasseswithmembernames class * {  
  97. public <init>(android.content.Context, android.util.AttributeSet, int);  
  98. }  
  99.   
  100. -keepclassmembers enum * {  
  101. public static **[] values();  
  102. public static ** valueOf(java.lang.String);  
  103. }  
  104.   
  105. -keep class * implements android.os.Parcelable {  
  106. public static final android.os.Parcelable$Creator *;  
  107. }  
  108.   
  109. -keepclasseswithmembers class * {  
  110. public <init>(android.content.Context);  
  111. }  
  112.   
  113. -dontshrink  
  114. -dontoptimize  
  115. -dontwarn com.google.android.maps.**  
  116. -dontwarn android.webkit.WebView  
  117. -dontwarn com.umeng.**  
  118. -dontwarn com.tencent.weibo.sdk.**  
  119. -dontwarn com.facebook.**  
  120.   
  121. -keep enum com.facebook.**  
  122. -keepattributes Exceptions,InnerClasses,Signature  
  123. -keepattributes *Annotation*  
  124. -keepattributes SourceFile,LineNumberTable  
  125.   
  126. -keep public interface com.facebook.**  
  127. -keep public interface com.tencent.**  
  128. -keep public interface com.umeng.socialize.**  
  129. -keep public interface com.umeng.socialize.sensor.**  
  130. -keep public interface com.umeng.scrshot.**  
  131.   
  132. -keep public class com.umeng.socialize.* {*;}  
  133. -keep public class javax.**  
  134. -keep public class android.webkit.**  
  135.   
  136. -keep class com.facebook.**  
  137. -keep class com.umeng.scrshot.**  
  138. -keep public class com.tencent.** {*;}  
  139. -keep class com.umeng.socialize.sensor.**  
  140.   
  141. -keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}  
  142.   
  143. -keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}  
  144.   
  145. -keep class im.yixin.sdk.api.YXMessage {*;}  
  146. -keep class im.yixin.sdk.api.** implements im.yixin.sdk.api.YXMessage$YXMessageData{*;}  
  147.   
  148. -keep public class [your_pkg].R$*{  
  149. public static final int *;  
  150. }