简体   繁体   中英

How to add external jar libraries to an android project from the command line

I'm trying to build an Android project that has some dependencies. The jar files are in the lib/ directory. I can build the project by adding those jar file to my classpath, but of course it Force Closes in the emulator because those libraries aren't present.

I'm doing this from the command line with ant ( not in eclipse). How can I make it include those libraries in the apk from the command line?

Jay K's answer was right at the time of writing, but now the SDK has changed, and here is the new way to do it:

Add the following line to ant.properties :

jar.libs.dir=lib

Tested, works where external.libs.dir does not work.
That changed in December 2010 and then again in October 2011.

Just for future reference: Right now there is a bug in the SDK which prevents of using jar.libs.dir: http://code.google.com/p/android/issues/detail?id=33194

A simple solution to use jar.libs.dir again (while waiting for the bug to get fixed), is to add this target in the build.xml file:

<target name="-pre-compile">
    <path id="project.all.jars.path">
        <path path="${toString:project.all.jars.path}"/>
        <fileset dir="${jar.libs.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>
</target>

At compiling do this:

javac -encoding ascii -target 1.5 -d bin/classes \
-bootclasspath $SDKDIR/android.jar src/some/project/* -classpath=libs/*.jar

And, when you are generating the bytecode for Dalvik:

dx --dex --output=bin/classes.dex bin/classes libs/*.jar

Also, when you are packaging the APK file:

apkbuilder bin/something.apk -z bin/projectname.ap_ \
-f bin/classes.dex -rf src -rj libs

I'm assuming you are using Linux... on Windows will be almost the same.

Answering my own question:

I created a file "build.properties" and added the line

external.libs.dir=lib

It was a lucky guess, really.

The example above dosen't quite work right. You cannot overwrite the project.all.jars.path inline. Following is a working example :

<target name="-pre-compile">
<!-- HACK to add the android-support-v4.jar to the classpath directly from the SDK -->

    <echo>ORIGINAL jars.path : ${toString:project.all.jars.path}</echo>
    <path id="project.all.jars.path.hacked">
            <path path="${toString:project.all.jars.path}"/>
            <path path="${sdk.dir}/extras/android/support/v4/android-support-v4.jar"/>
    </path>

    <path id="project.all.jars.path">
            <path path="${toString:project.all.jars.path.hacked}"/>
    </path>
    <echo>HACKED jars.path : ${toString:project.all.jars.path}</echo>

</target>

If you store your libraries in ${basedir}/libs the ant script will be able to find them.

I looked at the current ant script ( ${sdk.dir}/tools/ant/build.xml ) and it defines jar.libs.dir to "libs".

FYI, the ant script is well documentated so it's easily to trace through.

If you move libs around be sure to edit your .classpath files to prevent eclipse users from suffering needlessly.

external.libs.dir=lib

after 2 hours of searching for this answer, this worked for me. The jar file in my libs directory finally compiles AND is bundled with the APK.

将jar放在${YOUR_PROJECT}/libs目录中

The jar.libs.dir property is a single directory. It's not like a CLASSPATH which is a colon/semicolon separated list. The way to have multiple directories in your jar.libs.dir is to not set it directly; let it default to libs and go to libs and create the required soft links to .jar files you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM