Create an Android Jar Library for Distribution

How to create jar for Android Library Project

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.

Create a JAR for Android Library Distribution

I don't want to distribute the source code as it contains details on posting to my web server.

Bear in mind that anyone who wants to can get that data out of the JAR.

It must be possible as other companies provide JAR files for libraries that can be included.

AFAIK, this recipe still works:

  1. Create an Android library project, with your source code, resources, and such, and get it working

  2. Compile the Java source (e.g., via Ant) and turn it into a JAR file

  3. Create a copy of your original Android library project to serve as a distribution Android library project

  4. Place the compiled JAR from step #2 and put it in libs/ of the distribution library project from step #3.

  5. Delete everything in src/ of the distribution library project (but leave the now-empty src/ directory there)

  6. Distribute the distribution library project (e.g., ZIP it up)

This effectively gives you what you see with the Play Services SDK -- a library project with no source code, but instead a JAR in libs/, along with the resources and such.

I will be reconfiming this recipe tomorrow and will try to remember to update this answer if I find that it needs adjusting for the current crop of tools.

And the new Gradle-based build system supports the AAR package for distributing libraries and such, though I have not played with this yet.


UPDATE

This recipe works, so long as the library project does not itself have dependencies upon another JAR or library project. In those cases, things seem to get messed up in the build process -- everything can compile, but class references from the dependencies cannot be resolved at runtime.

What is the best way to create Android library project and use it as a jar file in another project using Android Studio

Finally with the help of few post from statckoverflow and by doing trial and error method I am able to get the solution to my question. Here are steps

a) How to create a Android library project

step 01> In Android Studio go to File -> New -> New Module

step 02> Select Android Library and click on "Next" button
Sample Image

step 03> Choose minimum SDK supported and click on "Finish".
Sample Image

step 04> A new Module will be added and wait until the gradle sync. Now your library file is created

b) How to export that Android library file as .jar file

step 01> After completion of stage "a" you need to build the project once and you are done . The .aar file will be created in the path of your Android app -> Module Folder -> build -> outputs -> aar -> module-release.aar

step 02> You just need to copy that module-release.aar and distribute it where ever you want.

c) Steps to integrate that .jar file to Another android app

step 01> Here the library type is in .aar format . The steps are same for both .jar file and .aar file. In Android Studio go to File -> New -> New Module

step 02> In that New Module window select "import .JAR/.AAR Package" and press "Next" Button
Sample Image

step 03> A new file browser window will open . Here in "File name" section choose file browse and open your module-release.aar file and click on "Finish"

step 04> After successful build copy following compile statement into Your "App module gradle file"

dependencies {
compile project(path: ':module-release')
}

step 05> Sync the gradle file.

step 06> If any, copy all permission given to your library file to your App Manifest.xml. Thats all you are done

d) And how to call a method of that .jar file from that android app

step 01> In your module you need to define a public method which can be accessible using class name . For example

public final class MyAPI {
public static int SumOf(int a, int b){
return a+b;
}
}

step 02> And from your Host App you can call the SumOf method using

MyAPI.SumOf(2,4);

Android library project as jar file for distribution, like google analytics

The android.jar will be located where you installed your Android SDK. Under the platforms directory there should be a number of other directories named android-<version>. The android.jar will be there. Choose the one specific to the minimum android version you are targeting.

Once you have that, copy it into your project. If you're using eclipse I think you can just cut and paste jars straight into your project, right click and add it to build path. If you're not using eclipse or any other IDE, you just need to ensure that the android.jar is on the classpath when building your jar file.

After that your newly built android library can be dropped into any Android project.

In answer to your additional questions:

  1. What they mean by a true library is a jar file as opposed to an Android library project.

  2. I don't think there's anything wrong with the way you created the jar file. I would have made it using the android.jar as I mentioned above but your way should also work. To verify this I would examine the jar contents and make sure all you have in there is .class files.



Related Topics



Leave a reply



Submit