我最终弄清楚了这一点。我最终做的是使用 Eclipse 导出工具为三个 Java 库项目导出一个 ant 构建脚本。然后我创建了一个名为 build-custom.xml 的文件,其中包含用于复制 jar 的其他目标。
接下来,我在 Android 库项目上使用 android update lib-project 命令为该项目生成 build.xml(如http://developer.android.com/tools/projects/projects-cmdline.html中所指定)。为了在构建中包含 Java 库项目,您必须将它们复制到 Android 项目的 libs 文件夹中。所以我在 Android 库项目中创建了一个名为 custom_rules.xml 的构建文件,将 jar 复制到 lib 文件夹中。重要提示:还要编写一个规则,在完成后删除 jar,否则当您尝试在 Eclipse 中运行时,您将收到 Davlik Error 1。
您可能有也可能没有 Android 库项目,但主项目的步骤与库项目的步骤几乎相同。只需使用 android update project 命令,然后创建一个 custom_rules.xml 来复制相应的 jar。
编辑:
在使用 Eclipse 生成基本 Java 库项目的构建脚本后,我创建了一个 common.xml ant 构建文件。这个文件基本上是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="common">
<target name="clean">
<delete dir="build/libs" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary1" target="clean" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary1" target="cleanup-jars" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary2" target="clean" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary2" target="cleanup-jars" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary3" target="clean" />
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary3" target="cleanup-jars" />
<ant antfile="build.xml" dir="./AndroidSubProject" target="clean" />
</target>
<target name="samplesublibrary1">
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary1" target="all" />
</target>
<target name="samplesublibrary2">
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary2" target="all" />
</target>
<target name="samplesublibrary3" depends="samplesublibrary2">
<ant antfile="build.xml" dir="./SubJavaLibrary/SampleSubLibrary3" target="all" />
</target>
<target name="android-sub-project-copy-jars" depends="samplesublibrary1, samplesublibrary2, samplesublibrary3">
<ant antfile="build.xml" dir="./AndroidSubProject" target="android-project-copy-jars" />
</target>
<target name="android-sub-project-debug" depends="android-project-copy-jars">
<ant antfile="build.xml" dir="./AndroidSubProject" target="debug" />
</target>
<target name="android-sub-project-release" depends="android-project-copy-jars">
<ant antfile="build.xml" dir="./AndroidSubProject" target="release" />
</target>
<target name="android-sub-project-remove-jars">
<ant antfile="build.xml" dir="./AndroidSubProject" target="android-project-remove-jars" />
</target>
</project>
然后在 Android-Sub-Project 中,我添加了一个 custom_rules.xml 文件,其中包含以下内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse.ant.import?>
<project default="android-project-copy-jars" name="Copy required jars to lib folder">
<target name="android-project-copy-jars">
<copy file="../../build/libs/SampleSubLibrary1.jar" todir="libs"/>
<copy file="../../build/libs/SampleSubLibrary2.jar" todir="libs"/>
<copy file="../../build/libs/SampleSubLibrary3.jar" todir="libs"/>
</target>
<target name="android-project-remove-jars">
<delete file="./libs/SampleSubLibrary1.jar" />
<delete file="./libs/SampleSubLibrary2.jar" />
<delete file="./libs/SampleSubLibrary3.jar" />
</target>
</project>
然后,我在项目根目录中创建了一个主 build.xml,其中包含并运行所有其他子 build.xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="all">
<target name="debug" depends="clean, main-debug, android-project-remove-jars" />
<target name="release" depends="clean, main-release, android-project-remove-jars" />
<import file="common.xml" />
<target name="clean" depends="common.clean">
<ant antfile="build.xml" dir="./Main" target="clean" />
</target>
<target name="main-debug" depends="android-sub-project-debug">
<ant antfile="build.xml" dir="./Main" target="debug" />
<copy file="./Main/bin/main-debug.apk" todir="./build/bin" />
</target>
<target name="main-release" depends="android-sub-project-release">
<ant antfile="build.xml" dir="./Main" target="release" />
<copy file="./Main/bin/main-release-unsigned.apk" todir="./build/bin" />
</target>
</project>
希望这可以帮助!