Bug 39264 - Adding Google Play Services package to Android Project removes AndroidUseLatestPlatformSdk msbuild key in csproj
Summary: Adding Google Play Services package to Android Project removes AndroidUseLate...
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Project Model ()
Version: Trunk
Hardware: PC Mac OS
: --- normal
Target Milestone: (C7)
Assignee: Matt Ward
Depends on:
Reported: 2016-03-01 19:19 UTC by Manish Sinha
Modified: 2016-03-08 15:01 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Manish Sinha 2016-03-01 19:19:30 UTC
This bug has been hard to reproduce and happens once in a while

Steps to reproduce
1. Create an Android Empty Project with Insights and Test Cloud
2. Add two GPS packages
3. Check csproj to find that <AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk> has been removed

XS Info: https://gist.github.com/XamarinQA/683f83ec5f6b6e71d6d2
Ide log: http://storage.bos.internalx.com/QA-xs_mac_uitest_long-cycle7/c3/c36804b6937a43d77ab05923f06a2de658c5604c/XQA-XS-ElCapitan/TestCreateBuildBlankAndroidApp/TestCreateBuildBlankAndroidApp.Ide.log
Comment 3 Greg Munn 2016-03-01 19:55:11 UTC
Adding GPS packages simply calls into the packagemanagement (nuget) addin and queues the nugets to be installed. I don't even call Save on the project. 

It's sounding like there is some race condition or something going on.
Comment 4 Matt Ward 2016-03-02 12:20:36 UTC
Running the QA UI test I have managed to repro the problem twice in 7 attempts.
Comment 5 Greg Munn 2016-03-04 15:04:33 UTC
@matt, I wonder if  bug 39075, comment 5 could be related to this issue?

I'm guessing that adding 1 GPS package never results in an issue and that 2 or more do - I also wonder if adding more than 2 at a time increases the chances of the bug occurring?
Comment 6 Matt Ward 2016-03-04 15:14:45 UTC
@greg - Yeah it may well be related. Note that the NuGet addin will call save a few times installing a single NuGet package.
Comment 7 Matt Ward 2016-03-04 17:05:27 UTC
Mmm, still seeing the problem with the latest code from master which includes the commit https://github.com/xamarin/md-addins/commit/97d7f30e74b8aa1ff7c9f58d789d99d6d90f095b which is the fix mentioned in bug 39075, comment 5
Comment 8 Greg Munn 2016-03-04 17:12:36 UTC
I didn't think that commit would fix this issue, and it doesn't completely fix it's own issue - see bug 39075, comment 8
Comment 9 Matt Ward 2016-03-07 12:35:07 UTC
I added some logging around saving and where the NuGet addin making changes to the project. The project seems to lose various project properties on loading the solution back into Xamarin Studio and not when adding the NuGet packages. The QA test also closes and re-opens the solution at one point during the test. On opening the solution the project seems to be saved and sometimes loses project information in the .csproj.

I will put some more logging into the Android project to see if I can narrow down where the project is being saved and go from there.
Comment 10 Matt Ward 2016-03-07 14:41:27 UTC
Getting a bit closer. What seems to be happening is that the manifest is being added to the project again even though it already exists. The MonoDroidProjectFlavor calls SaveAsync in GetManifestFileName


AddExistingManifest only adds the file to the project if it does not find it in the project items. The solution window shows two AndroidManifest.xml files after the problem occurs. So I am guessing that this code is being run before the project is fully loaded very occasionally. Maybe this explains why things are being removed - we are saving the project file again without it having read all the information from the .csproj file.

Going to dig a bit deeper...
Comment 11 Matt Ward 2016-03-07 17:09:29 UTC
OK it is a race condition. What is happening is we a race between two threads - one initializing the project and loading it and another (the UI thread) responding the Android devices updated event:

1) MonoDroidProjectFlavor.Initialize is called. This creates an event handler for OnDevicesUpdated.
2) OnDevicesUpdated is called. This can happen before OnBeginLoad can be called, which sets the Loading flag to true. 
3) The AndroidManifest.xml is not found in the project items. So another one is added and the project is saved. MonoDroidProjectFlavor now bypasses the check at the start of OnDevicesUpdated since Project.Loading is false. At this point in time the project properties, such as AndroidUseLatestPlatformSdk, have not been loaded. So saving the project at this point means we lose this information. The elements are removed since they are using the default values for the properties.

Probably an easier to reproduce this is to put a Task.Delay into the MSBuildProjectService just before BeginLoad.


Possible ways to fix this:

1) Do not rely on Project.Loading. Instead use another flag that is set when OnEndLoad is called. Use this flag in the OnDevicesUpdated method.
2) Move the code that sets up the OnDevicesUpdated event handler into OnEndLoad. At this point there is no problem with the event being fired. Not sure if this code can or should be moved from Initialize to OnEndLoad.

Note that 1) and 2) both may not work for new projects created from templates - need to check both new solutions and adding a project to an existing solution. Adding a project to an existing solution will not load the project. InitializeFromTemplate may need to be changed to handle this.

Output from extra logging showing BeginLoad being called after OnDevicesUpdated and the project file content:

INFO [2016-03-07 16:49:53Z]: BeginLoad - start - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp.UITests/BlankAndroidApp.UITests.csproj
INFO [2016-03-07 16:49:53Z]: BeginLoad - end - 1 - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp.UITests/BlankAndroidApp.UITests.csproj
INFO [2016-03-07 16:49:53Z]: Initialize - start
INFO [2016-03-07 16:49:53Z]: ANDROID: Tracking android devices started
INFO [2016-03-07 16:49:53Z]: Initialize end
INFO [2016-03-07 16:49:53Z]: ANDROID: Got new device list from adb with 0 devices
INFO [2016-03-07 16:49:53Z]: OnDevicesUpdated
INFO [2016-03-07 16:49:53Z]: OnDevicesUpdated - loading = False
INFO [2016-03-07 16:49:53Z]: BeginLoad - start - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp/BlankAndroidApp.csproj
INFO [2016-03-07 16:49:53Z]: BeginLoad - end - 1 - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp/BlankAndroidApp.csproj
INFO [2016-03-07 16:49:53Z]: AddExistingManifest - ProjectIsLoading
INFO [2016-03-07 16:49:53Z]: Saving project after adding manifest - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp/Properties/AndroidManifest.xml
INFO [2016-03-07 16:49:53Z]: GetManifestFileName - stack trace -   at System.Environment.get_StackTrace () [0x00000] in /private/tmp/source-mono-4.4.0/bockbuild-mono-4.4.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/corlib/System/Environment.cs:321 
  at MonoDevelop.MonoDroid.MonoDroidProjectFlavor.GetManifestFileName () [0x00033] in projects/md-addins/MonoDevelop.MonoDroid/MonoDevelop.MonoDroid/MonoDroidProject.cs:1318 
  at MonoDevelop.MonoDroid.MonoDroidProjectFlavor.GetAndroidManifestCache () [0x00000] in projects/md-addins/MonoDevelop.MonoDroid/MonoDevelop.MonoDroid/MonoDroidProject.cs:1287 
  at MonoDevelop.MonoDroid.MonoDroidProjectFlavor.GetTargetDeviceMinSdk () [0x00002] in projects/md-addins/MonoDevelop.MonoDroid/MonoDevelop.MonoDroid/MonoDroidProject.cs:1521 
  at MonoDevelop.MonoDroid.MonoDroidProjectFlavor.OnDevicesUpdated (System.Object sender, System.EventArgs e) [0x00039] in projects/md-addins/MonoDevelop.MonoDroid/MonoDevelop.MonoDroid/MonoDroidProject.cs:1510 
  at MonoDevelop.MonoDroid.MonoDroidProjectFlavor+<Initialize>c__AnonStorey8.<>m__0 () [0x00000] in projects/md-addins/MonoDevelop.MonoDroid/MonoDevelop.MonoDroid/MonoDroidProject.cs:334 
  at MonoDevelop.Core.Runtime+<RunInMainThread>c__AnonStorey0.<>m__0 (System.Object ) [0x00000] in projects/monodevelop/main/src/core/MonoDevelop.Core/MonoDevelop.Core/Runtime.cs:331 
  at MonoDevelop.Ide.DispatchService+GtkSynchronizationContext+<Post>c__AnonStorey0.<>m__0 (System.Object , System.EventArgs ) [0x00000] in projects/monodevelop/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide/DispatchService.cs:52 
  at Gtk.Application+InvokeCB.Invoke () [0x00000] in <filename unknown>:0 
  at GLib.Timeout+TimeoutProxy.Handler () [0x00000] in <filename unknown>:0 
  at Gtk.Application.gtk_main () [0x00000] in <filename unknown>:0 
  at Gtk.Application.Run () [0x00000] in <filename unknown>:0 
  at MonoDevelop.Ide.IdeApp.Run () [0x00000] in projects/monodevelop/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide/Ide.cs:376 
  at MonoDevelop.Ide.IdeStartup.Run (MonoDevelop.Ide.MonoDevelopOptions options) [0x00742] in projects/monodevelop/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide/IdeStartup.cs:281 
  at MonoDevelop.Ide.IdeStartup.Main (System.String[] args, MonoDevelop.Ide.Extensions.IdeCustomizer customizer) [0x000a3] in projects/monodevelop/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide/IdeStartup.cs:592 
  at MonoDevelop.Startup.MonoDevelopMain.Main (System.String[] args) [0x00000] in projects/monodevelop/main/src/core/MonoDevelop.Startup/MonoDevelop.Startup/MonoDevelopMain.cs:39 
INFO [2016-03-07 16:49:53Z]: GetManifestFileName - androidUseLatestPlatformSdk.hasValue - False
INFO [2016-03-07 16:49:53Z]: ---------Project file saved-----------------
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <Reference Include="System" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Core" />
    <Reference Include="Mono.Android" />
    <Reference Include="Xamarin.Insights">
    <Compile Include="MainActivity.cs" />
    <Compile Include="Resources\Resource.designer.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <None Include="Resources\AboutResources.txt" />
    <None Include="Properties\AndroidManifest.xml" />
    <None Include="Assets\AboutAssets.txt" />
    <None Include="packages.config" />
    <None Include="Properties\AndroidManifest.xml" />
    <Folder Include="Resources\layout\" />
    <Folder Include="Resources\mipmap-hdpi\" />
    <Folder Include="Resources\mipmap-mdpi\" />
    <Folder Include="Resources\mipmap-xhdpi\" />
    <Folder Include="Resources\mipmap-xxhdpi\" />
    <Folder Include="Resources\mipmap-xxxhdpi\" />
    <Folder Include="Resources\drawable\" />
    <AndroidResource Include="Resources\values\Strings.xml" />
  <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
  <Import Project="..\packages\Xamarin.Insights.1.11.3\build\MonoAndroid10\Xamarin.Insights.targets" Condition="Exists('..\packages\Xamarin.Insights.1.11.3\build\MonoAndroid10\Xamarin.Insights.targets')" />
INFO [2016-03-07 16:49:53Z]: AndroidUseLatestPlatformSdk - setting value - 
INFO [2016-03-07 16:49:53Z]: EndLoad - start - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp.UITests/BlankAndroidApp.UITests.csproj
INFO [2016-03-07 16:49:53Z]: EndLoad - end - 0 - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp.UITests/BlankAndroidApp.UITests.csproj
INFO [2016-03-07 16:49:53Z]: EndLoad - start - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp/BlankAndroidApp.csproj
INFO [2016-03-07 16:49:53Z]: OnEndLoad - start - 
INFO [2016-03-07 16:49:53Z]: OnDevicesUpdated
INFO [2016-03-07 16:49:53Z]: OnDevicesUpdated - loading = False
INFO [2016-03-07 16:49:53Z]: OnEndLoad - end
INFO [2016-03-07 16:49:53Z]: EndLoad - end - 0 - projects/QualityAssurance/Automation/XQA/XQA.XS.Tests/TestResults/TestCreateBuildBlankAndroidApp/Temp/BlankAndroidApp/BlankAndroidApp/BlankAndroidApp.csproj
Comment 12 Matt Ward 2016-03-08 11:57:57 UTC
There is a pull request with an implementation of the simplest fix I could do for the bug.


This uses a loaded flag which is set at the end of OnEndLoad and OnInitializeFromTemplate which stops OnDevicesUpdated from doing any processing until the project is fully loaded.

I tested this fix and could not reproduce the bug on opening and closing a solution 30 times. Normally it happens without 10 attempts.
Comment 13 xamarin-release-manager 2016-03-08 14:58:40 UTC
Fixed in version (master)

Author: Matt Ward
Commit: a707e02bb0d49583eba50448ea720579d94289d2 (xamarin/md-addins)
Included in Commit: df48c090e20d9093a2696988cf4721bd67246fe3 (mono/monodevelop)
Comment 14 xamarin-release-manager 2016-03-08 15:01:26 UTC
Fixed in version (cycle7)

Author: Matt Ward
Commit: 00efbf6615752b68f014f3d99a3b6ad5127416b5 (xamarin/md-addins)
Included in Commit: 0e06e3e46dd3f9eb13624b11ec65434cdbd420d1 (mono/monodevelop)