In an Android XML layout file, we can specify multiple unique namespaces, like so:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom-namespace-one="http://namespace-one"
xmlns:custom-namespace-two="http://namespace-two"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
Then, in child elements, we can specify attributes and prefix them with a namespace:
<TextView
android:id="@+id/lessens_listview_textview"
custom-namespace-one:foo="value1"
custom-namespace-one:foo="value2"/>
I understand the purpose overall of XML namespaces, which is to ensure that foo from custom-namespace-one is distinguishable from foo from custom-namespace-two. What I don't understand is how these namespaces are used when compiling and linking the resources.
Say for example that custom-namespace-one:foo refers to a certain attribute in library A, and custom-namespace-two:foo refers to another attribute in library B.
Are the namespaces used to find the specific attributes? It doesn't look like it, as they're always used when consuming the attributes from a library, never in their actual definition. So then, how does aapt know which resource to use?
Also, what if the namespace was http://schemas.android.com/apk/res-auto? How does that relate to the fact that the attributes should be defined in the current package?
They're used to define attributes. They specify the file to look for the attribute definitions in. http://schemas.android.com/apk/res/android is the Android SDK's built in definitions. "http://schemas.android.com/apk/res-auto" is the list of attribute defined by your application (including libraries you depend on). You can define any others you want, but typically an app only uses these two.
The namespace is used to qualify what attribute you're using. Let's say you have namespace A and B. Both define an attribute "foo". If you were to specify foo without a namespace, it would be impossible to know what foo to use. Using namespaces avoids that problem.