androidandroid-studiogradleaaptaapt2

How from aapt2 generate the same ID that was generated by aapt?


For some raison I do not yet understand, aapt and aapt2 do not output the same ID. is their any way to force aapt2 to output the same ID generated by aapt ?


Solution

  • No, you can't even force aapt resp aapt2 to guaranteedly assign the same resource IDs in consecutive runs. The IDs in the R.java file are static during the app's runtime but will be dynamically assigned during compile time.

    For a short discussion on the reasons you might take a look at this Stackoverflow thread on the purpose and structure of resource IDs.

    Further, it's discouraged to even touch the R.java file and with that the resource IDs. You also don't need to know those ID's for any purpose. Just use the common documented ways to access the app resources and you'll be perfectly fine.

    NB: There are some methods to predefine IDs as e.g. described in this Stackoverflow thread, employing the R.id class. But there shouldn't be many real use cases for tricks like that. Usually you'll make things just unnecessarily complicated.