2011-12-22 74 views
5

我想使用Apache ant文件构建两个版本的我的Android应用程序。问题是,除了lite版本中的广告外,两个版本都是相同的。我读了关于使用Configurations与蚂蚁对build调试版本。Android 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 = @[email protected]; 
} 

这很容易,但我怎么能使用它来建立我的应用程序的两个版本,没有广告。我怎样才能使用ant来更改软件包名称,因此android market将接受这两个软件包(Full和Lite)。


谢谢您的建议,但我仍然有一些问题。

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

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

终于......我需要做的就是构建可以被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> 
+0

您是否找到任何解决方案?我陷入了同样的问题。如果你解决这个问题,你愿意分享吗? – Mac 2012-11-06 03:59:48

+0

不是。我部署了没有轻量版本的程序。但我会很乐意找到一个可行的解决方案。 – Phidelux 2012-12-07 07:52:48

回答

3

有很多方法可以实现您的要求。

这里有一对夫妇的想法,我在过去使用,

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

2)有两个独立的构建目标,共享共同的构建目标来创建两个独立的apk文件。 在Ant构建脚本中,它构建了APK的两个版本。 一个是完整版本,另一个是构建lite版本的。 两个目标之间的区别在于,它们使用略微不同的文件(通过复制,指向不同目录或使用脚本进行修改)构建。

这可以使用目标和属性在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文件。