Fixing .NET MAUI Android: Why VS Code + Android Studio Setup Matters


π₯ Running a .NET MAUI Android app from VS Code mostly worksβuntil it doesn't. If you've ever faced rage-inducing errors like:
π¨
Android SDK directory could not be found...
π₯Building with JDK version 23.0.1 is not supported...
π΅No available device.
...then you know the pain. This guide is your escape hatchβwe'll walk through exactly what to install and why each step matters.
π STEP 1: Install Android Studio (Your New Best Friend)
Why this matters π‘:
Android Studio isn't just an IDEβit's your golden ticket to:
π¦ Android SDK (platforms, build tools, command-line tools)
π± Android Virtual Device (a lifelike emulator)
π Get it here: Download Android Studio
π What to install:
Component | What You Need |
SDK Platforms | Android 14 (API 34) + Android 15 (API 35) |
SDK Tools | Build-Tools, Platform-Tools, Emulator |
π‘ Pro Tip: This setup gives VS Code the Android muscle it lacks out of the box.Step 2: Install Java 21 (Not Java 23!)
β STEP 2: Install Java 21 (Not Java 23!)
π¨ Critical Warning: .NET MAUI on Windows only plays nice with:
Java 17 β
Java 21 β
It will rebel against newer JDKs! β
One-Command Magic β¨:
winget install --id EclipseAdoptium.Temurin.21.JDK -e
What happens if you ignore this?
You'll face the dreaded error:
π₯
Building with JDK version '23.0.1' is not supported. Please install JDK version '21.0'
π€ Why Does a C# Project Need Java? A Quick History Lesson
You might wonder: "Why does .NET MAUI, a C# framework, need Java for Android development?" Here's the fascinating backstory:
1. Android's Java Roots (2008)
When Android was created, Java was chosen as its primary language because:
β Ubiquity: Java was already widely adopted
ποΈ Portability: "Write once, run anywhere" philosophy
π Google's Decision: Oracle's Java was the industry standard
2. Xamarin Enters (2011)
Microsoft's Xamarin (which evolved into MAUI) needed to:
π€ Interoperate with Android's Java-based toolchain
βοΈ Use Android SDKs (written in Java)
π οΈ Compile to JVM bytecode for Android compatibility
3. The Build Process Quirk
.NET MAUI apps for Android still require:
π§© Java tooling for:
javac
(Java compiler)dx
/D8
(Dalvik bytecode conversion)aapt
(Android asset packaging)
π Intermediate translation from .NET IL β Java bytecode β Android ART
4. Modern Dependencies
Even in 2024, you need Java because:
π¦ Android SDK tools are Java-based
π .NET's Android bindings rely on Java Native Interface (JNI)
β³ Backward compatibility with millions of Java-based Android libraries
π‘ Key Takeaway
While you write C#, the Android build chain still speaks Java under the hood. This hybrid approach gives you:
C#'s productivity β¨
Access to Java's Android ecosystem π±
Performance close to native π₯
Fun Fact: This is why Flutter (Dart) and React Native (JavaScript) also need Java for Android builds!
βοΈ STEP 3: Configure Environment Variables (The Secret Sauce)
For Java:
[Environment]::SetEnvironmentVariable("JAVA_HOME",
"C:\Program Files\Eclipse Adoptium\jdk-21.0.7.6-hotspot",
"User")
[Environment]::SetEnvironmentVariable("Path",
"$([Environment]::GetEnvironmentVariable('Path','User'));%JAVA_HOME%\bin",
"User")
For Android:
[Environment]::SetEnvironmentVariable("ANDROID_SDK_ROOT",
"C:\Users\kelvi\AppData\Local\Android\sdk",
"User")
[Environment]::SetEnvironmentVariable("ANDROID_HOME",
"C:\Users\kelvi\AppData\Local\Android\sdk",
"User")
[Environment]::SetEnvironmentVariable("Path",
"$([Environment]::GetEnvironmentVariable('Path','User'));%ANDROID_SDK_ROOT%\platform-tools;%ANDROID_SDK_ROOT%\emulator",
"User")
Why this matters π:
These variables are like GPS coordinates for your tools. Without them:
You'll waste time typing full paths π
Commands become ridiculously long π
Your sanity will slowly evaporate π΅βπ«
π± STEP 4: Create Your Android Virtual Device (The Magic Phone)
Follow these steps π:
Open Android Studio
Navigate to:
Tools β Device Manager
(or click the π± phone icon)Click
Create Device
Choose: Pixel 5 (or your preferred model)
Select: Android 34/35 system image (download if needed)
Launch: Hit the βΆοΈ play button!
Why this is crucial β:
Your dotnet build -t:Run
command needs a device target. No emulator?
You'll get:
β
error XA0010: No available device.
π STEP 5: Build & Deploy from VS Code (The Grand Finale)
Run this command in your project folder:
powershell
Copy
Download
dotnet build -t:Run -f net9.0-android `
-p:AndroidSdkDirectory="C:\Users\kelvi\AppData\Local\Android\sdk" `
-p:JavaSdkDirectory="C:\Program Files\Eclipse Adoptium\jdk-21.0.7.6-hotspot"
What happens π:
βοΈ Builds your MAUI app for Android
βοΈ Uses your configured SDKs/JDK
βοΈ Auto-detects your running emulator
βοΈ Installs and launches your app
π Why This Setup Rocks
Component | Benefit |
Android Studio | Installs SDK + emulator in one go |
Java 21 | Guarantees MAUI compatibility |
Env Variables | Eliminates repetitive path typing |
Emulator | Provides instant testing environment |
VS Code Build | One-command deployment |
π Bonus Pro Tip
Automate this with .vscode/tasks.json
for one-click builds (Ctrl+Shift+B)!
Want the config? Let us know in comments! π¬
π TL;DR Cheat Sheet
π₯ Install Android Studio (gets SDK + emulator)
β Get Java 21 via
winget
βοΈ Set environment variables
π± Create Android emulator
ποΈ Run
dotnet build -t:Run
You're now MAUI-ready in VS Code! π
Faster, cleaner, and no more SDK errors!
Subscribe to my newsletter
Read articles from Kelvin R. Tobias directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
