Xamarin Android 12 project build in App Center

December 30th 2022 App Center Xamarin Android

When I tried to build my Xamarin.Forms app for Android in App Center with my existing build configuration, it failed with the following error:

error XA0000: Could not determine API level for $(TargetFrameworkVersion) of 'v12.0'.

This was odd, as the project could be built on my local machine without any problems. My first impulse was to check the build configuration, and I quickly found the likely culprit:

Android SDK setting in App Center build configuration

The Xamarin.Android SDK version was set to version 11.2. Since I wanted to build the project for Android 12 (API-31), the minimum required version was 12.0, so I changed the SDK version to 12.2, the latest production-ready v12 version, according to the docs.

Unfortunately, the build still failed with the same error.

After some research, I found a great resource that shed light on why the build was still failing. The build for API-31 required JDK 11, and the App Center build was still using JDK 8, as shown in the build log just above the error message:

_ResolveAndroidTooling:
  Found Java SDK version 1.8.0.
  Found Java SDK version 1.8.0.
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Legacy.targets(189,5): error XA0000: Could not determine API level for $(TargetFrameworkVersion) of 'v12.0'. [/Users/runner/work/1/s/***.Android/***.Android.csproj]

This can be changed by setting the JAVA_HOME environment variable correctly. There is even some documentation on this if you know where to look. At the top of the build log, the exact image that was used for the build is listed:

##[group]Runner Image
Image: macos-11
Version: 20221204.1
Included Software: https://github.com/actions/runner-images/blob/macOS-11/20221204.1/images/macos/macos-11-Readme.md
Image Release: https://github.com/actions/runner-images/releases/tag/macOS-11%2F20221204.1
##[endgroup]

It also includes a link to a page with all the pre-installed software. The linked page also lists all available versions of Java, including the environment variables with the path to each one:

Java version installed in App Center build runner

To use Java 11, the JAVA_HOME environment variable must be set to the value of the JAVA_HOME_11_X64 environment variable in the build configuration:

JAVA_HOME environment variable in App Center build configuration

After this change was made, the runner used Java 11 in the next build, which allowed it to determine the correct API level and successfully build the application:

_ResolveAndroidTooling:
  Found Java SDK version 11.0.17.
  Found Java SDK version 11.0.17.
_CheckInstantRunCondition:
  Dex Fast Deployment Enabled: False
_ResolveMonoAndroidSdks:
  MonoAndroid Tools: /Library/Frameworks/Xamarin.Android.framework/Libraries/xbuild/Xamarin/Android/
  Android Platform API level: 31
  TargetFrameworkVersion: v12.0

I really like App Center for building mobile apps. But it turns out that build errors do not always point to the root cause. Fortunately, I was able to fix the build pretty quickly, mostly thanks to the great repository of common App Center issues and solutions for them. I know I have bookmarked it for future reference. If you are an App Center user, you should do the same.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License