【问题标题】:Android Ant BuildAndroid Ant 构建
【发布时间】:2012-01-26 03:31:14
【问题描述】:

我想使用Apache ant 文件构建我的Androidapplication 的两个版本。问题是,除了精简版中的广告外,两个版本都是相同的。我读到过使用 Configurations 和 ant 到 build 调试版本。

以下类定义了一些可以在应用程序中引用的常量。

public class Config {
    // Whether or not to include logging in the app.
    public final static boolean LOGGING = true;
}

下面是一个示例,说明如何使用此常量来确定是否启用了日志记录。

if (Config.LOGGING) {
    Log.d(TAG, "[onCreate] Success");
}

现在我可以在我的属性文件中启用和禁用日志记录。

# Turn on or off logging.
config.logging=true

这不起作用,因为在使用此配置之前,我必须创建第二个配置文件并使用 filterset 并复制。

public class Config {
    // Whether or not to include logging in the app.
    public final static boolean LOGGING = @CONFIG.LOGGING@;
}

这很简单,但是我如何使用它来构建我的应用程序的两个版本,有广告和没有广告。以及如何使用 ant 更改包名称,因此 android market 将接受两个包(完整和精简版)。


谢谢你的建议,但我还是有一些问题。

我设法编写了一些基本目标来清理我的构建并将构建应用程序所需的所有文件复制到 /full 和 /lite 两个文件夹中。所以我有两个内容相同的目录。现在我重命名所有 *.java 文件和 AndroidManifest 文件(目标准备)中的应用程序包名称的所有匹配项。

要真正构建两个不同的版本,我现在必须包含我第一篇文章中的代码。但是我该怎么做?如何在发布目标中构建这两个版本并将生成的 *.apk 文件写入构建目录?

最后......我要做的就是构建运行的 *.apks 并被 android 市场接受吗?

<?xml version="1.0" encoding="UTF-8"?>
<project name="my.application" default="help" basedir=".">
    <!-- Load the custom property files -->
    <property file="build.properties" />
    <property file="passwords.properties" />

    <!-- Set global properties for this build -->
    <property name="my.application.pkg" value="my.application"/>
    <property name="my.application.pkg.full" value="my.application.full"/>
    <property name="my.application.pkg.lite" value="my.application.lite"/>

    <property name="my.application" location="."/>
    <property name="my.application.build" location="build"/>
    <property name="my.application.src" location="src"/>
    <property name="my.application.res" location="res"/>
    <property name="my.application.gen" location="gen"/>

    <property name="my.application.full" location="full"/>
    <property name="my.application.full.src" location="full/src"/>
    <property name="my.application.full.res" location="full/res"/>
    <property name="my.application.full.gen" location="full/gen"/>
    <property name="my.application.full.build" location="full/build"/>

    <property name="my.application.lite" location="lite"/>
    <property name="my.application.lite.build" location="lite/build"/>
    <property name="my.application.lite.src" location="lite/src"/>
    <property name="my.application.lite.res" location="lite/res"/>
    <property name="my.application.lite.gen" location="lite/gen"/>

    <!-- Create and update the local.properties file -->
    <loadproperties srcFile="local.properties" />

    <!-- Load the ant.properties file -->
    <property file="ant.properties" />

    <!-- Load the project.properties file -->
    <loadproperties srcFile="project.properties" />

    <!-- Quick check on sdk.dir. -->
    <fail
        message="sdk.dir is missing."
        unless="sdk.dir" />

    <!-- Version-tag: 1 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

    <target name="release" depends="report, prepare">
        <echo>Building the target!</echo>
    </target>

    <target name="prepare" depends="cleanup" >
        <!-- Copy the Manifest.xml to the full copy -->
        <copyfile src="${my.application}/AndroidManifest.xml" 
            dest="${my.application.full}/AndroidManifest.xml" />

        <!-- Copy the source files to the full copy -->
        <copy todir="${my.application.full.src}" overwrite="true">
            <fileset dir="${my.application.src}" /> 
        </copy>

        <!-- Copy the resources to the full copy -->
        <copy todir="${my.application.full.res}" overwrite="true">
            <fileset dir="${my.application.res}" /> 
        </copy>

        <!-- Copy the generated to the full copy -->
        <copy todir="${my.application.full.gen}" overwrite="true">
            <fileset dir="${my.application.gen}" /> 
        </copy>

        <!-- Replace the package name in the full manifest file -->
        <replaceregexp file="${my.application.full}/AndroidManifest.xml"
            match='package="(.*)"'
            replace='package="${my.application.pkg.full}"'
            byline="false" />

        <!-- Change the package name in all Java files -->
        <replaceregexp flags="g" byline="false">
            <regexp pattern="${my.application.pkg}" /> 
            <substitution expression="${my.application.pkg.full}" />
            <fileset dir="${my.application.full.src}" includes="**/*.java" /> 
        </replaceregexp>

        <!-- Copy the Manifest.xml to the lite copy -->
        <copyfile src="${my.application}/AndroidManifest.xml" 
            dest="${my.application.lite}/AndroidManifest.xml" />

        <!-- Copy the source files to the lite copy -->
        <copy todir="${my.application.lite.src}" overwrite="true">
            <fileset dir="${my.application.src}" /> 
        </copy>

        <!-- Copy the resources to the lite copy -->
        <copy todir="${my.application.lite.res}" overwrite="true">
            <fileset dir="${my.application.res}" /> 
        </copy>

        <!-- Copy the generated to the lite copy -->
        <copy todir="${my.application.lite.gen}" overwrite="true">
            <fileset dir="${my.application.gen}" /> 
        </copy>

        <!-- Replace the package name in the lite manifest file -->
        <replaceregexp file="${my.application.lite}/AndroidManifest.xml"
            match='package="(.*)"'
            replace='package="${my.application.pkg.lite}"'
            byline="false" />

        <!-- Change the package name in all Java files -->
        <replaceregexp flags="g" byline="false">
            <regexp pattern="${my.application.pkg}" /> 
            <substitution expression="${my.application.pkg.lite}" />
            <fileset dir="${my.application.lite.src}" includes="**/*.java" /> 
        </replaceregexp>
    </target>

    <!-- Deletes all directories, not needed anymore after compiling the source files -->
    <target name="cleanup">
        <!-- Delete the full version build dir -->
        <delete dir="${my.application.full}"/>
        <!-- Delete the lite version build dir -->
        <delete dir="${my.application.lite}"/>
        <!-- Delete the *.apk file -->
        <delete file="my.application.full.apk"/>
        <!-- Delete the *.apk file -->
        <delete file="my.application.lite.apk"/>
    </target>
</project>

【问题讨论】:

  • 您找到解决方案了吗?我被困在同样的问题上。如果你解决了这个问题,请分享一下吗?
  • 并非如此。我部署了没有轻量版的程序。但我很乐意找到可行的解决方案。

标签: java android apache ant build


【解决方案1】:

您可以通过多种方式实现您的要求。

这是我过去使用的一些想法,

1) 有两个应用程序“头”,它们拉入一个通用的 Android 库。 每个头初始化静态数据,这些数据将库设置为应用程序的精简版或完整版。 这样做的好处是您可以从 Eclipse 项目以及使用 Ant 执行构建。

2) 有两个单独的构建目标,它们共享共同的构建目标来创建两个单独的 apk 文件。 在 Ant 构建脚本中有它构建两个版本的 APK。 一个是完整版,另一个是构建精简版。 这两个目标之间的区别在于它们使用稍微不同的文件构建(通过复制、定向到不同的目录或使用脚本修改)。

这一切都可以在 Ant 中使用目标和属性来完成。

如果您在构建的顶层有一个依赖于其他两个目标的发布目标。

例如

<target name="release"
  depends="release-Full, release-Lite">
</target>

<target name="release-Full">
  <ant antfile="thisbuild.xml" inheritAll="true" target="full">
    <property name="MyCustomProperty" value="Full" />
  </ant>
</target>



<target name="release-Lite">
  <ant antfile="thisbuild.xml" inheritAll="true" target="lite">
    <property name="MyCustomProperty" value="Lite" />
   </ant>
 </target>

然后,您可以使用这些目标和属性来修改您的构建,以执行构建每个 APK 文件所需的任何操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2012-05-31
    • 2011-11-06
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多