111

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!

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
dum4ll3
  • 1,417
  • 2
  • 12
  • 17
  • 1
    I think what you want is a aar file (Android archive) that one can be linked quiet simple as dependency. Basically you need to create a Android library project. But I cannot write the exact steps from mind. – rekire Feb 11 '14 at 20:55
  • 3
    I need a .jar file so I can use in any java project. Not only android's. – dum4ll3 Feb 11 '14 at 20:58
  • 1
    Must be almost the same - make a library project and the export it. – Thrash Bean Feb 11 '14 at 21:00
  • [This bug](https://code.google.com/p/android/issues/detail?id=58287) describes a deficiency in the Android gradle plugin that prevents easily hacking the gradle buildfile to add a jar target... – mmigdol Feb 11 '14 at 21:25

11 Answers11

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

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
63

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

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thank you very much! Your answer was really helpful, most in the last screenshot that shows the Gradle tasks. I didn't know that. I could follow your the steps, but I still missing the org.apache.http.Header. How can I import it into my lib? – dum4ll3 Feb 12 '14 at 16:04
  • What do you mean when you say you're missing it? – Scott Barta Feb 12 '14 at 16:34
  • I can't import it. Like when you hit Ctrl+Space to see the imports' options, org.apache.http.Header isn't there. But I see others like com.sun.xml...Header. – dum4ll3 Feb 12 '14 at 16:39
  • If you're running a version of Android Studio earlier than 0.4.3, that's your problem -- please upgrade. – Scott Barta Feb 12 '14 at 16:50
  • I'm using 0.4.4. It seems like when you create a new Java Library (File menu > New Module... > Java Library) it comes without the apache's package. I miss it in my lib module, not in my app's. So how can I import this specific package inside my lib module? – dum4ll3 Feb 12 '14 at 17:07
  • Oh, you have to make sure it's in your lib module's dependencies, too. – Scott Barta Feb 12 '14 at 17:13
  • Yeah, it's not, so how can I fix it? And a last doubt, when I execute a gradle jar task, where can I find the resulting .jar file? Anyway, Im really grateful for your help! :) – dum4ll3 Feb 12 '14 at 17:33
  • 1
    Go through the same Project Structure > dependencies flow, but choose your library module instead of your project, and add the dependency in the same way. In my answer I say that it puts the jar file in the build/libs dir in its module directory. – Scott Barta Feb 12 '14 at 17:49
  • 2
    I got: java.lang.NoClassDefFoundError: com.loopj.android.http.RequestParams. That's my external library I'm compiling with mine. – dum4ll3 Feb 12 '14 at 20:09
  • Hi Scott, it doesn't feel right to have to do this to get a jar file for a microsoft sponsored project on github. I'm trying to use SignalR Android client in my android studio project and I must have taken a left turn somewhere. Surely I should not make my jar and put the MS "jar" inside of it. Thanks. – toddmo Apr 12 '15 at 00:54
  • 2
    It work with java library only, and jar task in android library not exist – cuasodayleo Dec 31 '15 at 02:57
  • When I move my classes into a new module, android studio complains about all the import statements I've used saying (e.g.) 'cannot resolve symbol Intent'. – Kamran Bigdely Apr 10 '17 at 16:42
  • I resolved my import issue (cannot resolve symbol) by creating **Android Library** instead of Java Library (**New Module... > Android Library**) – Kamran Bigdely Apr 11 '17 at 22:21
  • In the latest version of Android Studio (3.4.1) the gradle script is called 'createFullJarDebug' and 'createFullJarRelease' respectively. – moonlightcheese Sep 21 '19 at 09:12
40

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

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39
12

Simply add this to your java module's build.gradle. It will include dependent libraries in archive.

mainClassName = "com.company.application.Main"

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

This will result in [module_name]/build/libs/[module_name].jar file.

Bao Le
  • 16,643
  • 9
  • 65
  • 68
  • But would a .jar library have a Main class? The OP says they're building a .jar file "so I can use in another projects" (sic). That sounds like a library. – Steve White May 09 '20 at 16:01
  • Only this guy actually understood that, just jar creation doesn't solve prob. what if we need to execute it. Above code avoids "Can't execute jar- file: “no main manifest attribute”" error. – MobileEvangelist Jul 22 '20 at 12:49
2

I had a supplement; In the new version Android Studio(for me is: Android Studio Arctic Fox | 2020.3.1 Patch 3 Build #AI-203.7717.56.2031.7784292, built on October 1, 2021) In gradle panel, it has no task in default. Go to the Android Studio ---Settings--Experimental--Gradle tab, uncheck"Do not build Gradle task list during Gradle sync", then click File--Sync project with Gradle files, then the task appeared in the Gradle Panel.

Then follow the answer: https://stackoverflow.com/a/52681317/4065069

enter image description here

Michael Yang
  • 1,403
  • 2
  • 18
  • 27
1
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)
1
  1. Go to Gradle tab in Android Studio , then select library project .

  2. Then go to Tasks

  3. Then go to Other

  4. Double click on bundleReleaseaar

You can find your .aar files under your_module/build/outputs/aar/your-release.aar

Here image

Badro Niaimi
  • 959
  • 1
  • 14
  • 28
  • I don't see any "Gradle Tab" anywhere. Finally I typed "Gradle" in the Help search, and got something like your Gradle view. But the list inside is very different, much shorter than yours. There is no bundleReleaseaar under Tasks->Other. There is a "createJar", but when I click that, I get "Task 'createJar' not found in root project..." I'm stuck. And... why do we want .aar files? The OP asked for a .jar file. Is that the same thing> – Steve White May 09 '20 at 16:13
  • But question is how to make `.jar` not `.aar` – Sirop4ik May 20 '20 at 13:09
1

In case of this accepted answer not working for you

Use this

task createJar(type: Copy) {
    from('build/intermediates/packaged-classes/release/')
    into('libs/jars/')
    include('classes.jar')
    rename('classes.jar', 'plugin.jar')
}

Instead of this

task createJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('libs/jars/')
    include('classes.jar')
    rename('classes.jar', 'logmanagementlib.jar')
}
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

the way i found was to find the project compiler output (project structure > project). then find the complied folder of the module you wish to turn to a jar, compress it with zip and change the extension of the output from zip to jar.

yaniv
  • 152
  • 5
  • 15
0

.jar file will be automatically generate when u compile/run your application.

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

jar file location

Sonia John Kavery
  • 2,099
  • 2
  • 20
  • 36
0

If you use

apply plugin: 'com.android.library'

You can convert .aar -> .jar

If you run a gradle task from AndroidStudio[More]

assembleRelease 
//or
bundleReleaseAar

or via terminal

./gradlew <moduleName>:assembleRelease
//or
./gradlew <moduleName>:bundleReleaseAar

then you will able to find .aar in

<project_path>/build/outputs/aar/<module_name>.aar
//if you do not see it try to remove this folder and repeat the command

.aar[About] file is a zip file with aar extension that is why you can replace .aar with .zip or run

unzip "<path_to/module_name>.aar"
yoAlex5
  • 29,217
  • 8
  • 193
  • 205