Bug 16626 - MSBuild BeforeBuild target called twice when running app
Summary: MSBuild BeforeBuild target called twice when running app
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 4.10.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2013-12-05 16:44 UTC by Grant Macklem
Modified: 2017-07-06 07:07 UTC (History)
2 users (show)

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

Basic New Android Project with BeforeBuild target added to csproj (23.65 KB, application/x-zip-compressed)
2013-12-05 16:44 UTC, Grant Macklem

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 Grant Macklem 2013-12-05 16:44:27 UTC
Created attachment 5612 [details]
Basic New Android Project with BeforeBuild target added to csproj

Due to bug 13865, the way to single source a pre-build command for Visual and Xamarin Studio is to use the MSBuild BeforeBuild target.  However, when I run the app, this target runs twice.  I have attached a sample project demonstrating this.  It's just a new Android project from Visual Studio with the following lines added at the bottom of the .csproj file:
  <Target Name="BeforeBuild">
    <Message Text="### BeforeBuild target called ###" />
After clicking the start button (or F5) to start debugging, look in the build output.  You will see the text
### BeforeBuild target called ###
appear twice (Actually 3 times, but it looks like the last two are the same -- the SignAndroidPackage output is just appended a second time as the build start time is the same).

The BeforeBuild target should only run once, as the app should only be built once.  In the Xamarin.Android.Common.targets file, the SignAndroidPackage target depends on Build.  I don't know what the fix is, but since the app was just built, there seems to be a bug in the dependency scheduler that actually does the Build twice and thus causes the BeforeBuild target to run twice.

I'm using the BeforeBuild target to increment the version in AndroidManifest.xml, so this is particularly problematic because the version increments twice.  I think the workaround I'm going to use is the MSBuild PreBuildEvent and only build our versioned release from Visual Studio.
Comment 1 Grant Macklem 2013-12-05 17:42:00 UTC
Actually, upon further investigation, the PreBuildEvent is also called twice.  I don't currently know of a workaround for this issue.
Comment 2 Prashant [MSFT] 2017-07-06 07:07:48 UTC
Unfortunately, we’re unable to reproduce this report. If this issue is still occurring for you, please reopen this issue and attach a reproduction to the bug by starting with a clean Xamarin.Android project adding just the code necessary to demonstrate the issue.