Android R8 不混淆类名
Posted
技术标签:
【中文标题】Android R8 不混淆类名【英文标题】:Android R8 not obfuscating class names 【发布时间】:2020-10-05 14:05:28 【问题描述】:过去几个小时我一直在研究这个,但没有任何运气。无论如何,类名都不会被混淆。这些只是常规类,而不是活动、服务或其他也在 android 清单中的东西(我知道那些不会被混淆)。我在这里错过了什么?
Android Gradle Plugin version: 4.0.0
Gradle version: 6.1.1
Android Studio version: 4.0
对于这些版本,R8 should be enabled by default。这是我的 buildType 配置:
buildTypes
release
//useProguard false // even tried this without luck
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
这是我的proguard-rules.pro
-ignorewarnings
# --- Glide ---
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$**
**[] $VALUES;
public *;
# --- Billing library ---
-keep class com.android.vending.billing.**
# --- Retrofit2 ---
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod
# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface *
@retrofit2.http.* <methods>;
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * @retrofit2.http.* <methods>;
-keep,allowobfuscation interface <1>
# --- TwitterKit ---
#Picasso Proguard Config https://github.com/square/picasso
-dontwarn com.squareup.okhttp.**
# --- GSON ---
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** *;
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** <fields>;
# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class *
@com.google.gson.annotations.SerializedName <fields>;
# --- SciChart ---
# ignore warnings and save classes required for syntax highlighting
-dontwarn java.awt.**
-dontwarn javax.swing.**
-dontwarn syntaxhighlight.**
-keep public class java.awt.** *;
-keep public class javax.swing.** *;
-keep public class syntaxhighlight.** *;
-keep public class prettify.** *;
# need to keep these classes and their methods because they are used by resampling code
-keep public class com.scichart.core.model.DoubleValues *;
-keep public class com.scichart.core.model.FloatValues *;
-keep public class com.scichart.core.model.IntegerValues *;
-keep public class com.scichart.data.model.Point2DSeries *;
# repack obfuscated classes into single package so it would be hard to find their originall package
-repackageclasses ''
-allowaccessmodification
我检查过但没有提供任何解决方案的类似问题:
Android studio 3.4.2 R8 obfuscator does not obfuscate class names, but only java code inside Class no longer obfuscated after upgrading to Android Gradle plugin 3.4.0 Android/java: Transition / Migration from ProGuard to R8?【问题讨论】:
【参考方案1】:根据WorkManager's proguard file,预计所有扩展ListenableWorker
(及其子类,如Worker
)的类都会被保留。这是因为类名是 WorkManager 内部数据库中的唯一键。
【讨论】:
以上是关于Android R8 不混淆类名的主要内容,如果未能解决你的问题,请参考以下文章
Progruard 和 R8 被弃用 - Android Studio 3.6
com.android.tools.r8.utils.AbortException:错误:程序输入 - 不支持的源文件类型