Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 948
dum4ll3 Avatar asked Feb 11 '14 20:02

dum4ll3


People also ask

Where is JAR file in Android project?

If you are unable to find the libs folder in Android studio then open your android project in “Project” mode If the project is already opened in the “Android” mode. Then go to Your Project Name > app > libs and right-click on it and paste the downloaded JAR files.

Can Android execute jar?

You cannot directly run jar files in android as android runs on Dalvik Virtual Machine , whereas to run jar files you will need Java Runtime Environment which has Java Virtual Machine .


1 Answers

  • 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"

like image 173
Abhinav Tyagi Avatar answered Oct 06 '22 09:10

Abhinav Tyagi