6

I have a library project in Android Studio. I want to generate a .jar file for this library. I need the jar file contain/include all the dependencies this Library project has.

The problem is I couldn't find a way of generating a jar file. Is there any way I can generate a jar file for my project?

Best Regards

user2498079
  • 2,872
  • 8
  • 32
  • 60
  • Here is a complete example : http://stackoverflow.com/questions/21712714/how-to-make-a-jar-out-from-an-android-studio-project – Trifactor Feb 25 '15 at 10:26

2 Answers2

1

Here is an example of Gradle task which creates .jar from classes.jar.

classes.jar file can be found in /build/intermediates/exploded-aar/ folder.

Edit: Create jar file for every Android Library project and distribute them standalone. What about dependencies required by library projects - add those dependencies in final project, where your library will be used.

For example: you Android Library project MyLibProject, which has following dependencies in build.gradle

dependencies {
    compile "com.android.support:support-v4:19.+"
}

When you'll build your project you'll get classes.jar for MyLibProject, and those classes.jar will contain just class from library project, but not from com.android.support:support-v4:19.+.

After that in another project you add classes.jar as library dependency in build.gradle and also define same dependencies as in MyLibProject.

And if you still need fatJar, look here how to create fatJar with Gradle.

Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
  • Did that. It doesn't create a jar with the name I have assigned in POM_ARTIFACT in gradle.properties file. – user2498079 Feb 25 '15 at 08:14
  • The main idea: after building your project, you'll get classes.jar file for every Android library project. Gradle task just renames this file. – Veaceslav Gaidarji Feb 25 '15 at 08:58
  • No, doesn't and it's generally bad idea. Create jar file for every Android Library project and distribute them standalone. What about dependencies required by Library projects - add those dependencies in final project, where your library will be used :) – Veaceslav Gaidarji Feb 25 '15 at 09:55
  • Those classes.jar files don't contain the dependency files for the library. By this I mean the jar files that were in the library project aren't packaged in this classes.jar file. What should I do now? – user2498079 Feb 25 '15 at 09:58
  • Gradle says Coudln't find property 'jar'on task 'fatJar' – user2498079 Feb 25 '15 at 10:33
  • I gave you an idea, haven't tested it. If this example isn't working, then go to gradle documentation and find corresponding topic. – Veaceslav Gaidarji Feb 25 '15 at 10:38
1

If you set up the code as a plain Java module in Gradle, then it's really easy to have Gradle give you a jar file with the contents. That jar file will have only your code, not the other Apache libraries it depends on. I'd recommend distributing it this way; it's a little weird to bundle dependencies inside your library, and it's more normal for users of those libraries to have to include those dependencies on their own (because otherwise there are collisions of those projects are already linking copies of the library, perhaps of different versions). What's more, you avoid potential licensing problems around redistributing other people's code if you were to publish your library.

Take the code that also needs to be compiled to a jar, and move it to a separate plain Java module in Android Studio:

File menu > New Module... > Java Library
Set up the library, Java package name, and class names in the wizard. (If you don't want it to create a class for you, you can just delete it once the module is created)
In your Android code, set up a dependency on the new module so it can use the code in your new library:
File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below: 

enter image description here

Choose your module from the list in the dialog that comes up:

enter image description here

Hopefully your project should be building normally now. After you do a build, a jar file for your Java library will be placed in the build/libs directory in your module's directory. If you want to build the jar file by hand, you can run its jar build file task from the Gradle window:

enter image description here

Oroginal article here : How to make a .jar out from an Android Studio project

Community
  • 1
  • 1
Trifactor
  • 63
  • 9