20

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?

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
Tawani
  • 11,067
  • 20
  • 82
  • 106

4 Answers4

17

Might be late to reply but found this which is interesting. Setting up a Library Project

An Android library project is a development project that holds shared Android source code and resources. Other Android application projects can reference the library project and, at build time, include its compiled sources in their .apk files. Multiple application projects can reference the same library project and any single application project can reference multiple library projects.

If you have source code and resources that are common to multiple application projects, you can move them to a library project so that it is easier to maintain across applications and versions. Here are some common scenarios in which you could make use of library projects:

Sachin Chavan
  • 5,578
  • 5
  • 49
  • 75
13

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.

mxk
  • 43,056
  • 28
  • 105
  • 132
  • 3
    Everyone, I posted this before Android Library projects became available. Check out Sachin's answer below for how this is done today. – mxk May 31 '12 at 13:52
5

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
  • 986,068
  • 189
  • 2,389
  • 2,491
  • No longer the case with the newer build tools. – greg7gkb Jun 01 '12 at 01:00
  • 3
    @greg7gkb: Yes, it is still the case, as of R19, the current build tool. JARs still cannot have resources. *Android library projects* can have resources. At the present time, while JARs are internally created based on those library projects during the build process, those JARs cannot be used independently of the library project. This feature is still under development -- once completed, we will be able to create JARs from Android library projects and distribute those JARs without the library project itself. This probably is not in R20, but hopefully will show up later in 2012. – CommonsWare Jun 01 '12 at 11:00
0

Is this still true with R21? I am able to export a library project into jar file and used in other customer projects successfully, both code wise and accessing assets file wise.

yet http://developer.android.com/tools/projects/index.html still has the following statement which seems not true anymore:

You cannot export a library project to a JAR file A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

user443461
  • 41
  • 6