Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Understand resource-id in R.java

Does anybody know anything about the internals of resource-id and R.java, or could at least reference me to read some more about it?

What is the meaning of the resource-ids? Are they some kind of addresses in the apk? Where are they saved in the compiled apk?

I tried looking some info about it, but most of the things I get are issues with importing it and what it does and in general.

Why I am asking these questions: I recently tried to dynamically load an apk that uses a resource here and looking at this I believe it is not even possible. I wanted to understand why.

like image 387
Ravit D Avatar asked Mar 02 '14 07:03

Ravit D


People also ask

How do I find my resource id Android?

int resourceId = this. getResources(). getIdentifier("nameOfResource", "id", this.

What is resources and R Java in Android?

Android R. java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. If you create any component in the activity_main. xml file, id for the corresponding component is automatically created in this file.

What is the use of resource id in Android?

When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.


1 Answers

AFAIK

  • Android assigns ID for every resource.

  • The gen folder in a project contains the R.java references file which contains these generated values. These references are static integer values.

  • If you add a new resource file, the corresponding reference is automatically created in a R.java file.

  • Manual changes in the R.java file are not necessary and will be overwritten by the tooling.

    The Android system provides methods to access the corresponding resource files via these IDs.

For example, to access a String with the R.string.yourString ID in your source code, you would use following method defined on the Context class.

getString(R.string.yourString) 

I hope this helps a little

like image 196
Durai Amuthan.H Avatar answered Sep 27 '22 19:09

Durai Amuthan.H