1

I want to use android studio to make a library project to a jar file which contains many jar files and library projects like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':app:libraries:MyLibrary:app')
    compile 'com.luckycatlabs:SunriseSunsetCalculator:1.2' 
    compile project(':app:libraries:MyLibrary:app:libraries:MySubLibrary:app')
}

How can I make a jar file with all this dependencies and own class files?

Thinsky
  • 4,226
  • 3
  • 13
  • 22
  • Maybe following link help http://stackoverflow.com/questions/21712714/how-to-make-a-jar-out-from-an-android-studio-project/35431416#35431416 – Abhinav Tyagi Mar 15 '16 at 04:49

1 Answers1

2

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);
}
Thinsky
  • 4,226
  • 3
  • 13
  • 22
  • 1
    After so many efforts, finally solved with your solution. I m able to create jar file with dependencies. Just want to add, you need to run "jarRelease" from Gradle tab and the output will be in /build/libs – Kinjal Shah Jul 29 '16 at 09:52