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?

uTubeFan
  • 6,664
  • 12
  • 41
  • 65

2 Answers2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Thanks. Do I understand correctly that creating a JAR out of the .class files is as simple as [jar cf jar-file input-files](http://download.oracle.com/javase/tutorial/deployment/jar/build.html)? Also, can you elaborate a little more about how the resources accessed via `getIdentifier()` are distributed separately? – uTubeFan Nov 01 '11 at 23:36
  • 1
    @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 `` 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. – CommonsWare Nov 01 '11 at 23:48
  • Hi @uTubeFan 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 files** – KK_07k11A0585 Dec 15 '11 at 12:21
  • I had export Android Library to jar, How to proguard it? – Crossle Song Jun 14 '13 at 04:06
  • @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. – CommonsWare Jun 20 '14 at 10:22
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.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
Sinjo
  • 430
  • 3
  • 7
  • 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? – uTubeFan Nov 01 '11 at 23:13
  • 2
    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. – Sinjo Nov 01 '11 at 23:36
  • 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. – Nathan Schwermann Feb 02 '13 at 20:54
  • I had package Anroid Libary Project to jar, How to proguard it? – Crossle Song Jun 14 '13 at 04:13