How to Convert an Android Library Project to an External JAR?

11,989

Solution 1

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.

Solution 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.

Share:
11,989
uTubeFan
Author by

uTubeFan

Updated on June 17, 2022

Comments

  • uTubeFan
    uTubeFan about 2 years

    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
    uTubeFan over 12 years
    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
    uTubeFan over 12 years
    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?
  • Sinjo
    Sinjo over 12 years
    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.
  • CommonsWare
    CommonsWare over 12 years
    @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.
  • KK_07k11A0585
    KK_07k11A0585 over 12 years
    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
  • Nathan Schwermann
    Nathan Schwermann over 11 years
    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.
  • Crossle Song
    Crossle Song about 11 years
    I had export Android Library to jar, How to proguard it?
  • Crossle Song
    Crossle Song about 11 years
    I had package Anroid Libary Project to jar, How to proguard it?
  • CommonsWare
    CommonsWare about 10 years
    @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.