Bug 51620

Summary: Build error when using a versionCode > 65535 with "Generate one package per selected ABI" feature enabled
Product: Android Reporter: Keith Rome <krome>
Component: MSBuildAssignee: dean.ellis
Severity: normal CC: andrew.e.page, jonp, mono-bugs+monodroid, toopge
Priority: High    
Version: 7.0 (C8)   
Target Milestone: 15.4   
Hardware: Macintosh   
OS: Mac OS   
Tags: bb Is this bug a regression?: ---
Last known good build:
Attachments: Sample Xamarin.Android project with android:versionCode set to 65536.

Description Keith Rome 2017-01-18 18:27:47 UTC
When the "Generate one package per selected ABI" feature is enabled, high numerical values for versionCode ("Version number" in the build options page) cause the build process to fail.

The error produced during build is:

> "Error executing task Aapt: VersionCode is outside 0, 65535 interval" in Xamarin.Android.Common.targets

Disabling the APK per ABI feature works around the problem, but obviously you end up with a single bloated APK that way.

NOTE: There is guidance for using scripts to control the multi-APK build process (here: https://developer.xamarin.com/guides/android/advanced_topics/build-abi-specific-apks/#Creating_the_Version_Code_for_the_APK), but the options UI does not hint at this being a requirement or suggested approach, and the article does not provide a full solution - it leaves the actual script writing/debugging up to the reader.
Comment 1 Tom Opgenorth 2017-06-23 22:06:57 UTC
Created attachment 23094 [details]
Sample Xamarin.Android project with android:versionCode set to 65536.
Comment 2 Tom Opgenorth 2017-06-23 22:16:07 UTC
Steps to duplicate:

1. Open the attached solution.
2. Check the Projection options of the Xamarin.Android project - note that the android:versionCode is set to 65536 and that the APK per ABI is checked. Also note that multiple ABIs are selected.
3. Try to build the Application, the build will fail with the specified error.
4. Go back to the Project Options for the Xamarin.Android project, under Build > Android Build > General, unselect the "On ABI per APK". 
5. Try and build the Application, the build will fail with the same error. 

Expected Result:

Multiple APIs would be created with the specified version code.
Comment 3 dean.ellis 2017-06-26 09:32:03 UTC
Looking at the docs 65535 is no longer the max value for the version code. 
It is 2100000000 so our code needs to be updated to allow this. 

[2] https://developer.android.com/studio/publish/versioning.html
Comment 4 Cody Beyer (MSFT) 2017-07-12 22:42:18 UTC
*** Bug 51145 has been marked as a duplicate of this bug. ***
Comment 5 Cody Beyer (MSFT) 2017-07-12 22:43:23 UTC
*** Bug 51618 has been marked as a duplicate of this bug. ***
Comment 6 Jonathan Pryor 2017-07-14 20:17:29 UTC
Possibly related: https://github.com/xamarin/xamarin-android/pull/692
Comment 7 dean.ellis 2017-08-01 12:16:28 UTC
We have implemented a new system for allowing the user to define a version Code format. Details of which can be found at [1].

The old system is still in place (and will remain so) because people might well be using it. That said people should migrate over to using the new system once it is released. 

The new system introduces a two new properties.


which the user can define in the .csproj. 

AndroidVersionCodePattern - is a string which contains format characters which define the versionCode pattern. For example `{abi}{versionCode:D4}`.
This will produce a version in which the first character is the abi mapped to an integer (see [2]) and the last part is a left zero padded value of the versionCode from the AndroidManifest.xml . 
So if we are targeting x86 and our versionCode in the manifest is 22 we get


as the versionCode in the final APK.

AndroidVersionCodeProperties - allows you do define you own values to use in the AndroidVersionCodePattern. e.g screen=13;someprop=$(Foo). Note you can use MSbuild properties. You can then have a AndroidVersionCodePattern of `{abi}{screen}{someprop}`.

This opens some interesting possibilities with pulling the version code from the assemblyInfo and using that. See the commit comment in [1] for more details. Or even better the docs [3] :) 

For our testers - as stated our old system must remain in place as people will be using it. So technically we have not fixed this issue, but there is a new system to help developers work with the version Code. Hence I am not marking this a FIXED but as ANSWERED.

[1] https://github.com/xamarin/xamarin-android/commit/0a2f008c8ac6e031df87209b76d4206bf63508eb
[2] https://developer.xamarin.com/guides/android/advanced_topics/build-abi-specific-apks/
[3] https://github.com/xamarin/xamarin-android/blob/master/Documentation/build_process.md#AndroidVersionCodePattern