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

dum4ll3 picture dum4ll3 · Feb 11, 2014 · Viewed 142.5k times · Source

I'm using AndroidStudio and I have this project as shown:

enter image description here

What is inside the blue circle is myLib. myLib also needs to use an external lib that is inside the red circle, and an apache package (green circle).

So I want to make this whole thing become a single .jar, so I can use it in another projects.

A step-by-step guide would be really appreciated, I'm a beginner in the developer world.

Thanks!

Answer

Abhinav Tyagi picture Abhinav Tyagi · Feb 16, 2016
  • Open build.gradle for library project enter 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 deleteJar enter image description here

  • Double click on createJar enter image description here

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

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