Android Ant Build

Put your problem here if it does not fit any of the other categories.

Android Ant Build

Postby awilhelm » Thu Mar 29, 2012 11:35 pm

Hello Androiders!

I would like to build two versions (full and lite) of my app using an Apache Ant script. Both versions will be identical except the advertisement in the lite version. So I thought it would be the best to copy and refactor the code automatically before building it. But now I have some problems.

  1. Is it enough to replace the package name in the Manifest.xml and all Java files?
  2. How to share the resources and build the resource file R.java for both versions?
  3. How to create the *.apk files for both versions?
  4. How to sign both packages?
  5. How to enable and disable the advertisement block using ant?

For know I stuck with the following build.xml.

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<project name="my.project" default="precommit" basedir="." xmlns:android="antlib:com.googlecode.autoandroid.ant">

   <!-- Requires ant 1.7 or better -->
   <taskdef uri="antlib:com.googlecode.autoandroid.ant" classpath="lib/android-ant.jar"/>
   <android:home/> <!-- Set the detected android-home property to the sdk root. -->

   <!-- Load the custom property files -->
   <property file="build.properties" />
   <property file="passwords.properties" />

   <!-- 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" />

   <!-- Set global properties for this build -->
   <property name="my.project.pkg" value="my.project"/>
   <property name="my.project.pkg.full" value="my.project.full"/>
   <property name="my.project.pkg.lite" value="my.project.lite"/>
   
   <property name="my.project" location="."/>
   <property name="my.project.build" location="build"/>
   <property name="my.project.src" location="src"/>
   <property name="my.project.res" location="res"/>
   <property name="my.project.gen" location="gen"/>
   
   <property name="my.project.full" location="full"/>
   <property name="my.project.full.src" location="full/src"/>
   <property name="my.project.full.res" location="full/res"/>
   <property name="my.project.full.gen" location="full/gen"/>
   <property name="my.project.full.build" location="full/build"/>
   
   <property name="my.project.lite" location="lite"/>
   <property name="my.project.lite.build" location="lite/build"/>
   <property name="my.project.lite.src" location="lite/src"/>
   <property name="my.project.lite.res" location="lite/res"/>
   <property name="my.project.lite.gen" location="lite/gen"/>

   <!-- 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" />

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

   <!-- Creates all needed directories -->
    <target name="makedir">
      <!-- Creates directories for the full version -->
        <mkdir dir="${my.project.full}" />
        <mkdir dir="${my.project.full.src}" />
        <mkdir dir="${my.project.full.build}" />
      <!-- Creates directories for the lite version -->
        <mkdir dir="${my.project.lite}" />
        <mkdir dir="${my.project.lite.src}" />
        <mkdir dir="${my.project.lite.build}" />
    </target>

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

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

   <target name="release-Lite">
      <ant antfile="build.xml" inheritAll="true" target="lite">
         <property name="MyCustomProperty" value="Lite" />
      </ant>
   </target>
   
   <target name="prepare" depends="clean, makedir" >
      <!-- Copy the Manifest.xml to the full copy -->
      <copyfile src="${my.project}/AndroidManifest.xml"
         dest="${my.project.full}/AndroidManifest.xml" />
      
       <!-- Copy the source files to the full copy -->
      <copy todir="${my.project.full.src}" overwrite="true">
         <fileset dir="${my.project.src}" />
      </copy>
      
       <!-- Copy the resources to the full copy -->
      <copy todir="${my.project.full.res}" overwrite="true">
         <fileset dir="${my.project.res}" />
      </copy>
      
       <!-- Copy the generated to the full copy -->
      <copy todir="${my.project.full.gen}" overwrite="true">
         <fileset dir="${my.project.gen}" />
      </copy>
      
      <!-- Replace the package name in the full manifest file -->
      <replaceregexp file="${my.project.full}/AndroidManifest.xml"
         match='package="(.*)"'
         replace='package="${my.project.pkg.full}"'
         byline="false" />
      
      <!-- Change the package name in all Java files -->
      <replaceregexp flags="g" byline="false">
         <regexp pattern="${my.project.pkg}" />
         <substitution expression="${my.project.pkg.full}" />
         <fileset dir="${my.project.full.src}" includes="**/*.java" />
      </replaceregexp>
      
      <!-- Copy the Manifest.xml to the lite copy -->
      <copyfile src="${my.project}/AndroidManifest.xml"
         dest="${my.project.lite}/AndroidManifest.xml" />
      
       <!-- Copy the source files to the lite copy -->
      <copy todir="${my.project.lite.src}" overwrite="true">
         <fileset dir="${my.project.src}" />
      </copy>
      
       <!-- Copy the resources to the lite copy -->
      <copy todir="${my.project.lite.res}" overwrite="true">
         <fileset dir="${my.project.res}" />
      </copy>
      
       <!-- Copy the generated to the lite copy -->
      <copy todir="${my.project.lite.gen}" overwrite="true">
         <fileset dir="${my.project.gen}" />
      </copy>

      <!-- Replace the package name in the lite manifest file -->
      <replaceregexp file="${my.project.lite}/AndroidManifest.xml"
         match='package="(.*)"'
         replace='package="${my.project.pkg.lite}"'
         byline="false" />
      
      <!-- Change the package name in all Java files -->
      <replaceregexp flags="g" byline="false">
         <regexp pattern="${my.project.pkg}" />
         <substitution expression="${my.project.pkg.lite}" />
         <fileset dir="${my.project.lite.src}" includes="**/*.java" />
      </replaceregexp>
   </target>
</project>


Best regards

Andreas
awilhelm
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Mar 29, 2012 11:16 pm

Top

Re: Android Ant Build

Postby Martin_78 » Thu Jun 14, 2012 8:03 am

Martin_78
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 13, 2012 7:35 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 9 guests