Categories
.NET .NET 8 .NET MAUI Android Developer General Quick Fix Visual Studio Xamarin

.NET MAUI on .NET 8 Preview 6 – Android build issue

.NET 8 is the next LTS release and is currently in preview. .NET, .NET MAUI, and Visual Studio 2022 release a public preview every month for quite some time.

On Tue, Jul 11, another preview version of .NET 8 got released along with VS2022 17.7.0 Preview 3.0.

After installing the latest preview bits, the Android projects failed to build. Upon investigation, found that the Android SDK location got reset to the “Program Files” folder. Since I’ve been working with .NET MAUI from the early prerelease, the Android SDK was originally installed in the Android\Sdk sub-folder in the user’s local application data folder (then recommended path to install).

So in order to update the Android SDK location, head to the Options dialog within Visual Studio.

Tools (menu) → Options (Alt + T + O shortcut)

Then from the list of settings groups, scroll to the end to find Xamarin, and then expand the node to select the Android Settings. To easily locate it, type either Xamarin or Android in the top left Search Options text field.

On the right side, the Android-related options would be shown. Android SDK Location is one among them. Update the correct path here. Refer to the below (post-update) screenshot for details.

Android SDK Location – Visual Studio

And this month’s preview release of .NET MAUI on .NET 8 requires Android SDK Platform 34 (API Level 34), targeting Android 14, to be available to build the project.

To install this from within Visual Studio, head to Tools (menu) → Android (sub-menu) → Android SDK Manager … option.

After updating to the correct path and then installing this SDK Platform 34, able to build the Android projects within the IDE.

But the same build issue is still open while using the CLI dotnet build command. Updating the SDK location in the Visual Studio Option didn’t make any progress here. Since I’ve configured .NET MAUI on Linux, there ANDROID_HOME environment variable needs to be set to the path pointing to the Android SDK location for the relevant tools to be available while working on the CLI.

So following the same strategy did set the environment variable on Windows too. But in vain. The research went on and a few suggested on tech sites that this is now superseded by the ANDROID_SDK_ROOT environment variable. So tried that also. But no change in output.

So started going through the documentation, then found out that there’s actually an MSBuild property that controls this Android SDK location and can be overridden from the CLI, and is named AndroidSdkDirectory. Similarly, there’s an MSBuild property for Java SDK location as well named JavaSdkDirectory.

So the command to build the Android project on CLI until this issue gets addressed is as mentioned below.

dotnet build -f net8.0-android -p:AndroidSdkDirectory=%LocalAppData%\Android\Sdk

Note: %LocalAppData% is an OS environment variable that points to the user’s local application data directory. Have given the command matching with my current installation. Update the path where Android SDK is installed on your machine. If there’s any space in the path, then the path obviously needs to be within double quotes. Don’t use single quotes, it is causing trouble.

As usual, to run the project, include the -t:Run option in addition.

Pro Tip: This can be made as a batch file to automate the build process so that it doesn’t need to be typed again and again. Optionally, batch parameters can be used to pass the project file name as input.

Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.

By Vijay Anand E G

A software professional with over a decade long industry experience in developing products that spans across desktop, mobile, and web.

One reply on “.NET MAUI on .NET 8 Preview 6 – Android build issue”

Comments are closed.