user2342558
user2342558

Reputation: 6731

Why does R8 not rename all methods and classes?

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.

enter image description here enter image description here

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!

Upvotes: 1

Views: 1939

Answers (1)

user2711811
user2711811

Reputation:

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.

Upvotes: 5

Related Questions