使用@Keep注解防止被混淆

QuibblerQuibbler 2024-04-09 约 10 分钟 2078 次阅读

使用@Keep注解防止被混淆

使用proguard混淆代码是对产品本身的一种保护,常见的方法就是编写projuard-rules.pro配置文件。已经有开源项目将各类混淆配置集中到一起:github/android-proguard-snippets,提供给大家参考。一个典型的Android项目混淆配置文件如下:

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
 
//混淆时不使用大小写混合类名
-dontusemixedcaseclassnames
//不跳过library中的非public的类
-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
//不进行预校验,预校验是作用在Java平台上的,Android平台上不需要这项功能,去掉之后还可以加快混淆速度
-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*
//保留Google原生服务需要的类
-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
//保留native方法的类名和方法名
-keepclasseswithmembernames class * {
    native <methods>;
}
 
# 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
//保留Activity中参数是View的方法,如XML中配置android:onClick=”buttonClick”属性,Activity中调用的buttonClick(View view)方法
-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);
}
 
//Parcelable实现类中的CREATOR字段是绝对不能改变的,包括大小写
-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}
 
//R文件中的所有记录资源id的静态字段
-keepclassmembers class **.R$* {
    public static <fields>;
}
 
# The support library contains references to newer platform versions.
# Dont warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
//忽略support包因为版本兼容产生的警告
-dontwarn android.support.**

在AndroidStudio项目中都会用到proguard-android文件,这里大家一定不会陌生:

release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

一些常见的Proguard关键字如下,更多关于Proguard详见ProGuard manual。

keep                                            保留类和类中的成员,防止被混淆或移除

keepnames                                 保留类和类中的成员,防止被混淆,成员没有被引用会被移除

keepclassmembers                     只保留类中的成员,防止被混淆或移除

keepclassmembernames            只保留类中的成员,防止被混淆,成员没有引用会被移除

keepclasseswithmembers           保留类和类中的成员,防止被混淆或移除,保留指明的成员

keepclasseswithmembernames  保留类和类中的成员,防止被混淆,保留指明的成员,成员没有引用会被移除

使用@Keep注解能够更优雅的完成这一枯燥的混淆配置过程,哪里不对@Keep哪里。

#手动启用support keep注解
    #http://tools.android.com/tech-docs/support-annotations
    -dontskipnonpubliclibraryclassmembers
    -printconfiguration
    -keep,allowobfuscation @interface android.support.annotation.Keep
	 
    -keep @android.support.annotation.Keep class *
    -keepclassmembers class * {
        @android.support.annotation.Keep *;
    }

相关推荐

精选
获取系统SDK版本、判断手机ROM
Code

获取系统SDK版本、判断手机ROM

Build获取系统SDK版本Android中部分API的使用,需要在特定的SDK版本之后才能使用,因此在兼容老版本SDK的时候,经常需要判断API的版本。各种Android版本的对应关系参考《Android各版本对应的SDK版本》判断手机ROM有时候需要判断手机系统的ROM,检测ROM是MIUI、EMUI还是Flyme,可以使用getprop命令,去系统build.prop文件(关于build.p

3.7k
精选
AndroidStudio中各种中文乱码问题
Code

AndroidStudio中各种中文乱码问题

AndroidStudio中各种中文乱码问题1、编译Java错误信息乱码在出现这个Annotation processors must be explicitly declared now...问题的时候,正好也发现AndroidStudio Build Output错误信息都乱码。经常遇到各种问题,习惯了,现在遇到的问题,都是以后的答案。 1.1、修改项目build.gradle(无效)在整个p

2.4k
AndroidStudio 报错:has no declaration in the base values folder
Code

AndroidStudio 报错:has no declaration in the base values folder

在资源文件中正常定义的值,也能编译运行。昨天还正常,今天一打开就报错。应该又是AndroidStudio自身的Bug了。原因众说纷纭,参考Stack Overflow 一篇类似的讨论。解决:File => Invalidate Caches / Restart => Invalidate and Restart.

4.4k