I am trying to figure out what it mean to "compile resources".
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?.
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:
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)
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?)
An ARSC file is an Android Resource table file that contains the application's list of resources in a table-format. This contains information such as Resource names, properties, and IDs. ARSC files are part of APK packages that are used to install these Android apps.
Decompile your Android ARSC using ARSC decompiler Just drag and drop your ARSC. Now count till three. Your ARSC is decompiled and is ready for download. Don't worry, decompilation quality will be best in class.
Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.
TL;DR: With the help of android asset packagin tool(aapt), xml nodes get translated to Java classes and the corresponding xml attributes get translated to numerical Ids. Android run-time works with these numeric ids to instantiate classes and create the views
Run this command to dump the binary xmlaapt d xmltree apk_file_name res/layout/activity_main.xml
(aapt can be found in android-sdk-dir/build-tools/23.0.2/aapt.exe)
This will show the xml nodes (e.g. LinearLayout
, RelativeLayout
, etc) with their attributes(e.g. android:layout_width, android:layout_height
) and their values. Note that, the constants match_parent
(numeric value 0xffffffff
or -1
) or wrap_content
(numeric value 0xfffffffe
or -2
) can be seen there.
As a matter of fact, you can use this command on any other xml files in the apk e.g. AndroidManifest.xml or other layout files
The apk file is just a zip archive containing all the java class files(classes.dex
), all the compiled resource files and a file named resources.arsc
. This resource.arsc
file contains all the meta-information about the resources. Some of those are...
LinearLayout
, RelativeLayout
, etc),android:layout_width
),id
's.The resource id
's refer to the real resources in the apk-file. The attributes are resolved to a value at runtime. The resolution process is smart about any re-direction (@dimen/...
as opposed to 4dp
or @color/...
as opposed to "#FFaabbcc"
) and returns a usable value(a dimen
value is resolved differently than a color
value).
Whats a compiled XML file: A compiled XML file is just the same XML file with the resource references changed to their corresponding ids
. For example, a reference @string/ok
will be replaced by 0x7f000001
. Moreover, the attributes from android
namespace is changed to their respective integer values(e.g. wrap_content
is changed to 0xfffffffe
or -2
)
How Android resolves resources at runtime: The methodinflater.inflate()
parses a compiled xml file and creates a view hierarchy by instantiating the xml nodes. Each of the xml nodes is instantiated by a java class(e.g. LinearLayout.java, RelativeLayout.java). To instantiate, the inflater parses the compiled xml file, collects all the attributes of a node and creates a packed structure of type AttributeSet
. This AttributeSet
is passed to the class constructor. The class constructor has the responsibility of walking the AttributeSet
and resolving each of the attribute values.
For example, for a layout containing RelativeLayout
, the inflater
will pack layout_width
and layout_height
into a AttributeSet
and pass it to the constructor
RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes).
In this case, some of the attributes are resolved by RelativeLayout.initFromAttributes(). The rest of the attributes are resolved by the parent ViewGroup.initFromAttributes(). The attribute android:id
of a view is just another attribute. After inflating, the inflater stores the id of each view by calling setId(id)
on that view after instantiation
Now to answer your question R.id
is a java array and R.id.my_textview
is an integer in that array. The id
of the view my_textview
is this integer(starts with 0x7f). The method findViewById()
does a depth-first search on that view hierarchy to find the respective view.
Hope this helps. The link you provided in your question already answers how the ids are generated by aapt.
Its a wonderful system of managing resources for devices with multiple dimensions of variations. Moreover, the implementation is really fast !! With this as the foundation, it allows to implement higher level functionality(e.g. Runtime Resource Overlay)
LayoutInflater inflate view by using XML strings. XML strings compiled into resource file as you mentioned in your question.
Please check these code snippets of AOSP:
public View inflate(int resource, ViewGroup root, boolean attachToRoot) { final Resources res = getContext().getResources(); if (DEBUG) { Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" (" + Integer.toHexString(resource) + ")"); } final XmlResourceParser parser = res.getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); } }
Resources.getLayout
loads XML resource parser
public XmlResourceParser getLayout(int id) throws NotFoundException { return loadXmlResourceParser(id, "layout"); } XmlResourceParser loadXmlResourceParser(int id, String type) throws NotFoundException { synchronized (mAccessLock) { TypedValue value = mTmpValue; if (value == null) { mTmpValue = value = new TypedValue(); } getValue(id, value, true); if (value.type == TypedValue.TYPE_STRING) { return loadXmlResourceParser(value.string.toString(), id, value.assetCookie, type); } throw new NotFoundException( "Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); } }
getValue
uses AssetManager's getResourceValue and it calls loadResourceValue
native method. This native method calls ResTable
's getResource method to get XML strings stored in resource file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With