Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jar for Android Library Project

I have to create a library that I am going to export to the client in a jar file. Is there any way to create a jar with the resources in it?

The Google adMob have such a jar, which includes resource file such as R$layout.class in it. This page describes some way to do that but I am not able to understand exactly how am I supposed to import the library project to an application using the method above.

like image 879
Aayush Rana Avatar asked Jun 12 '13 11:06

Aayush Rana


People also ask

Can I use Android library in Java?

An Android library module can contain Java classes, Android components and resources. Only assets are not supported. The code and resources of the library project are compiled and packaged together with the application.

What is a JAR file in Android?

A JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform.


2 Answers

This is the closest that you can get:

Step #1: Create a regular Android library project, and get it working.

Step #2: Copy that Android library project into another directory.

Step #3: Create a JAR from the compiled Java classes from the original Android library project, and put that JAR in the libs/ directory of the copy you made in Step #2. You should be able to run ProGuard on this JAR manually, though I haven't tried that.

Step #4: Remove everything inside the src/ directory of the copied library project, leaving behind and empty src/ directory.

Step #5: ZIP up or otherwise distribute the copied Android library project.

This will give you an Android library project like the Play Services one, where the resources are available, but the source code is not.

UPDATE: An even better approach nowadays is to package your library project as an AAR, which is Android's native way of creating a reusable artifact from your compiled code plus required resources. At the present time (July 2014), Gradle for Android, Android Studio, and the android-maven-plugin can create and consume AARs.

like image 114
CommonsWare Avatar answered Oct 09 '22 06:10

CommonsWare


This works!

  1. Make your library project a normal project by deselecting IsLibrary flag.
  2. Execute your project as Android Application. (It will not show any error)
  3. You'll find a .jar file in bin folder along with .apk.
  4. Give you .jar who want to use your library.
  5. Tell them to just import external jar into build path.

This will work fine with all the resources. Thanking you all. for your responce

like image 22
Aayush Rana Avatar answered Oct 09 '22 06:10

Aayush Rana