如何从命令行将外部 jar 库添加到 android 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3217643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add external jar libraries to an android project from the command line
提问by Jay K
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.
我正在尝试构建一个具有一些依赖项的 Android 项目。jar 文件位于 lib/ 目录中。我可以通过将这些 jar 文件添加到我的类路径来构建项目,但当然它会在模拟器中强制关闭,因为这些库不存在。
I'm doing this from the command line with ant (notin eclipse). How can I make it include those libraries in the apk from the command line?
我正在使用 ant(不在eclipse 中)从命令行执行此操作。如何从命令行将这些库包含在 apk 中?
采纳答案by Nicolas Raoul
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:
在撰写本文时,Jay K 的回答是正确的,但现在 SDK 发生了变化,这是一种新的方法:
Add the following line to ant.properties
:
将以下行添加到ant.properties
:
jar.libs.dir=lib
Tested, works where external.libs.dir does not work.
That changed in December 2010and then again in October 2011.
经过测试,可以在 external.libs.dir 不起作用的情况下工作。
这种情况在 2010 年 12 月和2011 年 10 月发生了变化。
回答by Panayotis
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
仅供将来参考:现在 SDK 中存在一个错误,无法使用 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:
再次使用 jar.libs.dir 的简单解决方案(在等待错误修复时)是在 build.xml 文件中添加此目标:
<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>
回答by Cristian
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:
并且,当您为 Dalvik 生成字节码时:
dx --dex --output=bin/classes.dex bin/classes libs/*.jar
Also, when you are packaging the APK file:
此外,当您打包 APK 文件时:
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.
我假设您使用的是 Linux……在 Windows 上几乎相同。
回答by Jay K
Answering my own question:
回答我自己的问题:
I created a file "build.properties" and added the line
我创建了一个文件“build.properties”并添加了这一行
external.libs.dir=lib
It was a lucky guess, really.
这是一个幸运的猜测,真的。
回答by Akos Cz
The example above dosen't quite work right. You cannot overwrite the project.all.jars.path inline. Following is a working example :
上面的例子并不完全正确。您不能内联覆盖 project.all.jars.path。以下是一个工作示例:
<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>
回答by Peter Kahn
If you store your libraries in ${basedir}/libs the ant script will be able to find them.
如果您将库存储在 ${basedir}/libs 中,ant 脚本将能够找到它们。
I looked at the current ant script ( ${sdk.dir}/tools/ant/build.xml ) and it defines jar.libs.dir to "libs".
我查看了当前的 ant 脚本( ${sdk.dir}/tools/ant/build.xml )并将 jar.libs.dir 定义为“libs”。
FYI, the ant script is well documentated so it's easily to trace through.
仅供参考,ant 脚本有很好的文档记录,因此很容易追踪。
If you move libs around be sure to edit your .classpath files to prevent eclipse users from suffering needlessly.
如果您四处移动库,请务必编辑您的 .classpath 文件,以防止 eclipse 用户遭受不必要的痛苦。
回答by shaobin0604
put jars in ${YOUR_PROJECT}/libs
directory
将 jars 放入${YOUR_PROJECT}/libs
目录
回答by gpacer68
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.
在寻找这个答案 2 小时后,这对我有用。我的 libs 目录中的 jar 文件最终编译并与 APK 捆绑在一起。
回答by Scott C Wilson
The jar.libs.dir
property is a singledirectory. 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.
该jar.libs.dir
属性是单个目录。它不像一个 CLASSPATH,它是一个冒号/分号分隔的列表。在你的 jar.libs.dir 中有多个目录的方法是不要直接设置;让它默认libs
并转到 libs 并创建所需的软链接到您需要的 .jar 文件。