安卓Ant构建

5
我想使用一个 Apache ant 文件构建两个版本的 Android 应用程序。问题是,除了 Lite 版本中的广告之外,两个版本都完全相同。我了解到可以使用 ant 中的 Configurations 来构建 debug 版本。
下面的类定义了一些常量,可在应用程序中引用。
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文件写入构建目录?
最后...这就是我构建可由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>

你找到解决方案了吗?我也遇到了同样的问题。如果你已经解决了,能否分享一下? - Mac
并不是。我没有部署轻量版程序。但如果能找到一个可行的解决方案,我会很高兴的。 - Phidelux
1个回答

3
有几种方法可以实现您所需的功能。
以下是我过去使用过的几个想法:
1)有两个应用程序“头”,它们引入一个共同的Android库。 每个头初始化静态数据,将库设置为行为作为您的应用程序的lite版或完整版。 这样做的好处是您可以从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文件所需的任何操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接