How to make .jar from an Android Studio project

I am using AndroidStudio, and I have this project as shown below:

enter image description here

What is inside the blue circle is myLib. myLib should also use an external library located inside the red circle and the apache package (green circle).

So, I want all this to become a single .jar, so I can use it in other projects.

The walkthrough will be really appreciated, I'm new to the world of developers.

Thank!

+93
java android android-studio jar
Feb 11 '14 at 20:49
source share
8 answers
  • Open build.gradle for a library project enter image description here

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

Code 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 the Gradle panel on the right and open all the tasks in yourlibrary-> others. There you will see two new tasks - createJar and deleteJar enter image description here

  • Double click on createJar enter image description here

  • After successful completion of the task, get the generated jar file in the path specified in the createJar task, i.e. libs / xxxx.jar enter image description here

  • copy the generated jar file to the desired project lib folder -> right click -> select "add as library"

+128
Feb 16 '16 at 11:31
source share

If you configured the code as a simple Java module in Gradle, it is really easy to have Gradle provide you with a jar file with the contents. This jar file will only have your code, not other Apache libraries. I would recommend distributing it this way; linking dependencies inside your library is a little strange, and it is more common for users of these libraries to include these dependencies on their own (since otherwise conflicts between these projects already link copies of the library, possibly of different versions). What's more, you avoid potential licensing issues when redistributing code to other people if you want to publish your library.

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

  • Menu > New module ... > Java library
  • Configure the library, Java package name, and class names in the wizard. (If you do not want it to create a class for you, you can simply delete it after creating the module)
  • In your Android code, configure the dependency on the new module so that 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
  • Select your module from the list in the dialog box that appears: enter image description here

I hope your project should be built normally. After creating the assembly, the jar file for your Java library will be placed in the build / libs directory in your modules directory. If you want to create the jar file manually, you can run its jar task from the Gradle file: enter image description here

+56
Feb 11
source share

Gradle already has a Task for this under other

enter image description here

Then scroll down to createFullJarRelease and click on it.

Gradle tasks

After that, you will find your artifact in this folder in your project

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

+17
Oct 06 '18 at 16:56
source share

Just add this to your build.gradle java module. It will contain dependent libraries in the archive.

 mainClassName = "com.company.application.Main" jar { manifest { attributes "Main-Class": "$mainClassName" } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } 

As a result, the file [module_name] / build / libs / [module_name] .jar appears.

+9
Nov 26 '16 at 4:41
source share

the way i found is to find the output of the project compiler (project structure> project). then find the correct folder of the module that you want to turn into a jar, compress it with zip and change the output extension from zip to jar.

0
Jul 17 '17 at 9:37 on
source share

A .jar file will be automatically generated when you compile and run your application.

You can find the class.jar file from root_folder / app / build / intermediates / bundles / debug

jar file location

0
Nov 23 '17 at 8:11
source share
 task deleteJar(type: Delete) { delete 'libs/mylibrary.jar' } task exportjar(type: Copy) { from('build/intermediates/compile_library_classes/release/') into('libs/') include('classes.jar') rename('classes.jar', 'mylibrary.jar') } exportjar.dependsOn(deleteJar, build) 
0
May 28 '19 at 10:00
source share

If you run the Gradle task from AndroidStudio

 assembleRelease 

or through the terminal

 ./gradlew <moduleName>:assembleRelease 

then you can find .aar in <modulePath>/build/outputs/aar/<Name>.aar

After that, you can perform additional operations (for example, create .jar ) using command line tools.

0
Jul 01 '19 at 14:20
source share



All Articles