In my Android project I use the view binding:
buildFeatures {
dataBinding = true
viewBinding = true
}
Ideally I would only use the dataBinding, but in this case I always need to use the tag
<layout ...> </layout>
in XML layouts to get it used. That's why I only use that where I really need to bind data within XML. In other cases I use viewBinding since the plugin 'kotlin-android-extensions' has been deprecated. Everything is ok, but the following issue occurs when I run Analyze -> Inspect code. Once it's done and some unused resources are found (under Android -> Lint -> Perfomance -> Unused resources) and I remove all of them since I do not need them, it also removes the assigned IDs to all views in XML's. However, the IDs in XMLs with tag <layout ...> </layout> are not removed. The IDs are removed only in case of use viewBinding.
I am not expert, but I think, removing unused resources does not have anything to do with IDs of Views and that shouldn't happen.
After each optimization (meaning cleaning project from unused resources) I always have to assign the IDs which is very unproductive, especially when the project gets bigger and there are much more Views with IDs.
Why does it happen and how can I prevent that?
This might be happening because lint doesn't know that you will use those IDs in view binding... which is wrongs it should know. What you can do is when you run the lint to find unused resources there is an option you can uncheck which doesn't remove the unused IDs.
.
Related
Since I discovered the use of view binding by enabling
buildFeatures {
viewBinding true
}
in my gradle file, I never used findviewById in my code again. I wonder now if there is a cons to doing things this way.
If this is the best method, why does android studio not enable this option by default when creating a new project? If not, when should I avoid using view binding? thank you.
Once you enable it for a project, view binding will generate a binding class for all of your layouts. That's the only "con" I see, it just generates more code, so it would increase the size of the project, compile time, etc. While it won't be a huge difference for projects with very little layouts, it could change significantly for larger projects.
Here's a very interesting read about ViewBinding performance : https://blog.stylingandroid.com/view-binding-performance/
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.
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
I have a certain menu item I call 'debug' that does some things that are handy for 'debug' releases. I want this menu item not to exist on 'release' APKs.
Is there a way to tell the compiler to ignore those entries in the XML menu file?
Thanks :-)
If you are using menu XML resources, you can isolate those debug-only choices into their own resource XML file. Then, in addition to inflating your main one, you also inflate the debug-only one, if BuildConfig.DEBUG is true:
if (BuildConfig.DEBUG) {
// inflate that second resource XML file
}
If you are setting up your menu using Java code, you can do the same basic thing, wrapping the debug-only add() calls in a check of BuildConfig.DEBUG.
The upcoming Gradle-based build system will help to make this a bit easier, in principle, but that is several months away at this point AFAIK.
If you use Jenkins to create your builds you can replace your debug menu xml file with the production one using shell commands. This is also very effective with webservice detail xml files.
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.