Why the ids are removed?

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?

Answer

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.

unused resources dialog.