Reputation: 161
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?
Upvotes: 0
Views: 59
Reputation: 327
Also, what if the namespace was http://schemas.android.com/apk/res-auto?
Unfortunately if there is collision in resource there is no way to resolve that. Lets say you are using http://schemas.android.com/apk/res-auto
as your namespace. And you have foo
in libA
and in libB
. Then the behaviour will be uncertain because android will search foo
in all libraries and your custom views.
This is android guidline to avoid collisions:link
Upvotes: 0
Reputation: 93728
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.
Upvotes: 0