Home » Android » why , resources in android are converted into HEXA-DECIMAL inside R.java

why , resources in android are converted into HEXA-DECIMAL inside R.java

Posted by: admin June 16, 2020 Leave a comment

Questions:

why , the resources are converted into HEXAdecimal , does it create some fast access to your resources for app .

How to&Answers:

R.java is nothing more than a class file with a static constant for each resource in your project. The constant is an index number into what is effectively a file system. So myicon.png is given a file number of 12345. The resource manager uses this index to load the resource at run time. Open it up. Take a look.

Let’s look at an example R.java:

public final class R {
    public static final class id {
        public static final int myTextView=0x7f090008;
    } 
}

I can reference myTextview using:

findViewById(R.id.myTextView) - constant
findViewById(0x7f090008) - hex
findViewById(2131296264) - decimal
findViewById(017702200010) - octal
findViewById(0b1111111000010010000000000001000) - binary

They are all equivalent.

Equally, I could code my R.java file like this:

public final class R {
    public static final class id {
        public static final int myTextView=0b1111111000010010000000000001000;
    } 
}

It still works.

Nothing is “converted”, you are just using a constant to reference the index number. The compiler takes care of it. You just happen to be viewing it as hexadecimal but ultimately, as is everything in your app, it’s just ones and zeros.

The only reason for doing this is so that you can use the constants. Imagine maintaining your code if you had to use the actual index values, especially given that they may, and will change every time you rebuild R.java.

The index numbers are not memory addresses, or offsets, or a special kind of cheese. They are simply constants, generated by the compiler, to enable you to access resources using human friendly names.

Answer:

The R.java file is generated by the Android Resource Manager Android asset manager packer(aapt.exe) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format.

Answer:

It’s kind of architectural from android perspective. when you develop the Android application and just build the code(not installed on the device/emulator) at that time the resource manager allocate the unique address to all the resources. Once the application is installed on the device the actual memory allocation will happen and this file will help Android to map the actual memory required for it.

Hope this answer your question