Proguard/R8 do not rename classes used in XML layouts - android

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 ;)

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.

Related

How to find a specific warning in the whole project

Is there a way to search a specific warning in the whole project?
e.g. I have this warning in one of my LinearLayouts:
Set android:baselineAligned="false" on this element for better
performance
and I want to check if there are any similar warnings in other LinearLayouts in my XMLs. I know about Analyze -> Inspect Code but it doesn't help me in this situation and in this kind of problem.
How can I find them?
There is no filters but you can select custom scope in your case is xml folder as
file[app]:src/main/res/layout/*
Or you can Run Inspection by Name ... and this should be -> Missing baselineAligned attribute

Setting layout by remote xml

I was wondering if it was possible to set a view through a remote xml file. I had a look on the web and I found this post here on stackoverflow. Reading the answers I got I can't do it.
Why?
XML layout files are pre-processed at build time in order to provide efficient inflation of complex layouts.
Although there are LayoutInflator methods which take a path to an XML file, they have never been implemented.
In other words, unless your XML layout file is pre-processed and packaged at build time into your APK, then it can't be done.
There is one possibility, however, you could build an XML parser to parse your 'external' XML layout file and create your layout dynamically using Java code - not impossible but you're pretty much on your own if you choose to do that.

difference between #android and #*android

I've been given a few xml layout files made by another group of developers, one thing concerns me is that they used a mix of #android and #*android in their files, like
#android:style/Theme
#*android:style/Theme
Their xml layout files seem to be working fine at the moment, but I am curious about the difference between # and #*, please help!

How to rename widget ID's in Eclipse?

I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.

Deserialize Android Layout XML?

Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code? Does a tool like JAXB work and if so how do I use it?
It's an interesting idea, though a bit useless IMHO. Why would one want to do it? Inflating an XML is very fast operation and hides a lot of complexity - for example it takes into account what is the current DisplayMetrics and recalculates layout parameters (width/height) appropriately to the density and size of the screen... It's also very fast because it does not actually require XML parsing - parsing is done at compilation time and what is actually stored by android is a binary version of the layout which is optimized for efficiency (that's why you cannot build layout XML dynamically from an XML file).
If you would like to modify the Java code and add/remove some elements then it is much more efficient to inflate the XML and then do all the modifications -less clutter simpler code and all the calculations are done for you...
Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code?
Step #1: Parse the XML.
Step #2: Generate the Java code. You'll have to pray that you can build your own AttributeSet implementation that works with your generated code, otherwise this will be a very complex problem.
Does a tool like JAXB work
AFAIK, JAXB needs an XML Schema, and there is no such schema possible for the Android layout file.
Whatever problem you're trying to solve this way, there's probably a better solution.
I just want a simple way to take an android xml layout and pass it to, say, a command line tool to generate the equivalent Java.
I seriously doubt that this will be "simple" for any reasonable definition of the term.

Categories

Resources