Azue Devops multitarget build order - xamarin

I have complex Xamarin solution with android, ios and nestandard core project (defined via Microsoft.NET.Sdk) and few other csprojs with some logic (some are defined via Microsoft.NET.Sdk as netstandard2.0. others defined via MSBuild.Sdk.Extras for multitargeting the netstandard2.0, xamarin.ios10, monoandroid10.0 and have some platform specific code)
Local build on my macos with latest libraries is ok (both netcore 3 and 5, latest xamarin. tried on Rider and VS for mac).
ADO build succeed on windows-2019 agent using msbuild (just regular CI with codechecks etc)
ADO build on macos-10.15 agent (different jobs for ios and android packaging, using XamarinIos/XamarinAndroid tasks) fails with kind of strange behaviour:
i see a lot of namespace errors in droid/ios projects for platform-specific libraries from my solution.
i've made some investigations and found out that msbuild runs platform specific builds of csprojs first, copies the outputs, then runs the netstandard build for the same csprojs and replace files in output with netstandard versions. which, obviously, didn't have any line of platform specific code.
so, the question is wtf? how am i suppose to fix this?
what i tried:
replace Xamarin tasks with msbuild calls
change Xamarin SDK version to 16_12_3 and 16_12_0 (current is 16_12_2)
change .net version to 5 (current is 3.1.x) both ways - via global.json and installing v5 using the UseDotNet#2 task
core project was converted to multitarget
remove everything from sln and add every project from the scratch
also i thought that msbuild, maybe, trying to make a nupkg from my libraries so i tried this in build props:
<PropertyGroup>
<IsPackable>false</IsPackable>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
also i have this
<PropertyGroup Label="Multiplatform" Condition=" '$(TargetsToBuild)' != 'All' ">
<TargetFrameworks Condition=" '$(TargetsToBuild)' == 'Android' ">monoandroid10.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(TargetsToBuild)' == 'iOS' ">xamarin.ios10</TargetFrameworks>
</PropertyGroup>
and multitarget projects has this
<PropertyGroup Label="Multiplatform">
<TargetFrameworks>netstandard2.0;xamarin.ios10;monoandroid10.0</TargetFrameworks>
</PropertyGroup>
of course i've tried to remove the tags above and build without them. no luck :(

So the answer is:
you should add csproj references to other projects in right order :/ order is matters for msbild

Related

How to fix Xamarin Android: Mono Shared MonoRuntime is not supported

When I deploy (or archive) my Xamarin.Android application and attempt to use the .APK file in a UI test I am met with this error. My configuration is exactly how it should be for a release build.
(PLEASE READ TO THE END)
System.Exception : Mono Shared Runtime is not supported. This can be resolved by changing the project configuration or using a Release build.
Stack Trace
ApkFile.EnsureNoSharedRuntime()
AndroidAppInitializer.PrepareApkFiles(IAndroidAppConfiguration appConfiguration, ArtifactFolder artifactFolder)
AndroidApp.ctor(IAndroidAppConfiguration appConfiguration, IExecutor executor)
AndroidAppConfigurator.StartApp(AppDataMode appDataMode)
AppInitializer.StartApp(Platform platform)
Tests.BeforeEachTest()
AndroidManifest.xml
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<AndroidEnableProfiledAot>true</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
<AndroidSupportedAbis />
</PropertyGroup>
This is ridiculous I have seen questions about this going up to FIVE years back and many unanswered. If someone knows a fix to this please let's help me and everyone else having this issue.
I literally created a brand new project with VS2022 and even on the release config. this error still shows on both emulated and physical devices.
Update: When the project is built and signed with Azure Devops the .APK file works on UITest. I am convinced this is a Visual Studio APK deployment problem.
We've had similar problems before, you can try the following methods:
1.in release mode,please make sure you uncheck use Fast Deployment(debug mode only);
2.make sure update your nuget to the latest one,the old Nuget had this issue;
3.If the problem persists,you can try the following workaround: set
<AndroidUseAssemblyStore>false</AndroidUseAssemblyStore>
in the configuration you are using with Xamarin.UITest.
4.try to delete obj and bin folders in your app and try again.

How do I determine the current building platform when building a multi-targeting csproj using msbuild?

I have a cross-platform shared class library project in my solution. In the other projects (such as xamarin.android, xamarin.ios, uwp, etc.), I add reference to the shared project.
Now when I run build on a platform specific project, the shared project will build for all platform but not only the platform which I am building.
I was wondering if there is any variable I can use for determine the current building platform to avoid a complete build for the shared platfom?
Like:
<TargetFrameworks Condition=" '$(<THE_WONDERFUL_PLATFORM_VARIABLE>)' == 'android' ">MonoAndroidxx</TargetFrameworks>
<TargetFrameworks Condition=" '$(<THE_WONDERFUL_PLATFORM_VARIABLE>)' == 'ios' ">Xamarin.iOSxx</TargetFrameworks>
<TargetFrameworks Condition=" '$(<THE_WONDERFUL_PLATFORM_VARIABLE>)' == 'uwp' ">uapxx.x.xxxx</TargetFrameworks>
Edit:
My project structure is:
My expected:
Actual:

upgraded from VS2010 to VS2013 now I cannot publish

I can publish without issues in Visual Studio 2010 but when I attempt to publish with Visual Studio 2013 I am getting this message:
The target "MSDeployPublish" does not exist in the project. \Source2\Web Projects\SubService\subservice\subservice.csproj 0 0 subservice
Can anyone tell me what I need to do to resolve this? It's happening with 2 different projects.
For what its worth;
I had the same issue. Fresly installed Windows 8.1 machine, only installed Visual Studio 2013 (+update1) + Azure SDK's . Create new Web Api Project boom build error -
The imported project "C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
note the v10.0. This is the default fallback when no MSBuild parameter "VisualStudioVersion" has been set. Prior to VS2012 the paths to the target files were hardcoded. Other solutions for this bugs say to remove the following from your csproj which is added for backward compatibility:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
But removing this fixes your builds but breaks the publish feature with the original posters error:
The target "MSDeployPublish" does not exist in the project
Note: I have all Azure related SDK parts installed from the web platform installer.
I have resorted to re-adding the above XML part to my csproj file but changing the 10.0 part to 12.0 (=vs2013)
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
This might break backward compatibility but finally made my Visual Studio 2013 compile the code & made the publish feature work again.
If you don't want to install visual studio on build server, you can use this NuGet package with portable version of the targets: https://www.nuget.org/packages/MSBuild.Microsoft.VisualStudio.Web.targets and modify your csproj file to include it like this:
<Import Project="..\packages\MSBuild.Microsoft.VisualStudio.Web.targets.12.0.1\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" />
Solution originally posted here: Where is MsDeployPublish located?
(in my case it was already MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0).
after 4 hours I found a post not directly related to this but I took a blind shot and it worked:
Install from "Web Platform Installer" I installed the "Windows Azure SDK for .Net (VS 2013)" and that worked !

MSBuild task to Build other solution projects fails in VS but works with MSBuild.exe command line

I have a custom MSBuild task which among other things adds embedded resources to other projects in the solution. After adding the resources I'd like to then build those projects, but found I can't get this working within Visual Studio.
To test, I stripped out the custom task entirely and redefined a simple AfterBuild target in the web project of a Silverlight solution. The target uses the MSBuild task to build the Silverlight application project in the solution, and looks like this:
<Target Name="AfterBuild">
<PropertyGroup>
<LinkedProject>..\SilverlightApplication1\SilverlightApplication1.csproj</LinkedProject>
</PropertyGroup>
<MSBuild Condition="'$(LinkedProject)' != '' "
Projects="$(LinkedProject)"
Targets="Build"
Properties="CustomFlag=true" >
</MSBuild>
</Target>
The odd thing is that this works perfectly when using MSBuild from the command line, yet does not work in Visual Studio when building the web project. I thought this might be some sort of Silverlight problem, and had the task build a .NET class library project instead, but the result was the same - it worked from the command line but not within VS. In VS there's no actual error - it's just that the Csc task does not compile the assembly and generates no output.
What do I need to do to get this working within Visual Studio?
Pass the 'UseHostCompilerIfAvailable=false' property to the MSBuild task.
It looks like Visual Studio breaks badly if csc is invoked from a MSBuild task as it reuses the initial project build settings for its in-process host compiler. In my case, I was building the same project twice - default build was using target framework v3.5, with a AfterBuild MSBuild task specifying v4.0. I ended up with the same issue - csc appeared to run but produced no output. I think what was happening was that with the UseHostCompilerIfAvailable property set to true, csc was calling the hosted compiler which reused my initial project settings, so even though the command line showed csc "building" my v4.0 assembly, the host compiler was simply overwriting the v3.5 one I had just built!
Change Visual Studio verbosity to detailed and check build log. I think that CoreBuild is not executed if your files have not changed, so you could try to use AfterCompile instead of AfterBuild.

MSBuild: How to build web deploy package for Web Deployment Projects (VS2010)?

I migrated a Web Site project (with Web Deployment project) from VS2008 to VS2010. Now I can make "Build Deployment Package" for Web Deployment Project in VS2010 and it works great! But I can't find a way how to do the same via MSBuild.
I answer on my one question. So after a lot of googling and 2 days of investigation it finally works.
Brief how to:
I created Configuration = QA (based on Debug configuration) for Solution via Configuration Manager.
Important: I removed 'Platform' parameter for QA Configuration. I couldn't build package until I did it. (My dev computer is Win7-x64, and I'm not not sure would be this step necessary for x86. But my build server Win2008-x86 forks fine with this modification.) This is QA Configuration section from my .wdproj
<PropertyGroup Condition=" '$(Configuration)' == 'QA' ">
<DebugSymbols>True</DebugSymbols>
<OutputPath>QA\</OutputPath>
<EnableUpdateable>true</EnableUpdateable>
<UseMerge>true</UseMerge>
<SingleAssemblyName>
</SingleAssemblyName>
<UseWebConfigReplacement>false</UseWebConfigReplacement>
<DeleteAppDataFolder>true</DeleteAppDataFolder>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<ExcludeApp_Data>true</ExcludeApp_Data>
</PropertyGroup>
I build and package .wbproj file with the following command:
msbuild WebSite.Deploy.wdproj /t:Build;Package /p:Configuration=QA
For information: If you need you can use standard Web Publishing parameters (e.g. ExcludeApp_Data, DeployIisAppPath etc.) in the QA configuration section.
Try
MSBuild YourProject.csproj /T:Package
That should generate a deployment package. This page, How to: Use MSBuild to Create a Web Package might give a bit more information, but not much.

Resources