0

Android R IDs are generated by the IDE (Android Studio or Eclipse ADT).

Are they identical between builds?

If yes, will they be the same if generated on different machines?

mrd
  • 4,561
  • 10
  • 54
  • 92
  • You shouldn't be concerned about this. You should **never refer** the id values **directly**. – Phantômaxx Mar 26 '15 at 22:45
  • I am asking because I need to store a reference to string resources in the sqlite for localisation. How can I achieve that? – mrd Mar 26 '15 at 22:48
  • 1
    Why do you need to do that? What is your end goal - perhaps there's a better way. – Nicklas Jensen Mar 26 '15 at 22:55
  • 1
    The end goal: I have objects, which have a property "name". These objects are persisted in the database. But in the UI their name needs to be translated to english or german. So, I would like to access their name like a string resource which can be localized. – mrd Mar 26 '15 at 22:57
  • Why don't you see for yourself? Print the value of a resource ID. Then add a couple new resources. Then print the value of that same resource ID again. You'll see that the values have changed. – Alex Lockwood Mar 26 '15 at 23:03
  • I don't want to make a test. I want to know for sure, the official, documented way. – mrd Mar 26 '15 at 23:04
  • Use reflection, for that. – Phantômaxx Mar 26 '15 at 23:07
  • The IDs that you refer to by `android.R.id.*` are constant. E.g. `android.R.id.list` will have the same value in API 22 as in API 4. This is not true for any ids defined in your project or libraries your project uses (`R.id.*`). – Eugen Pechanec Mar 26 '15 at 23:09
  • I recently answered a similar question. http://stackoverflow.com/a/29182221/2649012 replace "drawable" with "string". – Phantômaxx Mar 26 '15 at 23:10
  • 1
    @DerGolem great! The link in that post http://stackoverflow.com/questions/6945678/android-storing-r-drawable-ids-in-xml-array/6945827#6945827 is exactly what I was looking for! Thanks! This solves my problem. – mrd Mar 26 '15 at 23:22
  • It sounds like the objects are grouped, since their names can share a resource id (string). Instead of storing a reference to the name, you should instead store a group identifier, which you can resolve to a string (read: name) in your presentation layer. This way you can always add extra presentation pieces without updating your database. – Nicklas Jensen Mar 26 '15 at 23:52

1 Answers1

4

Firstly, Android IDs are not generated by the IDE but by aapt, a command line tool which is run during a build. Yes, the IDE ultimately triggers it, but it isn't strictly part of Android Studio, it's part of the build tools package.

The numbers are essentially assigned alphabetically, so the resource IDs will be identical for the same project - but not if you modify the resource file tree. I am only prepared to guarantee that on the same file system, operating system and version of build tools. If you change any of those, all bets are off.

Ultimately, relying on this would be a really bad idea, because someone will break it by modifying the project one day, probably sooner rather than later.

I looked into this fairly extensively some years ago when I wrote an APK decompiler for the purposes of patching in resources.

Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75