Generating Jar file in Android Studio

11,220

Solution 1

Here is an example of Gradle task which creates .jar from classes.jar.

classes.jar file can be found in /build/intermediates/exploded-aar/ folder.

Edit: Create jar file for every Android Library project and distribute them standalone. What about dependencies required by library projects - add those dependencies in final project, where your library will be used.

For example: you Android Library project MyLibProject, which has following dependencies in build.gradle

dependencies {
    compile "com.android.support:support-v4:19.+"
}

When you'll build your project you'll get classes.jar for MyLibProject, and those classes.jar will contain just class from library project, but not from com.android.support:support-v4:19.+.

After that in another project you add classes.jar as library dependency in build.gradle and also define same dependencies as in MyLibProject.

And if you still need fatJar, look here how to create fatJar with Gradle.

Solution 2

If you set up the code as a plain Java module in Gradle, then it's really easy to have Gradle give you a jar file with the contents. That jar file will have only your code, not the other Apache libraries it depends on. I'd recommend distributing it this way; it's a little weird to bundle dependencies inside your library, and it's more normal for users of those libraries to have to include those dependencies on their own (because otherwise there are collisions of those projects are already linking copies of the library, perhaps of different versions). What's more, you avoid potential licensing problems around redistributing other people's code if you were to publish your library.

Take the code that also needs to be compiled to a jar, and move it to a separate plain Java module in Android Studio:

File menu > New Module... > Java Library
Set up the library, Java package name, and class names in the wizard. (If you don't want it to create a class for you, you can just delete it once the module is created)
In your Android code, set up a dependency on the new module so it can use the code in your new library:
File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below: 

enter image description here

Choose your module from the list in the dialog that comes up:

enter image description here

Hopefully your project should be building normally now. After you do a build, a jar file for your Java library will be placed in the build/libs directory in your module's directory. If you want to build the jar file by hand, you can run its jar build file task from the Gradle window:

enter image description here

Oroginal article here : How to make a .jar out from an Android Studio project

Share:
11,220
user2498079
Author by

user2498079

Updated on June 11, 2022

Comments

  • user2498079
    user2498079 almost 2 years

    I have a library project in Android Studio. I want to generate a .jar file for this library. I need the jar file contain/include all the dependencies this Library project has.

    The problem is I couldn't find a way of generating a jar file. Is there any way I can generate a jar file for my project?

    Best Regards