How to Convert an Android Library Project to an External JAR?
Asked Answered
S

2

21

I have an Android library project that has been working for me pretty well, but now I am interested in "converting" it to an external JAR.

How do I do that?

Can any Android library project be converted to an external JAR? If not, what are the restrictions or limitations?

Spallation answered 1/11, 2011 at 22:39 Comment(0)
S
29

Can any Android library project be converted to an external JAR?

Not right now.

If not, what are the restrictions or limitations?

If your library is purely Java code, with no resources, you can create a JAR out of the .class files, just as you would with regular Java.

If your library uses resources, you can still create a JAR as above. However, you will need to look up the R values (e.g., R.layout.main) via reflection or getIdentifier(), and your resources will not be packaged in the JAR but would have to be distributed separately.

In the future, the build tools should support creating distributable JAR files out of library projects, complete with resources. The current build tools do create JARs, but they are not designed to be distributed but are rather internal build artifacts at the moment.

Sisile answered 1/11, 2011 at 22:58 Comment(5)
Thanks. Do I understand correctly that creating a JAR out of the .class files is as simple as jar cf jar-file input-files? Also, can you elaborate a little more about how the resources accessed via getIdentifier() are distributed separately?Spallation
@uTubeFan: "Do I understand correctly that creating a JAR out of the .class files is as simple as jar cf jar-file input-files?" -- presumably. I use the Ant <jar> task myself. "Also, can you elaborate a little more about how the resources accessed via getIdentifier() are distributed separately?" -- um, well, if they're not in the JAR file, and the reuser of the JAR needs the files, the reuser needs to get those files somehow. I'd recommend a ZIP archive containing the JAR and the res/ tree.Sisile
Hi @Spallation Suppose consider a project MyTranslator in which i am translating some text, for that i have imported a jar file xyztranslator.jar. Now i want to convert MyTranslator project to a .jar file such that i can use all the classes in MyTranslator project and also all the classes in xyztranslator.jar. Simply my question is How i can export an android application to a .jar file which already had imported jar filesUncalledfor
I had export Android Library to jar, How to proguard it?Tenrec
@Hades: The focus now is more on AARs for library projects, as part of the Gradle for Android initiative. To create a JAR for a library project that does not use resources, you need to add your own Gradle task.Sisile
S
2

http://developer.android.com/guide/developing/projects/index.html#LibraryProjects claims that you can't export these to an external JAR. Specific quote:

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.

Stegodon answered 1/11, 2011 at 22:42 Comment(4)
Thanks. In the same page, there is also a clause that says: "You need SDK Tools r14 or newer to use the new library project feature that generates each library project into its own JAR file." Does that mean that if I use SDK r14 (or newer) the lmitation you quoted no longer exists?Spallation
To be honest, I'm not sure. That wording seems to contradict the part I quoted from lower down, though it could just be poor phrasing.Stegodon
r14 redid how they handle library project for the purpose of being able to export jars but I still can't figure out how to do it while keeping the generated resources.Scarf
I had package Anroid Libary Project to jar, How to proguard it?Tenrec

© 2022 - 2024 — McMap. All rights reserved.