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?
3 に答える
You can set version info in several ways,
- Passing as command line parameters
- 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?
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=".*""/>
<substitution expression="android:versionName="${version.name}""/>
</replaceregexp>
<replaceregexp file="AndroidManifest.xml">
<regexp pattern="android:versionCode=".*""/>
<substitution expression="android:versionCode="${build.number}""/>
</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.
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