Create an Android Jar library for distribution

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 (ie 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?


如果您创建的Android库项目没有任何资源,则ADT(首先在r16中注意到)将创建一个与'bin'文件夹中项目名称相同的.jar。


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.


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.

链接地址: http://www.djcxy.com/p/62794.html

上一篇: 对于新的Android ActionBar支持,无法找到Theme.AppCompat.Light

下一篇: 创建一个用于分发的Android Jar库