Create an Android Jar library for distribution

JavaAndroidJar

Java Problem Overview


I know of Android Library projects, which allow you to create a shared-source project that can be pulled into Android Applications as needed. However, that requires that source be available.

I'm looking for a way to build and distribute a closed-source library that can be used in other Android projects like a traditional JAR. This will require usage of the Android compiler, so it's not a vanilla-Java JAR file. FWIW, I do not need to embed resources/layouts in the JAR.

I've seen http://andparcel.com/ but it feels like a workaround, and I would rather use something 'officially supported' by Google. Also, I need to make sure that the JAR I build is compatible with old/new version of the Android SDK (i.e. I need a way to set the target platform version, etc).

Do the latest Android toolsets allow for the creation/consumption of JAR binaries? Can you point to some documentation on how I can do it?

Java Solutions


Solution 1 - Java

If you create a Android Library project without having any resources, the ADT (first noticed in r16) will create a .jar with the same name as the project in the 'bin' folder.

Solution 2 - Java

Android doesn't provide a special kind of Android-JAR. But what you can do is adding a build.xml to your project and build the JAR with ant.

My build.xml looks like this:

<project name="MyAndroidLib" default="dist" basedir=".">
  <description>
This is my Android lib
  </description>
  <!-- set global properties for this build -->
  <property name="src" location="src" />
  <property name="bin" location="bin" />

  <target name="jar">
    <jar destfile="MyAndroidLib.jar" basedir="bin/classes/">
      <!-- replace 'com' by what ever you are using -->
      <!-- as first part of the package name -->
      <!-- e.g. de, org, ... -->
      <!-- the ** is important to include the directory recursively -->
      <include name="com/**" />
    </jar>
  </target>
</project>

Build the JAR by running ant jar in your projects main folder.

Solution 3 - Java

You can create a "regular" Java project and import from there Android.jar. Then, you will have access to every component in the SDK. Then, you can export your project as jar... and load it from your Android app. This works great and it seems a preety straightforward way to do it.

Solution 4 - Java

just go to properties of the project of which you want to make jar.Click on Android tab. and tick in the Is library. now you can see .jar file in the bin folder.use it where you want to use.

Solution 5 - Java

The only solution 'officially supported' by Google is the Library project, and it requires the source code to be distributed. You can create a JAR in the normal way, but you cannot include or reference resources within it.

Unfortunately I think it is also not possible to include a packaged JAR within a Library project, as a means to get around the source code requirement.

Solution 6 - Java

In the latest build of Android Studio 1.2, the creation of JAR library has been made as simple as point and click.

Steps to follow :

  • Goto File -> New -> New Module enter image description here
  • Select "Java Library" at the end of the options list enter image description here
  • Enter the name of the jar lib and name of class in it and hit finish button enter image description here
  • Thats it !

The next step is adding your Jar Library as dependency in your app. Simple as that just

  • Goto File -> Project Structure -> Select 'app' -> Select 'Dependency'
  • Select the '+' at the bottom -> Select 'Module Dependency' enter image description here
  • Select your jar lib module you just created above enter image description here enter image description here
  • Select Ok and thats it!

....Or you could just add the below line to your App gradle file

dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar']) // Default Gradle Task, should be already present
      compile 'com.android.support:appcompat-v7:21.0.3' // Default Gradle Task, should be already present

      compile project(':nameOfYourJarLibraryModule') // This is your jar library module
 }

Google is promoting the Android Archive(AAR), even though JAR supported is brought back to android studio.To find out the difference between AAR and JAR refer this link

Solution 7 - Java

Try this: This works

  1. make your library project a normal project by deselecting IsLibrary flag.

  2. Execute your project as Android Application. (It will not show any error)

  3. you'll find a .jar file in bin folder along with .apk.

  4. Give you .jar who want to use your library.

  5. Tell them to just import external jar into build path.

this will works fine with all the resources. best of luck.

Solution 8 - Java

The ADT creates a bin folder which contains all of the class files for all matching source file and directories in your project, you could using the jar command,create a jar archive containing these class files and directories and presumable your library, but of course the class files platform level would only be targeted for current level of the project build- you would need a different jar file for each platform level; However the great thing about this is that the R.class file is include in the project directory tree and therefor you have access to its resources. I don't know if this is the official way to do things in android, but it worked for me.

Solution 9 - Java

In our project, we are creating apk (using apkbuilder) which is installed in /system/framework and added this apk in default classpath. For compilation of applications using the jar, we are creating a jar file (using jar c).

Note: We don't have any resources in the library.

Solution 10 - Java

.jar is just a .zip file which contains .class file (you can try change extension of any .jar file to .zip then see the result). Easily, you can create any .jar library for any purpose by zipping .class file.

Solution 11 - Java

Steps :

  1. Open file project.properties

  2. Add value android.library=true

  3. Save file

  4. From eclipse menu for project, select build automatically then select clean

Result : Brand new jar is created under bin.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpsychotikView Question on Stackoverflow
Solution 1 - JavaFreddroidView Answer on Stackoverflow
Solution 2 - Javawhite_geckoView Answer on Stackoverflow
Solution 3 - JavaferostarView Answer on Stackoverflow
Solution 4 - JavaDhrupalView Answer on Stackoverflow
Solution 5 - JavaJeff GilfeltView Answer on Stackoverflow
Solution 6 - JavaNishant SrivastavaView Answer on Stackoverflow
Solution 7 - JavaAayush RanaView Answer on Stackoverflow
Solution 8 - JavaDavid EvansView Answer on Stackoverflow
Solution 9 - JavaKaranView Answer on Stackoverflow
Solution 10 - JavaRenzokukenView Answer on Stackoverflow
Solution 11 - JavaPana TechnicaView Answer on Stackoverflow