Accessing application resources from the library project

前端 未结 3 1956
误落风尘
误落风尘 2020-12-06 01:23

My application depends on a library project.

The menu.xml file is within the application project.
All the java code is within the library projec

3条回答
  •  旧巷少年郎
    2020-12-06 02:17

    I found @triad's solution with Resources.getIdentifier(String, String, String) to be somewhat error-prone:

    • the String-literal resource identifiers aren't checked by the IDE
    • multiple sequential String arguments to a single method are easy to use incorrectly.

    I found this approach to work better for me:

    String libString = context.getString(example.library.pkg.R.string.library_string)
    

    Where the library's package is example.library.pkg.

    • The library's R class is resolved at compile-time, so your IDE will tell you if you referenced it correctly
    • Not importing the library's R class allows you to still use your own local R later, and explicitly marking the external resource usages makes them easier to spot.

提交回复
热议问题