Android Ant Include Java Library 项目 [英] Android Ant Include Java Library projects

查看:24
本文介绍了Android Ant Include Java Library 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ant 编译一个 Android 项目,以便它与构建机器一起工作,但我遇到了问题.我总共有五个项目:三个只是 java 库项目,另外两个是实际的 Android 项目.实际上只有一个项目被编译和安装,但它从其他项目中拉取以进行编译.

I am trying to compile an Android project using Ant so that it works with a build machine and I am having issues. I have five projects total: three are just java library projects and the other two are actual Android projects. Only one project is actually compiled and installed but it pulls from the other projects in order to compile.

我尝试过使用 android update project --name --目标<目标>--path 生成我的 build.xml 文件.它生成的 build.xml 很好,但是当我去运行它时,它不能正确地包含我的其他项目作为依赖项.

I have tried using the android update project --name <name> --target <target> --path <path> to generate my build.xml file. It generates the build.xml just fine but when I go to run it, it can't correctly include my other projects as dependencies.

然后我尝试使用 Eclipse 导出 build.xml 文件,这将正确包含依赖项,但不会生成我的 R.java 文件.

I then tried to export the build.xml file using Eclipse and that will correctly include the dependencies but it won't generate my R.java files.

我更喜欢第二种方法,因为 Eclipse 已经处理了我的依赖项设置,但是有什么我可以添加到 build.xml 文件中以正确生成我的 R.java 文件的吗?

I would prefer the second approach because Eclipse has already taken care of my dependency settings, but is there something I can add to the build.xml file that will generate my R.java file correctly?

推荐答案

我最终解决了这个问题.我最终做的是使用 Eclipse 导出工具为三个 Java 库项目导出一个 ant 构建脚本.然后我创建了一个名为 build-custom.xml 的文件,其中包含用于复制 jar 的其他目标.

I ended up figuring this out. What I ended up doing is use the Eclipse export tool to export an ant build script for the three Java library projects. I then created a file called build-custom.xml which contains additional targets for copying the jars.

接下来,我在 Android 库项目上使用 android update lib-project 命令为该项目生成 build.xml(如 http://developer.android.com/tools/projects/projects-cmdline.html).为了在构建中包含 Java 库项目,您必须将它们复制到 Android 项目的 libs 文件夹中.所以我在 Android 库项目中创建了一个名为 custom_rules.xml 的构建文件,将 jars 复制到 lib 文件夹中.重要提示:还要编写一个规则,在完成后删除 jar,否则当您尝试在 Eclipse 中运行时,您将收到 Davlik 错误 1.

Next, I used the android update lib-project command on the Android library project to generate the build.xml for that project (as specified in http://developer.android.com/tools/projects/projects-cmdline.html). In order to include the Java library projects in the build, you have to copy them to the libs folder in the Android project. So I created a build file called custom_rules.xml in the Android library project that copys the jars into the lib folder. IMPORTANT: Also write a rule that removes the jars when done otherwise you will get Davlik Error 1 when you try and run in Eclipse.

您可能有也可能没有 Android 库项目,但主项目的步骤与库项目几乎相同.只需使用 android update project 命令,然后创建一个 custom_rules.xml 来复制相应的 jar.

You may or may not have an Android library project but the steps are pretty much the same for the main project as the library project. Just use the android update project command and then create a custom_rules.xml to copy the appropriate jars.

在我使用 Eclipse 为基本 Java 库项目生成构建脚本之后,我创建了一个 common.xml ant 构建文件.这个文件基本上是这样的:

After I used Eclipse to generate the build scripts for the basic Java library projects, I created a common.xml ant build file. This file basically looks like this:

<?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 文件:

Then in the Android-Sub-Project, I added a custom_rules.xml file that contained the following:

<?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 文件:

I then created a main build.xml in the project root that includes and runs all the other sub build.xml files:

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

希望这会有所帮助!

这篇关于Android Ant Include Java Library 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆