Android Library projects and Jars
Android Library projects are really great for code reuse. But sometimes you want to create a library, that has resources in it, but you don't want to ship source code with it. It's possible to do this, but a little tricky.
This article has a great overview of how to use an Ant build.xml file to create a jar. But it leaves one with the impression that an Android Library project requires you to ship source with the library.
AndParcel has some really detailed information on Android Libraries and the issues involved in using a JAR file to distribute object code, and the relationship to resources. For example, the article on JAR files suggests users of your library will need to unpack resources into their projects:
The people reusing your code will need to not only add the JAR to their projects, but also unpack those shipped resources (in their respective resource sets) and put them in their projects as well.
Neither of these mentions another easy way to distribute Android Library projects with a JAR file, and resources. That is:
- Create an Android Library project that contains only resources. Users add the resources with the Android properties panel Library reference panel.
- Create an Android project that contains only code, and whose end product is a JAR. Users just add the JAR to their project build path.
If the code in the JAR references the resources, the best solution is to use context.getResources().getIdentifier() to look up the resource ids. This is slower than a direct reference to the id, but it prevents issues where the number of the id in the library's version of R.java is different than the number of the id in the application's version of R.java.