35

I've been trying to make an android library project, and while the build process works fine, I've been running into some trouble with replacing a resource in the project which uses the library.

In my library I have:

  1. A library_layout.xml containing

    <TextView  
        android:id="@+id/str_my_string"  
        android:layout_width="wrap_content"  
        android:layout_text="wrap_content">
    
  2. A java file which calls

    ((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
    
  3. A resource strings.xml containing

    <string name="my_string">Placeholder</string>
    

In the project using the library I have

  1. A resource strings.xml containing

    <string name="my_string">Actual string content</string>
    

The behavior I expect is that when I run the project using the library, the text view displays Actual string content, but it actually contains false.

Looking in the app which uses the library, I do see two R files, and both of them have R.string.my_string and both of those are equal to the same numeric value.

wittich
  • 2,079
  • 2
  • 27
  • 50
jwriteclub
  • 1,604
  • 1
  • 17
  • 33

2 Answers2

74

I have the same arrangement and this works for me as expected.

The library has layout/class with this reference to a string resource:

<TextView android:id="@+id/studentSinceLabel">

The library provides a default value in its strings.xml:

<string name="studentSinceLabel">Student Since</string>

The main app has this value in its strings.xml:

<string name="studentSinceLabel">Client Since</string>

When I give a value for this resource in the main apps strings.xml, I see "Client Since" when the app runs, when I delete it from the main apps strings.xml, I see the value from the library, "Student Since".

Seems this is expected behaviour based on my reading here: http://developer.android.com/tools/sdk/eclipse-adt.html

Relevant quote from the link above:

In the cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefines any resource behaviours or values that are defined in any library.

hannojg
  • 983
  • 8
  • 28
R Miller
  • 756
  • 6
  • 2
  • So it seems like I was simply having problems with Eclipse not cleaning out old cruft. – jwriteclub Apr 12 '11 at 20:39
  • 11
    This is exact, but it will fail if the string is translated in the library. The translated version in lib override the using-app version (translated or not). So lesson learnt: do not translate a placeholder string. – johan d Mar 18 '15 at 11:41
  • 1
    @johand. for libraries that uses translated string resource, any way to override it in our project? – Bruce May 03 '16 at 00:15
  • 2
    Actually I have found the solution for translated text, just override each of the translated versions as well. For my case I have the same resource id in values/strings.xml and values-en/strings.xml for english language to show my overridden text, cheers. – Bruce May 03 '16 at 12:52
  • In my case overriding the default resource doesn't work. My project structure is more complex, multiple modules - I have an App that uses Module A, which depends on a third-party Module B. In Module A I want to override any resources with the same name from Module B. For instance, I have defined my color "blue", however what I see at runtime is the shade of blue defined in Module B (3rd party). No translations. Any idea about such a setup? – Stan Jul 04 '16 at 11:40
2

Just had this error, here is what happened in my case :

If you are using multiple strings.xml file for different screen sizes in your library, your app might use the library file.

I.E, if you have :

LIBRARY :  
    res/values/strings.xml  
    res/values-sw600dp/strings.xml

and :

APP :
    res/values/strings.xml

And you try to launch your app on a 600dp screen size (i.e a tablet), your app is going to get the strings.xml(sw600dp) from your library.
To avoid this, you might want to create a strings.xml specific to sw600dp resolutions in your app, that contains the same strings values than in your library.

Mathieu
  • 1,435
  • 3
  • 16
  • 35