Search code examples
androidr.java-file

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


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


Solution

  • 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.