Search code examples
androidandroid-build

Android: How to pass version info during ant command line build?


When I build an Android project in command line using Ant, I would like to update the android:versionCode and android:versionName in AndroidManifest.xml file. Are there someways I can inject version number using a property file?


Solution

  • You can set version info in several ways,

    1. Passing as command line parameters
    2. Using a property file.

    To pass as command line parameters,

     <target name="set-version-using-commandline-args">
        <!-- Load properties from "version.properties" file -->     
        <replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)" 
            replace='android:versionCode="${Version.Code}"'/>
        <replaceregexp file="AndroidManifest.xml" match="android:versionName(.*)" 
            replace='android:versionName="${Version.Name}"'/>       
    </target>
    

    Then run the ant build like this,

    ant -DVersion.Code=100 -DVersion.Name=5.0.0.1201011 debug 
    

    If you want to pass the version info using a property file, use this target,

     <target name="set-version-using-file">
        <!-- Load properties from "version.properties" file -->             
        <property file="version.properties" />
    
        <replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)"
            replace='android:versionCode="${Version.Code}"'/>
        <replaceregexp file="AndroidManifest.xml" match="android:versionName(.*)"
            replace='android:versionName="${Version.Name}"'/>       
    </target>
    

    For detailed instruction, see this blog post. Android: How to version command line build?