how to create a jar file from android studio

10,441

Solution 1

Currently gradle based builds doesnt seems to be allowing to create jar with android studio, I decided to go for Intelij it has necessary option and does what i want , i can use community edition.

***Update**** Even the jar produced by Intelij with resources don't work , obviously its not easy to have a jar with resource , so decided to opt for .aar and hoping that Eclipse also gets support for .aar when gradle supports lands there, as that was my main concern.

Solution 2

I found a way to achieve this, plain simple, using Gradle 2.2.1:

task jar(type: Jar, dependsOn: 'assembleRelease') {
    from fileTree(dir: 'build/intermediates/classes/release')
}

Place this in your library module. It will compile it as release and produce a JAR file in: build/libs.

bash gradlew jar or use your IDE to target that jar Gradle task.

Solution 3

aar is not related to AS or eclipse but is an AndroidARchive for Android applications like JavaARchives are for java applications. Because Android is java based, jars can be used. But to take android specialities into account, like resource bundles, an aar is the right thing to use.

Share:
10,441
vishal dharankar
Author by

vishal dharankar

My code says all about me ! :P

Updated on June 17, 2022

Comments

  • vishal dharankar
    vishal dharankar almost 2 years

    I have a fairly latest version of android studio, I have created a module under a project which is basically supposed to be a library, when I build it, it creates an ".aar" file , what I want is .jar file as this library is supposed to be used with eclipse as well.

    The library contains activity as well, is there any way by which I can create a .jar file which I can use on Eclipse as well Android Studio ?

    I have already created this module and tried building , as a result it generated .aar file and not a .jar file.

    • Hanuman
      Hanuman almost 9 years
      see this link you may understand stackoverflow.com/questions/21712714/…
    • vishal dharankar
      vishal dharankar almost 9 years
      @Allu have seen that link the user wants to create jar for its java code and not entire module , in my case i have resources too
    • shkschneider
      shkschneider almost 9 years
      You cannot bundle resources in a JAR. That is what AAR files are here for.
  • shkschneider
    shkschneider almost 9 years
    Found a way that works for me (as of Gradle 2.2.1), see my new answer.
  • vishal dharankar
    vishal dharankar almost 9 years
    how about adding resources ? and some third party lib ?
  • shkschneider
    shkschneider almost 9 years
    JAR files cannot embed resources. That is why AAR files exists. And I guess third party libs are also JARs... That really answers your specific question IMHO. The rest would require AAR file (they do have a reason to exist).
  • vishal dharankar
    vishal dharankar almost 9 years
    I wish you have read my question carefully then :) i have an activity means resources too.
  • shkschneider
    shkschneider almost 9 years
    Impossible with à JAR file. Again, that is why AAR files came into place.
  • satur9nine
    satur9nine over 3 years
    As of AGP 4.0 the path is build/intermediates/javac/release/classes
  • Diamond
    Diamond about 2 years
    To use with maven publish gradle plugin, artifact(tasks["bundleReleaseClasses"].outputs.files.singleF‌​ile)