PowenKo, Tutorial 110-1, web api , JAR, Create an Android Jar Library JAR

Although the ADT plugin doesn’t provide a specific project type to create Android Jar files, you can do it easily using a custom builder of the type ant and adding to the project a build.xml file.

Let me give you the steps to do it:

  1. Create a new “Android Project” in Eclipse. You will need to provide the Project Name, the Build Target Name, the Application Name and  the Package Name. We are not going to export any activity, so uncheck the checkbox “Create Activity”. 
  2. Once the project is created, create a build.xml file and add it to the root path of your project.
    Here it is a sample build.xml file:

    <project name="AndroidLib" default="dist" basedir=".">
       <description>Android Sample Library</description>
       <!-- Setting global properties for this build -->
       <property name="src" location="src" />
       <property name="bin" location="bin" />
       <target name="dist">
          <jar destfile="AndroidLib.jar" basedir="bin/">
             <!-- Use ** to include the directory recursively -->
             <include name="com/**" />

    You can use this simple sample file replacing the line 10 with the name you are using in the first part of your package name. In this line, as the path indicator, use ** instead of * always you want to include directories recursively.

  3. Ok, the build.xml file is ready to be used. The next step is to integrate the jar file generation into the Project build process of Eclipse. To do it, follow the next steps:
    • Edit the project properties (right button in the project and select Properties) and create a new Builder from the Builders section.
    • In the “Choose Configuration Type” dialog, select “Ant Builder” as the external tool to be used.
    • Fill in the “Name” field. Then, put the path to your build file in the “Buildfile” field and put the path to your project root folder in the “Base Directory” field.
    • Build the project (right click on the project and select “Build Project”) and…. voila! Your jar file is ready to be used in any other Android project!

Note: Remember Jar files are not the recommended way to create a library of activities because then, to use those Activities you will also need to define those activities in the Manifest.xml file of your applications. If you plan to do it my recommendation is to use the “Is Library” option of the Android project properties. You can find more info in the following link:http://developer.android.com/guide/developing/projects/projects-eclipse.html.



Leave a Reply