How to create jar for Android Library Project
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!
- Make your library project a normal project by deselecting IsLibrary flag.
- Execute your project as Android Application. (It will not show any error)
- You'll find a .jar file in bin folder along with .apk.
- Give you .jar who want to use your library.
- 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):
- 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.
- Copy the .jar file into the libs folder of your target application
- Refresh your workspace and press
Ctr+Shift+O
(on Windwows/Linux) orCmd+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
- Create your library project
- Create empty project
- Add library project to your empty project
- Build project
- In /gen folder you will find class for your library project, this will contain your resources for lib project.
- Export JAR (select only this class ... from /gen folder)
- Export JAR from your lib project
- Rename exported files to zip and merge content
- 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.
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, 2022Comments
-
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 almost 11 yearsA 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 almost 11 yearsA 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 almost 11 yearsInt 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 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 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 over 10 yearsWhen I do this, I do not get a .jar file generated in my bin/ directory?
-
David Doria over 10 yearsI 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 over 10 yearsif 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 over 10 yearsSee my answer here for an easier way - btw your answer put me to the right track ;)
-
ripopenid over 10 yearsDid you try applying Proguard to the JAR? If so, how did it work? Were there any difficulties or complications?
-
ripopenid over 10 yearsThis is by far the simplest way to create a JAR from a Library Project but... it doesn't contain the
res
and theassets
folders. Am I missing something? -
ripopenid over 10 yearsI 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 over 10 yearsStep #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 over 10 yearsThis 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 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#LibraryProjects
-
Mahmoud Abou-Eita over 10 yearsyet 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 over 10 years@MahmoudAbou-Eita: Efforts are now focused on AAR artifacts, from the new Gradle-based build system.
-
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 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 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 almost 10 yearsThis will not include the resources, which is usually the point behind an Android library project.
-
CommonsWare almost 10 yearsThis will not include the resources, which is usually the point behind an Android library project.
-
Nathan Schwermann almost 10 yearsA 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 over 9 years@ripopenid Those cannot be in a JAR. That's what you need to reference library projects for.
-
Pacerier over 9 years@schwiz, If AAR works, why does the page developer.android.com/tools/projects/index.html#LibraryProjects state "you must compile the library indirectly, by referencing the library in the dependent application and building that application"?
-
Pacerier over 9 years@MarcoW., Only
res
is possible, Even referencing doesn't allow us to useassets
, See developer.android.com/tools/projects/index.html#LibraryProjects : "Any asset resources used by an application must be stored in the assets/ directory of the application project itself". -
caw over 9 yearsThat's right. A JAR gets you the
src
directory. A library project gets youres
in addition. More is not possible. -
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 over 9 yearsNow 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 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 over 9 yearsI 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 over 9 yearsHow 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 about 9 yearsi am getting Unable to execute dex: Multiple dex files define .../Manifest$permission; How to generate jar file without these manifest classes
-
Hunt about 8 yearscan we merge external libraries like
retrofit
,realm
etc into our custom library project and distribute -
Subhanshuja over 4 yearsUsing eclipse for only create jar is not complete, i think better using directly in android studio.
-
Subhanshuja over 4 yearsNice, idea using ant, but it would be difficult if add .class one by one into xml