简体   繁体   中英

Xamarin.Forms project fails to build in Azure Pipelines

I have a Xamarin.Forms project for Android that I can build/test/deploy/package without any errors locally and the App runs fine on my phone. Perfect!

Now I wanted to start using Azure DevOps Pipelines for CI, created a Xamarin Pipeline with the Wizard, started the Pipeline and it fails with a compilation error:

  /Users/vsts/agent/2.155.1/work/1/s/MyProject/MyService.cs(41,64): error CS1503: Argument 1: cannot convert from 'char' to 'string' [/Users/vsts/agent/2.155.1/work/1/s/MyProject/MyProject.Android.csproj]

Why does it build locally but not in the Pipeline? The referenced code in the error message is the following function:

var x = string.Join(',', nextStations);  // nextStations is IEnumerable<string>

I found out that the string.Join(char, IEnumerable<T>) function is only available in the .NET Core Framework but not in other versions like .NET for Xamarin.Android 7.1 - there is only a string.Join(string, IEnumerable<T>) function.

Of course, I could now change all of my code to not using those functions only available in .net Core. Actually I tried this, but then the Azure Build failed because one of my referenced NuGet Packages is also using such a .Net Core-only string function so that linking the application failed in the Pipeline. So this is not a solution.

So it seems that for whatever reason the Azure Pipeline tries to build my project against a different Framework. Why? Is there a way to configure this? Or is it a bug in Azure DevOps? Even when I take the msbuild command from the Pipelines log and execute it locally against my project, it runs without any errors. So why not in the pipeline?

msbuild /Users/vsts/agent/2.155.1/work/1/s/MyProject/MyProject.Android.csproj /t:PackageForAndroid /p:OutputPath=/Users/vsts/agent/2.155.1/work/1/b/Release /p:Configuration=Release

I guess this is the same problem as asked here: How to fix 'Error processing method: 'System.Void Prism.Navigation.PageNavigationService' error in Azure Pipelines But that question doesn't have a real answer (switching to Debug doesn't solve it here) and I found out more details, ie the different Framework versions.

So any idea how to make the Pipeline build the project?

Visual Studio 2019, Xamarin.Forms 4.2.0.778463, Android Target Framework v9.0

My azure-pipelines.yml file for reference:

trigger:
- master

pool:
  vmImage: 'macos-latest'

variables:
  buildConfiguration: 'Release'
  outputDirectory: '$(build.binariesDirectory)/$(buildConfiguration)'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '**/*.sln'

- task: XamarinAndroid@1
  inputs:
    projectFile: '**/*droid*.csproj'
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'

I'm not sure why it's building on your local machine, but if I had to guess it would be probably because you have some cached version of this code that was working fine at some point.

Nevertheless, if you look at the docs for the string.Join() method (for NETStandard), you will see that there's no overload that takes char as the first parameter. All of them take string . Therefore what you need to do is to use double quotes "<your_string_separator>" , instead of singles ones '<your_char_separator>' .

I believe that your faulty line should look like this

var x = string.Join(",", nextStations); // Notice that double quotes "", instead of single ones ``

EDIT : Apologies, I haven't read your entire question and I just realised that you're actually mentioning that you've checked string.Join() for the .NET Core. However, this is the confusing part and you might have not realised this, the compatibility part of the .NET Core with Mono (Xamarin framework) is done via the NETStandard (see docs )

Compatible: .NET Core is compatible with .NET Framework, Xamarin and Mono, via .NET Standard.

EDIT2 : More links

Mono Compatibility in a nutshell -> https://www.mono-project.com/docs/about-mono/compatibility/

In depth post about the difference between Mono and .NET Core -> https://stackoverflow.com/a/39740592/510627

I had the same issue a week ago and I found this . Cheking my pipeline logs I found the hosted MacOS was using mono version 5.12 so I added a step prior XamarinAndroid task where I set a newer version, this is my azure-pipelines.yaml file:

trigger:
- master

pool:
  vmImage: 'macos-latest'

variables:
  buildConfiguration: 'Release'
  outputDirectory: '$(build.binariesDirectory)/$(buildConfiguration)'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '**/*.sln'
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo 'Selecting Mono version...'
      sudo $AGENT_HOMEDIRECTORY/scripts/select-xamarin-sdk.sh 5_18_1
- task: XamarinAndroid@1
  inputs:
    projectFile: '**/*Droid*.csproj'
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'
- task: CopyFiles@2
  inputs:
    SourceFolder: '$(build.binariesDirectory)/$(buildConfiguration)'
    Contents: '*.apk'
    TargetFolder: '$(build.artifactstagingdirectory)'
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

You can find more information here and the versions of tooling here

Regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM