仅使用ant构建android应用程序

时间:2013-01-24 18:54:43

标签: android ant

我正在尝试通过ant构建简单的android项目。这是我的蚂蚁脚本

<?xml version="1.0" encoding="UTF-8"?>
    <project name="HelloWorld" default="help">

    <!-- Input directories -->
    <property name="source.dir" value="src" />
    <property name="source.absolute.dir" location="${source.dir}" />
    <property name="gen.dir" value="gen" />
    <property name="gen.absolute.dir" location="${gen.dir}" />
    <property name="resource.dir" value="res" />
    <property name="resource.absolute.dir" location="${resource.dir}" />
    <property name="asset.dir" value="assets" />
    <property name="asset.absolute.dir" location="${asset.dir}" />

    <!-- Directory for the third party java libraries -->
    <property name="external.libs.dir" value="libs" />
    <property name="external.libs.absolute.dir" location="${external.libs.dir}" />

    <!-- Output directories -->
    <property name="out.dir" value="ant"/>
    <property name="out.absolute.dir" location="${out.dir}/bin"/>
    <property name="out.classes.absolute.dir" value="${out.absolute.dir}/classes"/>

    <property name="sdk.dir" value="../../AndroidSDK"/>
    <property name="sdk.absolute.dir" location="${sdk.dir}"/>

    <property name="android.jar" value="${sdk.absolute.dir}/platforms/android-8"/>

    <!-- The final package file to generate -->
    <property name="out.debug.unaligned.package"
                  location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
    <property name="out.debug.package"
                  location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
    <property name="out.unsigned.package"
                  location="${out.absolute.dir}/${ant.project.name}-unsigned.apk" />
    <property name="out.unaligned.package"
                  location="${out.absolute.dir}/${ant.project.name}-unaligned.apk" />
    <property name="out.release.package"
                  location="${out.absolute.dir}/${ant.project.name}-release.apk" />

    <!-- Verbosity -->
    <property name="verbose" value="true" />

    <condition property="v.option" value="-v" else="">
        <istrue value="${verbose}" />
    </condition>

    <!-- tools location -->
    <property name="android.tools.dir" location="${sdk.absolute.dir}/tools" />
    <property name="android.platform.tools.dir" location="${sdk.absolute.dir}/platform-tools" />
    <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
    <condition property="bat" value=".bat" else=""><os family="windows" /></condition>
    <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />
    <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
    <property name="aidl" location="${android.platform.tools.dir}/aidl${exe}" />
    <property name="aapt" location="${android.platform.tools.dir}/aapt${exe}" />
    <property name="dx" location="${android.platform.tools.dir}/dx${bat}" />

    <!-- Generates the R.java file for this project's resources. -->
    <target name="resource-src" depends="dirs">
        <echo>Generating R.java, Manifest.java from the resources...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package" />
            <arg line="${v.option}" />
            <arg value="-m" />
            <arg value="-J" />
            <arg path="${gen.absolute.dir}" />
            <arg value="-M" />
            <arg path="AndroidManifest.xml" />
            <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-I" />
            <arg path="${android.jar}" />
        </exec>
    </target>

    <!-- Generates java classes from .aidl files. -->
    <target name="aidl" depends="dirs">
        <echo>Compiling aidl files into Java classes...</echo>
        <apply executable="${aidl}" failonerror="true">
            <arg value="-p${android.aidl}" />
            <arg value="-I${source.absolute.dir}" />
            <arg value="-o${gen.absolute.dir}" />
            <fileset dir="${source.absolute.dir}">
                <include name="**/*.aidl" />
            </fileset>
        </apply>
    </target>

    <target name="compile" depends="resource-src, aidl" description="Compiles project's .java files into .class files">
        <javac destdir="${classes.absolute.dir}" >
            <src path="${source.absolute.dir}" />
            <src path="${gen.absolute.dir}" />

            <classpath>
                <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
            </classpath>
        </javac>
    </target>

    <target name="clean" description="Removes output files created by other targets.">
        <delete dir="${out.absolute.dir}" verbose="${verbose}" />
        <delete dir="${gen.absolute.dir}" verbose="${verbose}" />
    </target>

    <!-- Creates the output directories if they don't exist yet. -->
    <target name="dirs">
        <echo>Creating output directories if needed...</echo>
        <mkdir dir="${resource.absolute.dir}" />
        <mkdir dir="${external.libs.absolute.dir}" />
        <mkdir dir="${gen.absolute.dir}" />
        <mkdir dir="${out.absolute.dir}" />
        <mkdir dir="${out.classes.absolute.dir}" />
    </target>

</project>

此脚本在代码块<target name="resource-src" depends="dirs">上失败,我收到以下消息: 配置:D:\ Development \ Eclipse_workspace \ HelloWorld \ res \ values \ styles.xml:7:错误:检索项目的父项时出错:找不到与给定名称“android:Theme.Light”匹配的资源 D:\ Development \ Eclipse_workspace \ HelloWorld \ res \ values-v11 \ styles.xml:7:错误:检索项目的父项时出错:找不到与给定名称“android:Theme.Holo.Light”匹配的资源。 D:\ Development \ Eclipse_workspace \ HelloWorld \ res \ values-v14 \ styles.xml:8:错误:检索项目的父项时出错:找不到与给定名称“android:Theme.Holo.Light.DarkActionBar”匹配的资源。< / p>

如果我使用eclipse构建应用程序 - 没关系,应用程序构建正常。

那么,出了什么问题?我该如何解决?

2 个答案:

答案 0 :(得分:6)

你这样做很难。 android有工具为你的项目创建ant构建脚本。只需从项目文件夹中运行它,

android update project --path .

android命令在<sdk folder>/tools)。

这将创建一个build.xml和其他几个附带的属性文件。有关android命令的详细用法,请参阅documentation on managing projects from the command line

答案 1 :(得分:0)

假设您已安装Ant .Go到Android SDK \ tools文件夹并执行以下命令

  

android更新项目--target android-18 --path“项目路径”   它将生成build.xml文件。