Multiple library projects declaring the same custom attribute name causes conflict?

3,787 views
Skip to first unread message

Kiran Rao

unread,
Mar 6, 2013, 8:36:51 AM3/6/13
to adt...@googlegroups.com
I have 2 library projects (say lib1 and lib2) and an app project (say awesomeapp)

lib1 defines a custom attribute

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="First">
        <attr name="foo" format="string"/>
    </declare-styleable>
</resources>

lib2 also defines a custom attribute, which is unfortunately also named foo

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="Second">
        <attr name="foo" format="string"/>
    </declare-styleable>
</resources>


When building the awesomeapp project, I see an error similar to the following in the console:

lib2/res/values/attrs.xml:31: error: Attribute "foo" has already been defined

Question:

How do I resolve such conflicts in library projects? Do note that they are using different "names" for declare-styleable .. so I presumed that this wouldn't cause a conflict.
Is there a way I can modify the attrs.xml of the individual lib projects to qualify the custom attribute names with a namespace?

Tor Norbye

unread,
Mar 6, 2013, 10:48:15 PM3/6/13
to adt...@googlegroups.com
Just FYI; I believe it's the case at least for the platform attributes that they are all unique. The "orientation" attribute used by LinearLayout is the same as the one used for GridLayout; the gravity attribute used in one view is the same one as used in any other view; meaning these all have the same type and/or flag or enum values everywhere.

-- Tor



--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kiran Rao

unread,
Mar 7, 2013, 1:17:59 AM3/7/13
to adt...@googlegroups.com
@Tor,

I guess the platform attributes must be declared once and used everywhere then.

I looked into attrs.xml for the support version of GridLayout and I see some of the android: namespaced attributes being re-defined.I don't see how that can help me though.

One solution I can think of is to create a library project just to define the attrs and then use it in all the other library projects, but before I attempt that I just want to be sure that there is no way I can solve this by using some XMLNS trick. Any pointer to what files in AOSP I should look into for a clue would be appreciated!

Tor Norbye

unread,
Mar 7, 2013, 1:20:07 AM3/7/13
to adt...@googlegroups.com
I'm not super familiar with how that works, so I'll ask around and get back to you.

-- Tor

Xavier Ducrohet

unread,
Mar 7, 2013, 2:16:03 AM3/7/13
to adt...@googlegroups.com
There is no namespace trick because for the purpose of your app, all the resources are merged together in a single pool in the same namespace.

On Wed, Mar 6, 2013 at 10:17 PM, Kiran Rao <techie....@gmail.com> wrote:



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Kiran Rao

unread,
Mar 8, 2013, 4:02:23 AM3/8/13
to adt...@googlegroups.com
@Xavier,

Thanks for the clarification. Fortunately for me, I am in a position to modify the individual library projects (both the attrs.xml files and the Custom views which use those attributes). I was looking to avoid this but I can do this as a last resort.

Is there any plan to make it possible for multiple library projects to have custom attributes with the same name? It is not all that rare for an app to depend on a handful of library projects. The scope for duplication of attribute names is not small.

Xavier Ducrohet

unread,
Mar 8, 2013, 2:03:22 PM3/8/13
to adt...@googlegroups.com
that would require a change on the platform side, and I'm not sure about the plan there.

We recommend devs to use different prefixes for the resources in each library.

Kiran Rao

unread,
Mar 11, 2013, 1:15:25 AM3/11/13
to adt...@googlegroups.com
Ah okay. The prefix point makes sense. Thanks!

blurkidi

unread,
Dec 7, 2013, 11:25:53 AM12/7/13
to adt...@googlegroups.com
I recommend reusing the attributes already specified in the android system to avoid conflicts. Often, there is already an attribute of the type that you need and which name makes sense. Your example might be something like this:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="First">
        <attr name="android:name"/>
    </declare-styleable>
</resources>

and

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="Second">
        <attr name="android:name"/>
    </declare-styleable>
</resources>

Then retrieve to the attributes with 'R.styleable.First_android_name' and 'R.styleable.Second_android_name' respectively.

If you can not use this method, prefixing is a good solution. And I agree that it could be really nice to have the possibility to define attributes in a custom context to avoid conflicts. 
Reply all
Reply to author
Forward
0 new messages