Proguard/R8 do not rename classes used in XML layouts

782 Views Asked by At

I need to obfuscate the whole code of an Android library except some classes/methods which will be used by developers.

I succeed except that some classes invoked in my XML layouts have not been renamed and I struggle to find an option in R8/Proguard or a trick to force it, even if I have to update manually or with a script these classes in my XML layouts (I know that R8/Proguard do not edit them itself) thanks to the generated mapping.txt file.

The closer question I found is Proguard (R8) obfuscate custom view names but it did not solve the issue I face, R8/Proguard is still ignoring the rename for these classes :/

If anyone has an idea, you're welcome :) Thanks for your time and knowledge ;)

[EDIT]

I finally gave up and put placeholders in my XML layouts for my custom views and inflated them at runtime.

It's a shame that Proguard/R8 can not handle custom classes renaming in XML layouts with aapt :/

For info, I ran into issues also with the use of fragment items in my XML layouts where the name property is not renamed while the corresponding class is...

So for these too, I had to put placeholders and inflate them at runtime...

I let the question opened in case someone find a trick one day ;)

1

There are 1 best solutions below

2
On

There is currently no support for renaming inside XML layouts. As part of the compilation process the aapt2 tool will generate -keep rules for the names present in the XML layouts, so the Android runtime will be able to perform the required reflection for layout inflation.

By adding the following option to the configuration (proguard-rules.pro)

-printconfiguration <somefile>

the full configuration can be inspected including the rules generated by aapt2.