MAUI / Xamarin build failing with "Microsoft.Android.Sdk.Tooling.targets(20,5): error XA0031: Java SDK 11.0 or above is required when using .NET 6"
Asked Answered
N

3

0

We have an existing Azure DevOps build pipeline for a .NET Core 7 MAUI Android app that was working fine until today, 20th November 2023. We have noticed that the previous successful build was using MSBuild version 17.7.3. Today it's using 17.8.3 that I assume means it is on a server with the new VS image on it.

MSBuild version 17.8.3+195e7f5a3 for .NET
Build FAILED.
C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\targets\Microsoft.Android.Sdk.Tooling.targets(20,5): error XA0031: Java SDK 11.0 or above is required when using .NET 6 or higher. Download the latest JDK at: https://aka.ms/msopenjdk`

Looks like this is a breaking change for Xamarin / MAUI builds in Visual Studio 2022 17.8.3, if you are building for NET 7 without explicitly saying so in a global.json file (the dotnet build step is explicitly stating framework -f net7.0-android).

Necessity answered 20/11, 2023 at 21:36 Comment(4)
Having the same problem. Wonder if Microsoft will fix this... isn't version 11 old? (21 latest?)Discommon
Yeah think so. However it seems because we never specified the Java SDK version before , it was previously using version 8 that is the default for MSBuild 17.7.3 .Necessity
Coincidentally I had that happen too today and it doesn't actually have to do with Java, but rather I was trying to build a project that was targeting .NET 7 with the .NET 8 SDK. Double check that your build is using the correct .NET SDK and you project is targeting the right version and that those are aligned.Krawczyk
To add to my previous comment. I think what might have happened is that the hosted build agents now have .NET 8 installed by default and you're trying to build your .NET 7 project against it. Either add a step to use .NET 7 (or a global.json) or update your project to .NET 8 and I think it should be fixed without having to set the Java version explicitly.Krawczyk
N
9

Classic DevOps Build Step

Fixed it by adding a Java Tool Installer step before the dotnet build step, to use Java 11 explicitly. Only using Java 11 because this is the minimum requirement - you could try using latest Java if you like - v17 at this point in time.

enter image description here

Settings:

enter image description here

YAML Syntax

Just adding @Kuepper's answer for YAML build steps. Please give his answer the vote if it helped you.

  - task: JavaToolInstaller@0
    displayName: 'Use Java 17'
    inputs:
      versionSpec: 17
      jdkArchitectureOption: x64
      jdkSourceOption: PreInstalled
Necessity answered 20/11, 2023 at 21:37 Comment(1)
would you mind adding the yaml syntax into your answer? I posted it as a separate answer now to preserve the formatting.Mellicent
Z
1

Github Actions

I had the same problem on github actions.

Just adding this action before publishing fixed for me:

- name: Setup Java JDK
  uses: actions/[email protected]
  with:
    distribution: 'microsoft'
    java-version: '17'
Zambia answered 23/11, 2023 at 5:48 Comment(0)
M
1

That's the yaml for Azure DevOps (James Cormack's screenshot used version 11 instead of 17)

  - task: JavaToolInstaller@0
    displayName: 'Use Java 17'
    inputs:
      versionSpec: 17
      jdkArchitectureOption: x64
      jdkSourceOption: PreInstalled
Mellicent answered 28/11, 2023 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.