9

I had a project library in Eclipse with Maven and the generated jar included some libraries dependencies inside. Now I am migrating to Android Studio and I would like to build the same jar. I can generate a jar with the following lines in gradle:

task clearJar(type: Delete) {
  delete 'build/libs/mysdk.jar'
}

task makeJar(type: Copy) {
  from('build/intermediates/bundles/release/')
  into('release/')
  include('classes.jar')
  rename ('classes.jar', 'mysdk.jar')
}

makeJar.dependsOn(clearJar, build)

But inside the jar there are not included the libraries that I use in my project library. With Maven I can use the "provided" scope in order to include or not a library in my jar but with gradle... how can I do that?

Thanks

esteban
  • 543
  • 4
  • 18

1 Answers1

12

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
}
esteban
  • 543
  • 4
  • 18
  • 2
    You rock mate. Saved the day. Just to clarify, you need to run "gradlew createJarWithDependencies" and the output will be in /build/outputs/libs – Elad Nava Mar 22 '15 at 17:31
  • Can you please help me for above answer i am not getting desire output –  Sep 11 '15 at 08:30
  • 4
    This does not work for me. Source files are included as is and not in compiled form as was the case with 'Export..' in eclipse. – devanshu_kaushik Dec 30 '15 at 12:57
  • Its not able to find my source files (.java files). can you please give me the solution what i m doing wrong ? – Kinjal Shah Jul 28 '16 at 12:02
  • please, can you check this question? http://stackoverflow.com/questions/39036702/how-to-import-an-android-library-project-created-on-android-studio-into-existing – Universe Aug 20 '16 at 14:18
  • Works perfectly. But, this is not applying proguard though. Any idea how to use proguard? – nizam.sp Nov 01 '16 at 14:16
  • @nizam.sp Where do you add these lines? – Alan Nov 16 '16 at 19:15
  • While this works `as advertised` I wasn't planning on packaging my `java` files with the dependent `jar` files. I planned on packaging my _`class`_ files with its dependent `jar` files. -- I'll keep looking; but thanks for these clues. – Jesse Chisholm Nov 16 '16 at 22:29
  • @Alan - re: `where do you add these lines?` as top level structures; so just below the `dependencies { ... }` section. – Jesse Chisholm Nov 16 '16 at 22:30
  • @JesseChisholm Below the dependencies section. – esteban Nov 17 '16 at 10:15
  • Change `from 'src/main/java'` to `from 'build/classes/main'` and it will package the `.class` files instead of the `.java` files. – Octocat Dec 26 '16 at 04:08