How to create jar for Android Library Project

AndroidJarAndroid Library

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

Follow the below steps to obtain a distributable jar of your library (verified for library project without external dependencies):

  1. Create your library project. Clean it. You will get a libraryProjectName.jar file inside the bin folder. (Make sure "is Library" is selected in the library project.
  2. Copy the .jar file into the libs folder of your target application
  3. Refresh your workspace and press Ctr+Shift+O (on Windwows/Linux) or Cmd+Shift+O (on Mac) to resolve any import dependencies from the library added.

Comment in case facing any issue.

Solution 4 - Android

You cannot package resource files into jar. You can export your project as a library project.

You export your utility classes that do not refer to resources in android ie pure java classes as jar file.

Check the link below

http://developer.android.com/tools/projects/index.html

To make your project a library project

Right click on your project. Go to properties. Choose Android. Make sure Is Library is checked.

To refer the library project in your android project

Right click on your project. Go to properties. Choose Android. Click Add. Browse and the library project.

Quoting from the docs

A library project differs from a standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

Solution 5 - Android

Tested with adt 22.2.1

  1. Create your library project
  2. Create empty project
  3. Add library project to your empty project
  4. Build project
  5. In /gen folder you will find class for your library project, this will contain your resources for lib project.
  6. Export JAR (select only this class ... from /gen folder)
  7. Export JAR from your lib project
  8. Rename exported files to zip and merge content
  9. Rename the final file as JAR and add it to project.

Solution 6 - Android

Here is an easy way to do it with ant, found this in the Volley sourcecode. Add the code below to custom_rules.xml then build with ant jar

<?xml version="1.0" encoding="UTF-8"?>
<project name="volley-rules" default="help">

  <!-- A rule to generate the JAR for inclusion in an Android
       application. Output file will be bin/volley.jar -->
  <target name="jar" depends="-compile">
    <jar destfile="bin/volley.jar"
         basedir="bin/classes" />
  </target>
</project>

Solution 7 - Android

As per latest Android Studio v1.2, its as easy as having a cake. Already answered complete solution with screenshots on this link

Solution 8 - Android

With Eclipse ADT v22.6.2 you can just export jar by clicking File->Export->Java->Jar File, select directories and destination where to save.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAayush RanaView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidAayush RanaView Answer on Stackoverflow
Solution 3 - AndroidAkshat SinghalView Answer on Stackoverflow
Solution 4 - AndroidRaghunandanView Answer on Stackoverflow
Solution 5 - AndroidcoltbgView Answer on Stackoverflow
Solution 6 - AndroidNathan SchwermannView Answer on Stackoverflow
Solution 7 - AndroidNishant SrivastavaView Answer on Stackoverflow
Solution 8 - AndroidsomeUserView Answer on Stackoverflow