views:

1047

answers:

2

Is it possible to create a custom library in android (having its own layout resources) for use across several android applications?

  • I created a regular *.jar file but when I tried to create/style my views dynamically most of the properties do not work. Even referencing simple styles from the android.jar file such as android.attr.listSeparatorTextViewStyle did not work.

  • I created an android project without any Activity, having its own resource files and then referenced this project from another Android project to use in its build path. Everything seems to work fine but the emulator keeps crashing (with no meaningful error message in the LogCat) when I try to run the project.

Am I missing something?

A: 

Is it possible to create a custom library in android (having its own layout resources) for use across several android applications?

No. JARs cannot have resources. You can share code this way, but resources have to reside in the applications reusing the JARs.

Even referencing simple styles from the android.jar file such as android.attr.listSeparatorTextViewStyle did not work.

Make sure you are linking your JAR against android.jar. You can see many examples of this in the CWAC components on my github page.

CommonsWare
+1  A: 

Well, I think there is a way to do it (although I didn't try it myself), but it requires the users of your library having to build their APKs manually via aapt.

The problem is this: The JAR you're exporting contains the R class file which holds the resource IDs of your library. So, any application can access these IDs by simply linking your JAR. However, that's not enough, because by default, only resources in the app's res/ folder are bundled with the APK. Hence, you have to create the APK yourself using aapt, telling it to also include any resources from your library. Check the aapt tool help for more information.

Since this implies that users of your library have to introduce a manual build process (or at least modify it), it makes for a poor experience.

Matthias