androidlibrary-project

Export android library project for reuse without source code


I need to export a library project without the source code for security reasons. Unfortunately the jar file generated within a library project does not contain the resources. I cannot expect the users of this library to deal with any resources needed by the library.

There have been similar post to this one, but I am yet to see a solution.


Solution

  • The following recipe used to work, though I have not tried it very recently:

    Step #1: Get the library project working as-is. I will refer to the directory holding this project as $ORIG.

    Step #2: If you have not done so already, create your Ant build scripts in $ORIG using android update project.

    Step #3: Add a task to $ORIG/build.xml that creates a JAR file, something like:

    <target name="jar" depends="debug">
      <jar
          destfile="bin/YOUR-LIBRARY-NAME-GOES-HERE.jar"
          basedir="bin/classes"
      />
    </target>
    

    Step #4: Copy your entire library project from $ORIG into another directory, which I'll call $DIST.

    Step #5: Get rid of the src/ tree in $DIST except for the root src/ directory itself.

    Step #6: Move bin/YOUR-LIBRARY-NAME-GOES-HERE.jar into $DIST/libs/. This effectively replaces your source code from src/ with its compiled equivalent.

    Step #7: Get rid of $DIST/bin/ as it is no longer needed.

    $DIST now holds an Android library project that is the equivalent of $ORIG, except that the src/ tree is replaced by libs/YOUR-LIBRARY-NAME-GOES-HERE.jar.

    Since the Google Play Services package from the SDK Manager is packaged this way, not only do I assume the recipe still works, but it would appear to be reasonably officially endorsed.

    Note that there is new build system in the works that may give us more options here. If you are reading this question in, say, 2014, be sure to check to see if there are better alternatives.