3

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?

4

3 に答える 3

9

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?

于 2013-01-07T19:56:10.350 に答える
2

Sure! First, create your property file. Say, spiffy.properties. Next, you're going to make use of the Android Ant build's custom_rules.xml file. Create it, if you don't already have one.

Near the top of that file, add a line that looks like this:

<property file="spiffy.properties"/>

Now, add a dependency to the -pre-build target to call this:

<target name="-set-manifest-values">
  <replaceregexp file="AndroidManifest.xml">
    <regexp pattern="android:versionName=&quot;.*&quot;"/>
    <substitution expression="android:versionName=&quot;${version.name}&quot;"/>
  </replaceregexp>
  <replaceregexp file="AndroidManifest.xml">
    <regexp pattern="android:versionCode=&quot;.*&quot;"/>
      <substitution expression="android:versionCode=&quot;${build.number}&quot;"/>
  </replaceregexp>
</target>

With your version name and build number being specifed by ${version.name} and ${build.number} respectively. Since they're properties, you can also specify them on the command-line or as part of a continuous integration setup.

于 2013-01-07T19:58:38.003 に答える
0

Updated AndroidManifest via Ruby, see https://gist.github.com/cybertk/24ce4d20d76f9d6a16c6

    File.open('AndroidManifest.xml', 'r+') do |f|
        manifest = f.read

        build = ENV['TRAVIS_BUILD_NUMBER'] || 0
        # Update version
        manifest = manifest.gsub(
            /android:versionCode=".*"/, "android:versionCode=\"#{build}\"")

        # Write back
        f.rewind
        f.truncate(0)
        f.write(manifest)
    end
于 2014-10-22T09:01:40.423 に答える