160

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?

psychotik
  • 38,153
  • 34
  • 100
  • 135
  • have you got answer to your question? I have a similar requirement.. How you went about creating jar files? – Krishnabhadra May 30 '12 at 04:08
  • To distribute a .jar for Android you don't need the Android dx compiler, the regular Java complier works. If you distribute a jar it gets converted into dx bytecode when the app is built. – ThomasW Aug 28 '12 at 05:37

11 Answers11

49

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.

Freddroid
  • 2,449
  • 22
  • 23
  • pls see this question http://stackoverflow.com/questions/12970802/unable-to-refer-library-project-android-on-updated-adt – vnshetty Oct 19 '12 at 10:07
  • 1
    This gets me the jar, but do I need to still distribute the resources folder? Trying to jar up actionbar sherlock and my projects won't compile because they can't find the actionbar resources. – Nathan Schwermann Feb 06 '13 at 05:47
  • 1
    @schwiz this only works for non-resource projects (like I state in the answer). Otherwise you need to import it as a standard Android Library project. – Freddroid Feb 06 '13 at 15:40
  • 1
    Is this still true? I don't see `myapp.jar` in the `/bin` folder. There's `classes.jar`, but that's not it, right? – garbagecollector May 01 '13 at 22:41
  • I'm running the latest ADT and I still get a .jar in my /bin folder. But if your project depends on resources it's no good and the project need to be included as a library project (you still get a .jar though. – Freddroid May 02 '13 at 14:35
  • @DumpHole I've gotten both bin/myapp.jar and bin/classes.jar. One comes via the ADT IDE, one via command line build tools. They are almost the same size but not identical. Probably both will do? #fingerscrossed – akauppi Feb 13 '14 at 13:15
23

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.

white_gecko
  • 4,808
  • 4
  • 55
  • 76
  • Tried this and was able to build a JAR file, but the size was odd (only 343 bytes), as compared to when exporting a library to JAR. Any idea on what flags are missing? – Shane Oliver Nov 15 '11 at 14:35
  • You can check the content of the JAR with a archive program (JAR is ZIP) and see what is missing and add them. You can find more information here: http://ant.apache.org/manual/Tasks/jar.html – white_gecko Nov 23 '11 at 17:29
  • 5
    With the latest Android SDKs, the Java files are compiled to `bin/classes`, so `` should be `` – Theo May 15 '12 at 20:28
17

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.

ferostar
  • 7,074
  • 7
  • 38
  • 61
  • 1
    In that case how to declare the components of the library like services, activities, broadcastreceivers, intentfilters. Do the client application needs to hold all of these declaration in manifest? – Ron Jan 24 '12 at 14:49
  • @ferostar How can I import android.jar to java project? – Krishnabhadra May 31 '12 at 04:56
  • In case that library has resources, how to generate a R.java and add it to JAR –  Nov 06 '15 at 18:35
15

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.

Dhrupal
  • 1,863
  • 1
  • 23
  • 38
11

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

Community
  • 1
  • 1
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • I don't understand how this creates a new jar file. It looks like you're juste creating a new module which will be used as a library in your Android project – Jejefcgb May 04 '15 at 14:34
  • What?! you create new jar from nothing? – Ewoks Nov 05 '15 at 16:59
  • @Ewoks : after you have created the module, you have to add the classes to it. Once done, you add it as a dependency – Nishant Srivastava Nov 05 '15 at 17:22
  • 2
    Question was: there is an implenented library with source code. How to make jar of it so just jar is used in another project instead of whole library source code.. Or I missed something? – Ewoks Nov 05 '15 at 20:43
  • I explained how to create a java library module. Once made you can copy all your class files into the same module, sync gradle and you have a jar file under your java lib module folder/build/output folder. – Nishant Srivastava Nov 06 '15 at 04:36
  • 2
    @Nishant. Worked for me! Thanks! – ramires.cabral Jan 19 '17 at 00:43
10

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.

Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
  • "You can create a JAR in the normal way, but you cannot include or reference resources within it." <-- doesn't seem to work right in Eclipse if I reference Android classes or use the Android compiler. Clues on how to get that to work? – psychotik Feb 16 '11 at 10:23
  • 5
    I just create a normal Java project in Eclipse, add the android.jar to the build path, then select Export - Java - Jar File. – Jeff Gilfelt Feb 16 '11 at 10:47
  • @JeffGilfelt and if you include the android.jar for the android-13 sdk directory in example, will you be able to use your jar library still in projects for a higher API level? – Fran Marzoa Oct 31 '12 at 18:20
6

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.

Aayush Rana
  • 1,303
  • 1
  • 12
  • 19
  • 1
    I have to correct this post: JAR-files can contain only code, no android resources. If any resources are included/needed, we have to keep the library project open and can't just use a simple JAR-file. – cody Dec 16 '13 at 11:51
  • @cody :How do we generate jar file along with resources and use it in another project? – Mahantesh M Ambi Apr 07 '15 at 12:33
1

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.

0

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

  • Not strictly true, see http://en.wikipedia.org/wiki/JAR_(file_format) for subtle differences (mainly ordering of entries within the ZIP) – iddo Oct 14 '13 at 11:59
0

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.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

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.

Karan
  • 12,724
  • 6
  • 40
  • 33