I am trying to figure out what it mean to "compile resources".
What I did in order to understand this issue:
I have read many articles about the subject but didn't find a simple answer. The best one I have read was this: How does the mapping between android resources and resources ID work?.
How I understand it:
From my understanding, when we compile our project either by ANT (Eclipse) or Gradle (AS). We use a tool called aapt - Android Asset Packaging Tool which: Is used to generate unique IDs for each of our resources, such as our layouts, our styles and more and store them in a lookup table. Then it persists this lookup table by generating two files:
- It Generates the R.java file with these unique IDs so we will be able to use our resources from our java code during compilation.
- It generate the resources.arsc file which can be found in resources*.ap_ file.
This resources.arsc file will later be packed by the apktool to the apk.
This arsc file format is a format that will be easily mapped and parsed by the device at runtime.
An Example:
So to make it simple: lets say I have this in my activity_main.xml:
<TextView android:id="@+id/my_textView"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And I call it from my onCreate using:
findViewById(R.id.my_textView)
In my R.java file I will see:
public static final int my_textView=0x7f08003f;
Using: aapt dump resources on the generated apk I can see it contains two lines with my_textView: ec resource 0x7f08003f com.example.lizi.liortest2:id/my_textView: flags=0x00000000 resource 0x7f08003f com.example.lizi.liortest2:id/my_textView: t=0x12 d=0x00000000 (s=0x0008 r=0x00)
What I don't understand:
I would have thought that this resources.arsc file will not just contain the resource ID but also all the properties I have defined for the view, such as android:layout_width="wrap_content".
So now during runtime when the VM tries to run findViewById(R.id.my_textView)
How does it know which view to get / its properties to create?
I simply can't understand how it works... Shouldn't this lookup table contain also the properties data? And what is this 0x7f08003f number? (Should it represent a value that will later be mapped to physical memory in which the object will be stored?)