How to reference a string in the strings.xml file of an Android library in code?

To refer to the string foo in the strings.xml file (in res \ values) of the application project, you can simply use

getString(R.string.foo) 

getString is a context method.

Suppose the Android library has a string foo in the strings.xml file. How can it be used in a library method?

Edited: It was suggested to pass the context link to the library method so that getString () could be used. Since this is the context of the application project, there is a potential conflict that can be illustrated as follows:

Suppose that: There is a string foo in the library with a value of = "library foo". The application project has the string foo with the value = "app foo"

Following code

 Log.d("Debug", "App foo ID: " + R.string.foo); Log.d("Debug", "App: foo value: " + getString(R.string.foo)); 

generates:

 03-22 05:53:55.590: D/Debug(16719): App foo ID: 2131230723 03-22 05:53:55.590: D/Debug(16719): App foo value: app foo 

In the library method, the following code

 Log.d("Debug", "Library foo ID: " + R.string.foo); Log.d("Debug", "Library foo value: " + context.getString(com.my.library.R.string.foo)); 

generates:

 03-22 05:55:03.680: D/Debug(16719): Library foo ID: 2131230723 03-22 05:55:03.680: D/Debug(16719): Library foo value: app foo 

The above shows that the ID conflict causes an erroneous string value.

+7
source share
2 answers

As you indicated for the link to the string resource defined in your library, you can use the getString () method, you only need the context, but in your example the conflict is generated by the same string name.

When you define a resource in your library, you have a conflict if you use the same resource name in the application module. To avoid conflicts, you must use unique names, for example, using a common prefix for resource names in the library, a single package name is not enough.

To solve the problem, you can use the following names:

  • mylib_foo in your library
  • foo in your application

Android documentation source: http://developer.android.com/tools/projects/index.html#considerations

As your library modules and dependent applications grow, keep the following points:

  • Resource conflicts

    Since the tools combine the resources of the library module with the resources of the dependent module of the application, this resource identifier can be defined in both modules. In this case, the tools select the resource from the application or library with the highest priority and discard another resource. When you develop your applications, keep in mind that the identifiers of shared resources are likely to be defined in more than one project and will be combined with the resource from the application or the priority library takes precedence.

  • Use prefixes to avoid resource conflicts

    To avoid resource conflicts for shared resource identifiers, consider using a prefix or other sequential naming scheme that is unique to a module (or unique to all modules in a project).

+7
source

A library project should be able to reference its resources in exactly the same way.

Is there a specific usage / code example that illustrates that this does not work?

Any lines defined in the parent project will overwrite the library. Even actionbarsherlock uses the ex prefix. abs__action_bar_home_description

+4
source

All Articles