How to export library to Jar in Android Studio?
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
export android library as a .jar file
You must export it as .AAR
file. It is not possible to export an Android Studio library as a .jar
file.
Android .AAR Format
Check this out: https://androidbycode.wordpress.com/2015/02/23/building-an-aar-library-in-android-studio/
Purpose of AAR: Library reuse has long been available in Java based
applications through Java Archive (.jar) files. Android AAR files
build upon this concept to allow you to package not only the source
code but also the libraries self contained Android resources. The
library can have its own manifest and resources and can also
optionally provide assets, NDK built libraries, proguard configuration
and even its own custom lint filters. This is all bundled into a zip
archive that can be published to a repository for your team (or the
world!) to get easy access from a Gradle build.
This is how to create the .AAR file by @alex18aa:
File
-> New module
-> Import aar/jar
, and then add the dependencie in the module settings, and linking it.
Android Studio exporting Android library project as jar
You don't want to produce a jar
file as the res/
folder will be missing and anything that references the R
file in your code will fail when executed. This is because the jar
format doesn't support android res
. You want your build to instead target aar
format. This is simple using gradle. Instead of applying the application plugin (apply plugin: 'com.android.application'
) just apply the library plugin (apply plugin: 'com.android.library'
).
Then the gradle build
will produce an aar
file which will contain all your resources.
Developers who are still using eclipse will be familiar with how to import aar
files because that is the android standard. If they have not learned yet there are plenty of SO posts to help.
Developers who are using gradle or maven will have no problems consuming your sdk
Creating my own Android library and exporting it to .jar
Android Studio doesn't have UI to allow you to create an Android Library as a new project. To do this, you'll have to one of a few different routes.
Before you start, I'd encourage you to think about packaging your library as an .aar instead of a .jar, since you say it will be calling Android APIs. The reason for this is that .aar is a richer format for Android projects and allows the archive to include manifest information, such as the minimum required SDK and such -- it will go farther to ensure that your library is compatible with the environment it's being included in. The disadvantage is that only the Gradle build system supports .aar files, so Eclipse users wouldn't be able to use the archive.
Though you're not using Android resources, if in the future you do need to include resources in your archive, you'll be set up.
There's more information on .aar here: http://tools.android.com/tech-docs/new-build-system/aar-format
If you want to package in .aar format, you could go a couple routes:
You could create a project with the New Project wizard. This will initially be set up to make a full-fledged Android app that compiles to an APK. You can go into the app module's build.gradle file and change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.library'
It will now build an .aar. However, this project will still have resources in it, and maybe layouts and activities depending on what options you chose in the wizard, so you may have to rip out what you don't want.
The alternative is you could start with a blank slate, crafting your own build.gradle file from scratch that uses the com.android.library
plugin, and fleshing out the source directories. This will be more cumbersome, but as with all difficult activities, you'll learn a lot and it will build character, or you can at least tell yourself that as you're puzzling things out.
If you want to package as .jar instead of .aar, then again there are a couple routes you could go. For this type, if you want maximal assistance from the UI, you could start a new Android project and add a plain Java module to it. You can either remove the unwanted Android module from the project, or you could even leave it alone; the build process will output a .jar for your plain Java module in any event.
Your plain Java module will need to depend on the android.jar archive from the appropriate platform in your SDK.
Alternately, you could build this module up from scratch, authoring the build.gradle file and setting up the sources yourself, and then you could import that into Android Studio as a new project.
Android Studio export jar with dependencies
Solved with the following tasks:
task jarTask(type: Jar) {
baseName="my-sdk-android"
from 'src/main/java'
}
task createJarWithDependencies(type: Jar) {
baseName = "my-sdk-android-jar-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
Android how to export jar with resources?
You cannot export self-contained Android Library jar at this time and announced will be available in the future SDK release.
https://developer.android.com/guide/developing/projects/index.html#LibraryProjects
Related Topics
Stopping an Android App from Console
Programmatically Change Input Type of the Edittext from Password to Normal & Vice Versa
Edittext Maxlines Not Working - User Can Still Input More Lines Than Set
Error :: Duplicate Files During Packaging of APK
Get List of Installed Android Applications
My Current Location Always Returns Null. How to Fix This
How to Attach Multiple Files to Email Client in Android
Android - Spacing Between Checkbox and Text
Customize Android Intent.Action_Send
How to Start an Intent by Passing Some Parameters to It
Android:Inapp Purchase Receipt Validation Google Play
Android: Remove All the Previous Activities from the Back Stack
Eclipse/Android:"Errors Running Builder 'Android Pre Compiler' on Project..."
"Canvas: Trying to Draw Too Large Bitmap" When Android N Display Size Set Larger Than Small
Appcompatactivity.Oncreate Can Only Be Called from Within the Same Library Group
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3