android代码混淆

什么是代码混淆

Java 是一种跨平台的、解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中。由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名、方法名,并且通过这些名称来访问变量和方法,这些符号带有许多语义信息,很容易被反编译成 Java 源代码。为了防止这种现象,我们可以使用 Java 混淆器对 Java 字节码进行混淆。

混淆就是对发布出去的程序进行重新组织和处理,使得处理后的代码与处理前代码完成相同的功能,而混淆后的代码很难被反编译,即使反编译成功也很难得出程序的真正语义。被混淆过的程序代码,仍然遵照原来的档案格式和指令集,执行结果也与混淆前一样,只是混淆器将代码中的所有变量、函数、类的名称变为简短的英文字母代号,在缺乏相应的函数名和程序注释的况下,即使被反编译,也将难以阅读。同时混淆是不可逆的,在混淆的过程中一些不影响正常运行的信息将永久丢失,这些信息的丢失使程序变得更加难以理解。

混淆器的作用不仅仅是保护代码,它也有精简编译后程序大小的作用。由于以上介绍的缩短变量和函数名以及丢失部分信息的原因, 编译后 jar 文件体积大约能减少25% ,这对当前费用较贵的无线网络传输是有一定意义的。

混淆文件 proguard.cfg 参数详解

复制代码
-optimizationpasses 5                                                           # 指定代码的压缩级别
-dontusemixedcaseclassnames                                                     # 是否使用大小写混合
-dontskipnonpubliclibraryclasses                                                # 是否混淆第三方jar
-dontpreverify                                                                  # 混淆时是否做预校验
-verbose                                                                        # 混淆时是否记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*        # 混淆时所采用的算法

-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 com.android.vending.licensing.ILicensingService              # 保持哪些类不被混淆

-keepclasseswithmembernames class * {                                           # 保持 native 方法不被混淆
    native <methods>;
}

-keepclasseswithmembers class * {                                               # 保持自定义控件类不被混淆
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);     # 保持自定义控件类不被混淆
}

-keepclassmembers class * extends android.app.Activity {                        # 保持自定义控件类不被混淆
   public void *(android.view.View);
}

-keepclassmembers enum * {                                                      # 保持枚举 enum 类不被混淆
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {                                # 保持 Parcelable 不被混淆
  public static final android.os.Parcelable$Creator *;
}

-keep class MyClass;                                                            # 保持自己定义的类不被混淆
复制代码

代码混淆的方法

根据 SDK 的版本不同有 2 中不同的代码混淆方式,以上的 proguard.cfg 参数详解中所涉及到的信息是在较低版本 SDK 下的混淆脚本,事实上在高版本的 SDK 下混淆的原理和参数也与低版本的相差无几,只是在不同 SDK 版本的环境下引入混淆脚本的方式有所不同。具体方法如下:

  • 低版本 SDK 下,项目中同时包含 proguard.cfg project.properties 文件,则只需在 project.properties 文件末尾添加 proguard.config=proguard.cfg 再将项目 Export 即可。
  • 高版本 SDK 下,项目中同时包含 proguard-project.txt project.properties 文件,这时需要在 proguard-project.txt 文件中进行如下信息的配置,然后再将项目 Export 即可。下面以真实的文件进行演示说明。
复制代码
# 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

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

# Project target.
target=android-16
复制代码

以上的配置信息即是 project.properties 文件中内容,蓝色文字为我们在代码混淆过程中需要添加的配置信息,其中:sdk.dir 为你在当前机器上 SDK 的安装路径。如果想保留某个包下的文件不被混淆,可以在 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:

-dontwarn com.cnki.android.cnkireader.** 
-keep class com.cnki.android.cnkireader.** { *; }

# 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 *;
#}
复制代码

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

  

  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 *;  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值