Android 混淆配置

177 阅读4分钟
Android Progurad是为了优化apk的体积,提高apk的编译效率所提供的工具。  
具体使用方式可以参考 ***/sdk/tools/progurad/progurad-android.txt中的内容。   
具体使用规则可以参考 ***/sdk/tools/progurad/docs/index.html中的内容。

使用方法

在项目中使用progurad
1、在每个项目的根目录中创建progurad-rules.pro文件,一般默认情况下新建一个项目或者新建一个module的时候编译工具都会默认将此文件创建出来如下图:

image.png

2、在progurad-rules.pro文件中配置我们自己想要配置的混淆规则,但需要注意一点就是我们自定义的混淆规则要在Android官方给出的混淆规则之上就行修改,否则会出现意想不到的问题。Android官方提供的混淆规则在progurad-android.txt中已经给出。因此在自定义混淆规则之前可以将官方的混淆规则copy过来在这个原有的基础之上进行自己的自定义配置。
3、在progurad-rules文件修改好之后还需要在对应项目模块下的build.gradle中进行配置是否使用progurad功能,配置如下:

  • 在build.gradle中的android节点中添加buildTypes节点
  • 在buildTypes节点中添加release节点和debug节点
  • release节点中配置上如下代码即可开启progurad功能
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  • debug节点可以配置也可以不进行配置,一般情况debug节点下面不进行progurad配置因为便于调试。 浅析progurad-android.txt的内容
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# This file is no longer maintained and is not used by new (2.2+) versions of the
# Android plugin for Gradle. Instead, the Android plugin for Gradle generates the
# default rules at build time and stores them in the build directory.

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# 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
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation* 
保留ILicensingService整个类
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>; 保留类中native修饰的方法
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
保留所有继承致View的类的带有set和get的方法
-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
保留在xml中自定的onClick方法
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
保留所有枚举类中的values方法和valueOf方法
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class **.R$* {
    public static <fields>;
}

# 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.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
保留被keep注解修饰的整个类
-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

关键字介绍
-keep [,modifier,... ] class_specification
指定需要保留的类和类成员(作为公共类库,应该保留所有可公开访问的public方法)
-keepclassmembers [,modifier,... ] class_specification
指定需要保留的类成员:变量或者方法
-keepclasseswithmembers [,modifier,... ] class_specification
指定保留的类和类成员,条件是所指定的类成员都存在(既在压缩阶段没有被删除的成员,效果和keep差不多)
-keepnames class_specification
[-keep allowshrinking class_specification 的简写]
指定要保留名称的类和类成员,前提是在压缩阶段未被删除。仅用于模糊处理
-keepclassmembernames class_specification
[-keepclassmembers allowshrinking class_specification 的简写]
指定要保留名称的类成员,前提是在压缩阶段未被删除。仅用于模糊处理
-keepclasseswithmembernames class_specification
[-keepclasseswithmembers allowshrinking class_specification 的简写]
指定要保留名称的类成员,前提是在压缩阶段后所指定的类成员都存在。仅用于模糊处理
-printseeds [filename]
指定详尽列出由各种-keep选项匹配的类和类成员。 列表打印到标准输出或给定文件。 该列表可用于验证是否真的找到了预期的类成员,特别是如果您使用通配符。 例如,您可能想要列出所有应用程序或您保存的所有小程序。

混淆问题

1、使用混淆之后可能会出现日志不打印的问题,其实这并不是日志不打印了只是因为混淆的时候将日志打印类一同混淆了,因此在混淆的时候可以在混淆配置文件中将日志打印的类过滤了, 也可参考blog.csdn.net/qq_29634351… 这篇文章中的方法进行配置。