问题描述
我在我的应用程序中使用R8,并且具有几个自定义视图(在xml布局中引用),它们的名称完全没有混淆.有什么办法可以做到这一点?我正在使用标准的Gradle规则:
I am using R8 in my app and have several custom views (which are referenced in xml layouts) tho their names are not obfuscated at all. Is there any way to achieve this? I am using the standard Gradle rules:
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
并且还尝试了 android.enableR8.fullMode = true
,但这是相同的.
And also tried with android.enableR8.fullMode=true
but it's the same.
推荐答案
这是因为 proguard-android-optimize.txt 具有以下规则:
This is because the proguard-android-optimize.txt has the following rule:
# keep setters in Views so that animations can still work.
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
因此,默认情况下,不会混淆您的自定义视图或任何视图的名称.
So your custom views, or any views, will not have their names obfuscated by default.
现在的问题是,您是否仍可以使用R8重命名应用程序中的自定义视图?,答案是不是真的.
您可以通过复制以下内容来添加 -applymapping myCustomMapping.txt
< root_dir>/app/build/outputs/mapping/< build_variant>/mapping.txt
,并用混淆的名称替换所有对您的自定义视图的引用.像这样:
You could add an -applymapping myCustomMapping.txt
by copying the contents of<root_dir>/app/build/outputs/mapping/<build_variant>/mapping.txt
and replacing all references to your custom views that are NOT obfuscated with obfuscated names.Like this:
- 将
< root_dir>/app/build/outputs/mapping/< build_variant>/mapping.txt
的内容复制到新文件< root_dir>/app/myCustomMapping中.txt
- 在进行任何更改之前,它会像这样:
my.app.package.CustomView -> my.app.package.CustomView :
13:34:void <init>(android.content.Context,android.util.AttributeSet,int) -> <init>
15:16:void <init>(android.content.Context,android.util.AttributeSet,int,int,kotlin.jvm.internal.DefaultConstructorMarker) -> <init>
43:46:void customMethod() -> c
- 您只需要更改具有顶级类映射的这一行.请注意,由于android proguard规则,它未更改.将其更改为您想要的任何混淆的名称,例如:
my.app.package.CustomView -> my.app.package.youcantseemeatall :
13:34:void <init>(android.content.Context,android.util.AttributeSet,int) -> <init>
15:16:void <init>(android.content.Context,android.util.AttributeSet,int,int,kotlin.jvm.internal.DefaultConstructorMarker) -> <init>
43:46:void customMethod() -> c
- 最后,将这些行添加到您的
proguard-rules.pro
文件
-applymapping myCustomMapping.txt
-printmapping mapping.txt
上述步骤将更改您的.class文件,以将 CustomView
混淆为 youcantseemeatall
,但,您的资源文件仍将引用原始的 CustomView
名称,您的应用将在运行时崩溃.
Those above steps will change your .class files to obfuscate CustomView
to youcantseemeatall
, BUT your resource files will still reference the original CustomView
name and your app will crash at runtime.
不幸的是,确实没有一种方法可以通过proguard或Android Studio随附的任何工具来完成您的要求.可能有一个自定义的Gradle插件,可以在组装该应用之前更改所有自定义视图的名称,但现在找不到一个可以谷歌搜索的人.
Unfortunately there really isn't a way to do what your asking with proguard or any tooling that comes with Android Studio. There may be a custom Gradle Plugin that changes all custom view names before the app is assembled, but I couldn't find one just googling it now.
这篇关于Proguard(R8)混淆了自定义视图名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!