Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Ant Build

I would like to build two versions of my Androidapplication using an Apache ant file. The problem is, that both versions are identical except the advertisement in the lite version. I read about using Configurations with ant to build debug versions.

The following class defines some constants that can be referenced within the application.

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

And here is an example on how to use this constants to determine if logging is enabled or not.

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

Now i can enable and disable logging in my properties file.

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

That does not work, because before using this config I have to create a second config file and use filterset and copy.

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

That's pretty easy, but how I could use this to build two versions of my application with and without advertisement. And how could I change the package names using ant, so the android market would accept both packages (Full and Lite).


Thank you, for your suggestions, but I still have some problems.

I managed to write some basic targets that cleanup my builds and copy all files needed to build the application in two folders /full and /lite. So I have two directories with the same content. Now I rename all matches of the applications package name in all *.java files and the AndroidManifest file (target prepare).

To really build two different version I would now have to include the code from my first post. But how do I have to do this and how can I build both versions in the release target and write the resulting *.apk files into the build directoy?

Finally ... Would that be all I have to do to build running *.apks that would be accepted by the android market?

<?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>
like image 503
Phidelux Avatar asked Oct 23 '22 14:10

Phidelux


1 Answers

There are a number of ways in which you could achieve what you require.

Here are a couple of ideas that I have used in the past,

1) Have two application 'heads' that pull in a common Android library. Each head initializes static data that sets up the library to behave as either the lite or the full version of your application. This has the advantage that you can perform the build from Eclipse projects as well as with Ant.

2) Have two seperate build targets that share common build targets to create two seperate apk files. In the Ant build script have it build two versions of the APK. One that is the full version and then the other which builds the lite version. The difference between the two targets are that they build using slightly different files (either by copying, directing to diferent directories or modifying with scripts).

This can all be done in Ant using targets and properties.

If at the top level of your build you have a release target depending on two other targets.

e.g.

<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>

You can then use these targets and properties to modify your build to do whatever you require to build each of the APK files.

like image 147
quickdraw mcgraw Avatar answered Oct 29 '22 19:10

quickdraw mcgraw