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!
Related
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.
To my understanding, the content file is just the xml files contents...but in a separate file. Whats the point?
I've seen
What is the role of content_main.xml in android studio 1.4?
and there doesn't seem to be much documentation about content_XXX.xml, so I was wondering, why did the new update include the default creation of this file?
Why divide the code into two files, why not just put all of the contents in its own xml file, I've tried it...and it works. I fail to understand why the content_main file is generated on creation of a new project.
Can someone give me a list of benefits of why I should be using the content file?
Thanks,
Ruchir
activity_main.xml is used to display coordinatorLayout, tabLayout, floatingActionButton, viewPager etc.
content_main.xml is used to display your stuff i.e. what you want to display to users.
for more details check my answer here.
Update after conversation in comments below: The point of doing this is to bring in more organization in the code. We can always go forward with the old way of putting everything in one file. Android Studio is just giving us a start with keeping the layouts in a more organized way to allow easy comprehension of code and reuse wherever possible, by segregating related stuff.
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.
There is already a similar question , but I am not satisfied with the answer
Can I put layout directory's xml file in different subdirectories in Android?
because there are too many xml files , if they are not into different group ,I can not find the specific one that I want .
Do you guys have any different method to solve this problem such as virtual group or something ?
As you can read in the linked question, and in the questions linked to this question, it is not supported to have custom folders inside the folders supported by Android.
You could clean up your files by following naming conventions, like layout_xyz.xml, image_abc.png, ...
I use the namespaceing technique,
i.e.
addnewrecord_main.xml
addnewrecord_custombutton.xml
addnewrecord_newbutton.png
search_listviewitem.xml
search_bgimage.png
etc
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.