0

Are all IDs in R permanent? Recently I've made a game and use IDs of sound tracks to save to SharedPreferences. I'm wondering if those IDs (of the same files) will be changed at next build? Could you point me to documentation?

1 Answers1

3

They are changed every time the resources are changed. You should not rely on hardcoded values (such as saving them in SharedPreferences).

There seems to be no real documentation saying this (see the docs on resources), however you can test this yourself by recording the int value of R.id.hello_world (for example), then add a whole bunch of strings before and after it in the XML files, then rebuild it. The int value will be completely different.

Edit: I did find a bit of documentation you may want to read. It states: "you should never need to look there to discover a resource ID"; even though, in your case, you are not looking into this file, you are using a value directly from it.

Additionally, there is this answer which talks about how the IDs are generated, and it details that each generated ID has an order to it (package name, type name, resource name), but that these are generated arbitrarily. And this: "Note that aapt by default makes no attempt to keep these identifiers the same between builds."

You may also want to read through this slideshow for more details on AAPT and the Android Resource generation process.

Community
  • 1
  • 1
Eric
  • 63,873
  • 22
  • 120
  • 135