How to make a .jar out from an Android Studio project How to make a .jar out from an Android Studio project android android

How to make a .jar out from an Android Studio project


  • Open build.gradle for library projectenter image description here

  • Write two tasks in build.gradle -- deleteJar and createJar and add rule createJar.dependsOn(deleteJar, build)enter image description here

The code from above:

task deleteJar(type: Delete) {    delete 'libs/jars/logmanagementlib.jar'}           task createJar(type: Copy) {    from('build/intermediates/bundles/release/')    into('libs/jars/')    include('classes.jar')    rename('classes.jar', 'logmanagementlib.jar')}createJar.dependsOn(deleteJar, build)
  • Expand gradle panel from right and open all tasks under yourlibrary->others. You will see two new tasks there -- createJar and deleteJarenter image description here

  • Double click on createJarenter image description here

  • Once the task run successfully, get your generated jar from path mentioned in createJar task i.e. libs/xxxx.jarenter image description here

  • copy the newly generated jar into your required project's lib folder-->right click-->select "add as library"


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:

  1. File menu > New Module... > Java Library
  2. 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)
  3. In your Android code, set up a dependency on the new module so it can use the code in your new library:
  4. File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below:enter image description here
  5. 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


In the Android Studio IDE, access the "Run Anything bar" by:

CTRL+CTRL +gradle CreateFullJarRelease+ENTER

After that you'll find your artefact in this folder in your project
Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar


OR


Gradle has already a Task for that, in the gradle side-menu, under the other folder.

gradle toolbar

Then scroll down to createFullJarRelease and click it.

Gradle Tasks

After that you'll find your artefact in this folder in your project

Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar