How to create jar for Android Library Project

98,075

Solution 1

This is the closest that you can get:

Step #1: Create a regular Android library project, and get it working.

Step #2: Copy that Android library project into another directory.

Step #3: Create a JAR from the compiled Java classes from the original Android library project, and put that JAR in the libs/ directory of the copy you made in Step #2. You should be able to run ProGuard on this JAR manually, though I haven't tried that.

Step #4: Remove everything inside the src/ directory of the copied library project, leaving behind and empty src/ directory.

Step #5: ZIP up or otherwise distribute the copied Android library project.

This will give you an Android library project like the Play Services one, where the resources are available, but the source code is not.

UPDATE: An even better approach nowadays is to package your library project as an AAR, which is Android's native way of creating a reusable artifact from your compiled code plus required resources. At the present time (July 2014), Gradle for Android, Android Studio, and the android-maven-plugin can create and consume AARs.

Solution 2

This works!

  1. Make your library project a normal project by deselecting IsLibrary flag.
  2. Execute your project as Android Application. (It will not show any error)
  3. You'll find a .jar file in bin folder along with .apk.
  4. Give you .jar who want to use your library.
  5. Tell them to just import external jar into build path.

This will work fine with all the resources. Thanking you all. for your responce

Solution 3

Follow the below steps to obtain a distributable jar of your library (verified for library project without external dependencies):

  1. Create your library project. Clean it. You will get a libraryProjectName.jar file inside the bin folder. (Make sure "is Library" is selected in the library project.
  2. Copy the .jar file into the libs folder of your target application
  3. Refresh your workspace and press Ctr+Shift+O (on Windwows/Linux) or Cmd+Shift+O (on Mac) to resolve any import dependencies from the library added.

Comment in case facing any issue.

Solution 4

Tested with adt 22.2.1

  1. Create your library project
  2. Create empty project
  3. Add library project to your empty project
  4. Build project
  5. In /gen folder you will find class for your library project, this will contain your resources for lib project.
  6. Export JAR (select only this class ... from /gen folder)
  7. Export JAR from your lib project
  8. Rename exported files to zip and merge content
  9. Rename the final file as JAR and add it to project.

Solution 5

You cannot package resource files into jar. You can export your project as a library project.

You export your utility classes that do not refer to resources in android ie pure java classes as jar file.

Check the link below

http://developer.android.com/tools/projects/index.html

To make your project a library project

Right click on your project. Go to properties. Choose Android. Make sure Is Library is checked.

To refer the library project in your android project

Right click on your project. Go to properties. Choose Android. Click Add. Browse and the library project.

Quoting from the docs

A library project differs from a standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

Share:
98,075
Aayush Rana
Author by

Aayush Rana

Declare VARCHAR2 Name := "Aayush Rana"; VARCHAR2 Languages_Known := "Java, C, Python, HTML, PHP, JavaScript"; Begin I m computer loving person and programming is my passion .. Sure I love maths and physics they seek my attention very much. But puzzles never leave me alone. Sometime wake up in night @2 pm just because I got one answer in my dream and I take my laptop and run the solution "Cracked It!". END

Updated on July 05, 2022

Comments

  • Aayush Rana
    Aayush Rana almost 2 years

    I have to create a library that I am going to export to the client in a jar file. Is there any way to create a jar with the resources in it?

    The Google adMob have such a jar, which includes resource file such as R$layout.class in it. This page describes some way to do that but I am not able to understand exactly how am I supposed to import the library project to an application using the method above.

  • Raghunandan
    Raghunandan almost 11 years
    A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools. Quoting from developer.android.com/tools/projects/index.html
  • Raghunandan
    Raghunandan almost 11 years
    A library project differs from a standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application. this quote from doc supports your answer i guess
  • Tim Roes
    Tim Roes almost 11 years
    Int he new Gradle build system, you will have the possibilities to create a so called aar-file (Android Archive), that is exactly what you want. Its basically like an APK (containing resources, manifest, etc.) just for libraries. So switch to Gradle, if you want proper support for that.
  • stumpped
    stumpped almost 11 years
    @CommonsWare Your outline works very well if ProGuard is not run on the JAR. If you do apply ProGuard (manually) to the JAR, then all dependent projects will complain about all referenced symbols "cannot be resolved". I just tried it and that's what I received. Am I missing something?
  • CommonsWare
    CommonsWare almost 11 years
    @stumpped: You need to have your ProGuard configuration file set up to not obfuscate the names of public stuff that the dependent projects need.
  • David Doria
    David Doria over 10 years
    When I do this, I do not get a .jar file generated in my bin/ directory?
  • David Doria
    David Doria over 10 years
    I only get the .jar when I DO have "Is Library" checked. However when I add that to my dependent project, the resources don't work.
  • Aayush Rana
    Aayush Rana over 10 years
    if you want resources to work check this: stackoverflow.com/a/17064024/1779907. The method I explained in answer works if you have an library that do not have any resources. If you want to secure you resources too please hard code them in java (like I used base64 encoding to save my Images directly within a java interface) and then u can use the method explained in my answer(work better if u have less resources).
  • Mr_and_Mrs_D
    Mr_and_Mrs_D over 10 years
    See my answer here for an easier way - btw your answer put me to the right track ;)
  • ripopenid
    ripopenid over 10 years
    Did you try applying Proguard to the JAR? If so, how did it work? Were there any difficulties or complications?
  • ripopenid
    ripopenid over 10 years
    This is by far the simplest way to create a JAR from a Library Project but... it doesn't contain the res and the assets folders. Am I missing something?
  • ripopenid
    ripopenid over 10 years
    I didn't downvote (not enough points) but I can guess that instead of answering How to create jar for Android Library Project, you answered that it is not possible. All other answers actually show a (working) way to create such jar.
  • ripopenid
    ripopenid over 10 years
    Step #6 would have made your approach a better newer alternative to @CommonsWare's approach, except that it doesn't really work if the user (i.e. application project) of this library, defines its own resources. So looks like CommonsWare's method is still the best.
  • TacB0sS
    TacB0sS over 10 years
    This would not work with resources! that jar, doesn't even have the res library in it, you've probably overlooked the properties of the project using the library!.
  • Mahmoud Abou-Eita
    Mahmoud Abou-Eita over 10 years
    @CommonsWare quoting the official guide: Note: You need SDK Tools r14 or newer to use the new library project feature that generates each library project into its own JAR file. developer.android.com/tools/projects/index.html#LibraryProje‌​cts
  • Mahmoud Abou-Eita
    Mahmoud Abou-Eita over 10 years
    yet on the same page: You cannot export a library project to a JAR file A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.
  • CommonsWare
    CommonsWare over 10 years
    @MahmoudAbou-Eita: Efforts are now focused on AAR artifacts, from the new Gradle-based build system.
  • Mahmoud Abou-Eita
    Mahmoud Abou-Eita over 10 years
    @CommonsWare Using this approach, which resources should I exclude from the eclipse JAR export wizard. Including everything causes build problems. (Duplicate Drawables and Manifest)
  • CommonsWare
    CommonsWare over 10 years
    @MahmoudAbou-Eita: I never use the Eclipse JAR export wizard, and this has nothing to do with creating an AAR artiface anyway.
  • Mahmoud Abou-Eita
    Mahmoud Abou-Eita over 10 years
    @CommonsWare I was referring to the sourceless library project approach mentioned in the main answer. My issue is solved anyway. Thanks.
  • CommonsWare
    CommonsWare almost 10 years
    This will not include the resources, which is usually the point behind an Android library project.
  • CommonsWare
    CommonsWare almost 10 years
    This will not include the resources, which is usually the point behind an Android library project.
  • Nathan Schwermann
    Nathan Schwermann almost 10 years
    A little late to the party now that we have AARs but if you still build with ant like me this will do steps 1-3 for you. stackoverflow.com/a/24580308/384306
  • caw
    caw over 9 years
    @ripopenid Those cannot be in a JAR. That's what you need to reference library projects for.
  • Pacerier
    Pacerier over 9 years
    @schwiz, If AAR works, why does the page developer.android.com/tools/projects/index.html#LibraryProje‌​cts state "you must compile the library indirectly, by referencing the library in the dependent application and building that application"?
  • Pacerier
    Pacerier over 9 years
    @MarcoW., Only res is possible, Even referencing doesn't allow us to use assets, See developer.android.com/tools/projects/index.html#LibraryProje‌​cts : "Any asset resources used by an application must be stored in the assets/ directory of the application project itself".
  • caw
    caw over 9 years
    That's right. A JAR gets you the src directory. A library project gets you res in addition. More is not possible.
  • CommonsWare
    CommonsWare over 9 years
    @Pacerier: That documentation is for a regular Android library project, not one packaged as an AAR. AAR is only supported by Android Studio (and Gradle for Android), and little documentation on that has been merged into developer.android.com at this time.
  • Vetalll
    Vetalll over 9 years
    Now i am using gradle and i have to privde to customer jar with resources. I made jar and put resources to res folder. But project that uses this library throws exception like ResourceNotFound with id. What is a problem? Please don't say about module and arr. I have to privide jar
  • CommonsWare
    CommonsWare over 9 years
    @Vetalll: "i have to privde to customer jar with resources" -- there really isn't such a concept. You are welcome to attempt to create a binary-only Android library project, but I don't have any current recipes for that. Or, you are welcome to unpack an actual AAR to effectively "reverse engineer" a binary-only Android library project: commonsware.com/blog/2014/07/03/consuming-aars-eclipse.html
  • Muneeb Mirza
    Muneeb Mirza over 9 years
    I have been searching for this solution for so long. Tried so many things and here is the best and fasted of them all. Is there any way i can vote this answer up, a 100 times???
  • user2273146
    user2273146 over 9 years
    How to Create a JAR file from the compiled Java classes from the original Android library project can you please help suggesting what this statement mean.
  • png
    png about 9 years
    i am getting Unable to execute dex: Multiple dex files define .../Manifest$permission; How to generate jar file without these manifest classes
  • Hunt
    Hunt about 8 years
    can we merge external libraries like retrofit , realm etc into our custom library project and distribute
  • Subhanshuja
    Subhanshuja over 4 years
    Using eclipse for only create jar is not complete, i think better using directly in android studio.
  • Subhanshuja
    Subhanshuja over 4 years
    Nice, idea using ant, but it would be difficult if add .class one by one into xml