为什么R8不重命名所有方法和类? [英] Why does R8 not rename all methods and classes?

查看:99
本文介绍了为什么R8不重命名所有方法和类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用具有混淆和代码优化功能的R8的Android Studio版本.

Contenuti manifest.xml 中提到的活动.

mostraView nascondiView 是我创建的方法,它们不在任何库中,也不扩展任何内容,因此我希望看到它们的名称已更改.

  void mostraView(View v){v.setVisibility(View.VISIBLE);}无效的nascondiView(View v){v.setVisibility(View.GONE);} 

此R8的行为正确吗?

如何设置R8来混淆它们,或者至少混淆这两个?

非常感谢!

解决方案

此默认规则可能是导致此问题的原因:

 #我们希望将可以在XML属性onClick中使用的方法保留在Activity中.-keepclassmembers类*扩展了android.app.Activity {公共无效*(android.view.View);} 

我是通过使用诊断开关 -printseeds printconfiguration 来获得的.

匹配的方法将是(a)在扩展Activity的类中定义的,并且(b)具有与模式匹配的方法签名(基本上是任何名称和View参数).

但是,唯一可以重现您问题的方法是,如果我修改了访问修饰符,使其包含 public ,如下所示:

  public void mostraView(View v){v.setVisibility(View.VISIBLE);} 

请注意添加了关键字 public .因此,假设您所发布的代码是逐字记录的,那么默认访问修饰符就是不匹配该模式的package friend.

无论如何,我都可以进行-经过重述的修改可以重现该问题,并提供了基于默认-keeps的可能解释.

请注意,保留"一词已被重载,因为它也适用于混淆.

I'm trying for my first time an Android Studio version with R8 that perform obfuscation and code optimization.

As the official documentation says:

Obfuscate your code

The purpose of obfuscation is to reduce your app size by shortening the names of your app’s classes, methods, and fields.

I think that R8 will rename all method and class names, but if I analyze the APK through "Build -> Analyze APK..." I can read most of the original method and class names.

Contenuti is an Activity mentioned in the manifest.xml.

mostraView and nascondiView are methods created by me, they aren't in any library, they don't extend nothing, so I expected to see their name changed.

void mostraView(View v)
{
     v.setVisibility(View.VISIBLE);
}
void nascondiView(View v)
{
     v.setVisibility(View.GONE);
}

Is this R8's behavior correct?

How to set R8 to obfuscate all of them, or at least these two?

Thanks a lot!

解决方案

This default rule may be the cause:

# We want to keep methods in Activity that could be used in the XML attribute onClick.
-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

I obtained this from using diagnostic switches -printseeds and -printconfiguration.

A matched method would be (a) defined in a class extending Activity and (b) have a method signature matching the pattern (essentially any name and a View paramter).

However the only way I could reproduce your issue is if I modified the access modifier to include public as in:

public void mostraView(View v)
{
    v.setVisibility(View.VISIBLE);
}

Note the addition of the keyword public. So assuming your posted code is verbatim then the default access modifier is package friend which would not match the pattern.

Anyways that's as far as I can take - was able to reproduce the problem with noted modification and provided a possible explanation based on default -keeps.

Note the term "keep" is overloaded in that it also applies to obfuscation.

这篇关于为什么R8不重命名所有方法和类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆