I have downloaded some library sources and would like to export it as a Jar file using Android Studio. Is there a way to export to jar file using Android studio ?
edit:
The library I want to export as jar is an Android library. It's called "StandOut" and can be downloaded from GitHub. https://github.com/pingpongboss/StandOut
To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.
The main difference is aar is splitted inside android to jar. If your app will be used only in user app only in android studio then aar is preferred. If you are planning for app to communicate with c/c++ compiled lib.so file jar is preferred.
How to find the libs folder in Android Studio? 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.
To use a Java library (JAR file) inside your Android project, you can simple copy the JAR file into the folder called libs in your application. *. jar files in this folder are included into the compile classpath via the default build.
It is not possible to export an Android library as a jar file. It is possible, however, to export it as aar
file. Aar
files being the new binary format for Android libraries. There's info about them in Google I/O, the New Build System video.
First, build the library in Android Studio or from command line issuing gradle build
from your library's root directory.
This will result in <yourlibroot>/libs/build/yourlib.aar
file.
This aar file is a binary representation of your library and can be added to your project instead of the library as a dependency project.
To add aar file as a dependency you have to publish it to the maven central or to your local maven repository, and then refer the aar file in your project's gradle.build
file.
However, this step is a bit convoluted. I've found a good explanation how to do so here:
http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
I was able to build a library source code to compiled .jar
file, using approach from this solution: https://stackoverflow.com/a/19037807/1002054
Here is the breakdown of what I did:
In may case it was a Volley library
I used Android Studio 0.3.7. I've encountered some issues during that step, namely I had to copy gradle
folder from new android project before I was able to import Volley library source code, this may vary depending on source code you use.
build.gradle
file// If your module is a library project, this is needed //to properly recognize 'android-library' plugin buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.3' } } apply plugin: 'android-library' android { compileSdkVersion 17 buildToolsVersion = 17 sourceSets { main { // Here is the path to your source code java { srcDir 'src' } } } } // This is the actual solution, as in https://stackoverflow.com/a/19037807/1002054 task clearJar(type: Delete) { delete 'build/libs/myCompiledLibrary.jar' } task makeJar(type: Copy) { from('build/bundles/release/') into('build/libs/') include('classes.jar') rename ('classes.jar', 'myCompiledLibrary.jar') } makeJar.dependsOn(clearJar, build)
gradlew makeJar
command from your project root.I my case I had to copy gradlew.bat
and gradle
files from new android project into my library project root. You should find your compiled library file myCompiledLibrary.jar
in build\libs
directory.
I hope someone finds this useful.
Edit:
Althought this works, you will encounter duplicate library exception while compiling a project with multiple modules, where more than one module (including application module) depends on the same jar
file (eg. modules have own library directory, that is referenced in build.gradle
of given module).
In case where you need to use single library in more then one module, I would recommend using this approach: Android gradle build and the support library
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With