Android Ant Build

Phi*_*lux 5 java apache ant android build

我想Android使用一个Apache ant文件构建我的应用程序的两个版本.问题是,除了lite版本中的广告之外,两个版本都是相同的.我读到了使用Configurationsant来build调试版本.

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

public class Config {
    // Whether or not to include logging in the app.
    public final static boolean LOGGING = true;
}
Run Code Online (Sandbox Code Playgroud)

这里有一个示例,说明如何使用此常量来确定是否启用了日志记录.

if (Config.LOGGING) {
    Log.d(TAG, "[onCreate] Success");
}
Run Code Online (Sandbox Code Playgroud)

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

# Turn on or off logging.
config.logging=true
Run Code Online (Sandbox Code Playgroud)

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

public class Config {
    // Whether or not to include logging in the app.
    public final static boolean LOGGING = @CONFIG.LOGGING@;
}
Run Code Online (Sandbox Code Playgroud)

这很简单,但我如何使用它来构建我的应用程序的两个版本,有或没有广告.如何使用ant更改包名称,因此android market可以接受两个包(Full和Lite).


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

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

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

最后......难道我需要做的就是构建可以被Android市场接受的运行*.apks吗?

<?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>
Run Code Online (Sandbox Code Playgroud)

qui*_*raw 3

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

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

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>
Run Code Online (Sandbox Code Playgroud)

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