全部产品
云市场
云游戏

混淆 Android 文件

更新时间:2020-06-16 16:14:13

mPaaS Android 客户端开发的应用程序是通过 Java 代码编写而成,而 Java 代码易被反编码,因此为了保护 Java 源代码,需要使用 ProGuard 混淆 Android 文件。

ProGuard 是一个压缩、优化和混淆 Java 字节码文件的工具。

  • 压缩 指检测以及删除没有用到的类、字段、方法以及属性。
  • 优化 指分析以及优化方法的字节码。
  • 混淆 指使用无意义的短变量,对类、变量、方法进行重命名。

使用 ProGuard 可以让代码更精简,更高效,也更难被逆向或破解。

前置条件

您已经配置 mPaaS 工程。

关于此任务

采用组件化方案的 mPaaS 工程,每一个 bundle 的编译产物都是一个已经混淆的 dex 文件,所以配置混淆文件是以 bundle 工程为单位而进行的。Portal 工程通常没有代码,不需要开启混淆。

代码示例

  • Gradle 配置

    1. android {
    2. compileSdkVersion 23
    3. buildToolsVersion "19.1.0"
    4. defaultConfig {
    5. applicationId "com.youedata.xionganmaster.launcher"
    6. minSdkVersion 15
    7. targetSdkVersion 23
    8. versionCode 1
    9. versionName "1.0"
    10. }
    11. buildTypes {
    12. release {
    13. // 混淆开关,是否混淆
    14. minifyEnabled true
    15. // 混淆文件列表,混淆规则配置
    16. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    17. }
    18. }
    19. lintOptions {
    20. checkReleaseBuilds false
    21. // Or, if you prefer, you can continue to check for errors in release builds,
    22. // but continue the build even when errors are found:
    23. abortOnError false
    24. }
    25. }
  • 混淆文件示例

    下列混淆是一个基本示例(如果要添加额外的第三方库,需要加入其它混淆,通常配置文件可在第三方库的官网中找到):

    1. # Add project specific ProGuard rules here.
    2. # By default, the flags in this file are appended to flags specified
    3. # in ${sdk.dir}/tools/proguard/proguard-android.txt
    4. # You can edit the include path and order by changing the proguardFiles
    5. # directive in build.gradle.
    6. # For more details, see [Shrink your code and resources](http://developer.android.com/guide/developing/tools/proguard.html)。
    7. # Add any project specific keep options here:
    8. # If your project uses WebView with JS, uncomment the following
    9. # and specify the fully qualified class name to the JavaScript interface
    10. # class:
    11. # -keepclassmembers class fqcn.of.javascript.interface.for.webview {
    12. # public *;
    13. # }
    14. -optimizationpasses 5
    15. -dontusemixedcaseclassnames
    16. -dontskipnonpubliclibraryclasses
    17. -dontpreverify
    18. -verbose
    19. -ignorewarnings
    20. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
    21. -keep public class * extends android.app.Activity
    22. -keep public class * extends android.app.Application
    23. -keep public class * extends android.app.Service
    24. -keep public class * extends android.content.BroadcastReceiver
    25. -keep public class * extends android.content.ContentProvider
    26. -keep public class com.android.vending.licensing.ILicensingService
    27. -keep public class com.alipay.mobile.phonecashier.*
    28. -keepnames public class *
    29. -keepattributes SourceFile,LineNumberTable
    30. -keepattributes *Annotation*
    31. #-keep public class * extends com.alipay.mobile.framework.LauncherApplicationAgent {
    32. # *;
    33. #}
    34. #-keep public class * extends com.alipay.mobile.framework.LauncherActivityAgent {
    35. # *;
    36. #}
    37. -keepclasseswithmembernames class * {
    38. native <methods>;
    39. }
    40. -keepclasseswithmembernames class * {
    41. public <init>(android.content.Context, android.util.AttributeSet);
    42. }
    43. -keepclasseswithmembernames class * {
    44. public <init>(android.content.Context, android.util.AttributeSet, int);
    45. }
    46. -keepclassmembers enum * {
    47. public static **[] values();
    48. public static ** valueOf(java.lang.String);
    49. }
    50. -keep class * extends java.lang.annotation.Annotation { *; }
    51. -keep interface * extends java.lang.annotation.Annotation { *; }
    52. -keep class * implements android.os.Parcelable {
    53. public static final android.os.Parcelable$Creator *;
    54. }
    55. -keep public class * extends android.view.View{
    56. !private <fields>;
    57. !private <methods>;
    58. }
    59. -keep class android.util.**{
    60. public <fields>;
    61. public <methods>;
    62. }
    63. -keep public class com.squareup.javapoet.**{
    64. !private <fields>;
    65. !private <methods>;
    66. }
    67. -keep public class javax.annotation.**{
    68. !private <fields>;
    69. !private <methods>;
    70. }
    71. -keep public class javax.inject.**{
    72. !private <fields>;
    73. !private <methods>;
    74. }
    75. -keep interface **{
    76. !private <fields>;
    77. !private <methods>;
    78. }
    79. # for dagger
    80. -keep class * extends dagger.internal.Binding
    81. -keep class * extends dagger.internal.ModuleAdapter
    82. -keep class **$$ModuleAdapter
    83. -keep class **$$InjectAdapter
    84. -keep class **$$StaticInjection
    85. -keep class dagger.** { *; }
    86. -keep class javax.inject.**{ *; }
    87. -keep class * extends dagger.internal.Binding
    88. -keep class * extends dagger.internal.ModuleAdapter
    89. -keep class * extends dagger.internal.StaticInjection
    90. # for butterknife
    91. -keep class butterknife.* { *; }
    92. -keep class butterknife.** { *; }
    93. -dontwarn butterknife.internal.**
    94. -keep class **$$ViewBinder { *; }
    95. -keepclasseswithmembernames class * {
    96. @butterknife.* <fields>;
    97. }
    98. -keepclasseswithmembernames class * {
    99. @butterknife.* <methods>;
    100. }
    说明:如果在您的 bundle 工程中定义了框架类LauncherApplicationAgentLauncherActivityAgent,请注意进行防混淆设置。
  • 避免混淆通用组件

    如果在metainfo.xml中注册了通用组件,编译时会检查这些组件是否存在,请避免这些组件被混淆,否则会编译失败。例如注册了以下组件:

    1. <metainfo>
    2. <service>
    3. <className>com.mpaas.cq.bundleb.MyServiceImpl</className>
    4. <interfaceName>com.mpaas.cq.bundleb.api.MyService</interfaceName>
    5. <isLazy>true</isLazy>
    6. </service>
    7. </metainfo>

    请在混淆配置中添加:

    1. -keep class com.mpaas.cq.bundleb.MyServiceImpl
    2. -keep class com.mpaas.cq.bundleb.api.MyService

    相关链接

    ProGuard 帮助手册