Android Jar libraries

How do you setup a project that can result in a jar library file that can be used for android? I want to create a custom library across all projects.

Few other questions:

  1. Does it need to be compiled against a specific version of android sdk?
  2. When an android package is compiled against a jar library does the classes necessary to work with the code get compiled with main code into the apk or does the entire jar get included?
  3. Any notable optimizations or pitfalls I need to know about with using a jar instead of integrating the code directly?
  4. Does the jar have to be signed like the apk needs to?

Google just release a new version of the SDK that handles Shared Libraries!

https://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

ADT 0.9.7 (May 2010)

Library projects:
The ADT Plugin now supports the use of library projects during development, a capability that lets you store shared Android application code and resources in a separate development project. You can then reference the library project from other Android projects and, at build time, the tools compile the shared code and resources as part of the dependent applications. More information about this feature is available in the Developing in Eclipse with ADT document. If you are not developing in Eclipse, SDK Tools r6 provides the equivalent library project support through the Ant build system.


There is nothing special you need to do to your project setup to create a jar file that will work in an Android app. Just create a normal Java project in Eclipse.

  1. It doesn't need to be compiled against Android at all. You can include any jar file as long as it doesn't reference classes that aren't included in Android. Just include your jar file in your build path of your Android projects in Eclipse, and it will automatically be included in your APK file.

  2. Not sure but I assume all classes in the jar file get included in the APK.

  3. I don't think just including some classes in a jar vs. in the project directly will make any difference in the resulting APK.