How to Make a .Jar Out from an Android Studio Project

how to create library ( jar ) in android studio ?

A library module is useful in the following situations:
When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.
To create a new library module in your project, proceed as follows:

Click File > New > New Module.

In the Create New Module window that appears, click Android Library, then click Next.

There's also an option to create a Java Library, which builds a traditional JAR file.

Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

Once the Gradle project sync completes, the library module appears in the Project panel on the left.

If you don't see the new module folder, make sure it's displaying the Android view.

Visit https://developer.android.com/studio/projects/android-library.html

how to create a jar file from android studio

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.

Error while creating own jar library for Android project

Update 2

According to logs you build your *.jar with Java 1.8. The max version of java that Android supports is 1.7. So you have to set source code compatability to 1.7 and rebuild the jar-file.

If you are building this jar not from gradle (or Android Studio) you need to change your JDK version (File -> Project Structure -> SDK Location -> JDK Location).

For gradle module see "Update 1"


Update 1

For you java module (from which you are trying to get jar) put these lines:

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7

Try to put next code to your gradle files for Android modules:

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

Android studio make jar file with dependencies

Finally, I found the solution:

android.libraryVariants.all { 

variant ->
def name = variant.buildType.name

if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
//Include Java classes
task.from variant.javaCompile.destinationDir
//Include dependent jars with some exceptions
task.from configurations.compile.findAll {
it.getName() != 'android.jar' && !it.getName().startsWith('junit') && !it.getName().startsWith('hamcrest')
}.collect {
it.isDirectory() ? it : zipTree(it)
}
artifacts.add('archives', task);
}


Related Topics



Leave a reply



Submit