Reference an integer resource for Android manifest

2019-02-14 08:22发布

问题:

In my Android app I:

  • generate an incrementing build number (integer) using a script as part of my build (uses Integer from SVN revision)
  • I generate an Android resource file with it defined as an integer: 399

  • I show that on the UI using the resource generated above

  • I reference the integer in the Manifest for the versionCode element

All that works fine, but when I attempt to upload to Market I get this error:

"The file is invalid: ERROR getting 'android:versionCode' attribute: attribute is not an integer value "

Question: For Market, does the versionCode have to be a "literal" integer directly inside the manifest, or is there some way to reference an externally generated integer and not be touching my manifest all the time (manually or automatically).

thanks (BTW: I have just realized that scheme might cause me issues with patch releases, so I will probably generate a bigger number or something, but would still like the answer to this question)

回答1:

In the end, this is what I have done:

I create a tag in my version control system with the numbe rof the release in this format

X.YY.ZZ where they are all integers, like 1.20.00, and reserve the last two digits for unplanned patch releases maybe made on a branch after a later release is made....

So if I release 1.20.00 then 1.23.00, I can still go back and do a patch release to 1.22.00 called 1.22.01.

I have a build step that gets the tag name and generates a string resource "1.22.00" for Android that I use in the UI. It also generates it as a number 12200 and I use this as the version number.

BUT, I don't try and include this version number directly in the manifest, I set it for the package by setting an ant property for version code that the ant build picks up if defined and uses to create the .apk.

So I get an always increasing integer for Android Market, but a user with an older version can install a patch release, but you cannot install a patch release if you have a newer official release.

i.e. if you have 1.20.00 you can install patch 1.20.01, but if a user has moved onto 1.23.00 they cannot install the 1.20.01 patch...

To set the property you can use; project.setProperty("version.name", versionName); project.setProperty("version.code", versionCode);

if those properties are set, then the Android build system (both old and newer) will pick-up the property value and use it, no need to do anything else special.

Just make sure you set it using one of the pre-build/pre-compile ant extension points.

In the new build system (Platform Tools >= 12) in custom_rules.xml I have added

<project name="custom" default="help">      
    <import file="build_info.xml" />
    <target name="-pre-build" depends="build-info" />
</project>

where build_info is my own ant project that calculates the version name/number from the Tag name (if you are building in a tag....).



回答2:

From everything I've read, Google Play currently requires android:versionCode to be a 32-bit integer, not a resource. Like you, I also initialize android:versionCode with an @integer resource.

To work around the market's limitation, our build script parses the @integer value and injects it directly into android:versionCode for release builds. This was a simple solution for us. There's far more involved ways to achieve this using ant.properties and the build.xml -pre-build option, however, that is beyond my needs.

For more on that, see:

Reference an integer resource for Android manifest versionCode https://groups.google.com/d/msg/android-developers/1dt0yxyNPsk/c9c6PlG84iwJ