dotnet/maui · error · Exception

Environment variable 'ANDROID_SDK_ROOT' or 'ANDROID_HOME' mu

Error message

Environment variable 'ANDROID_SDK_ROOT' or 'ANDROID_HOME' must be set to the Android SDK root.

What it means

Thrown by GetAndroidSDKPath when both the ANDROID_SDK_ROOT and ANDROID_HOME environment variables (and the `android` argument) are null or empty. The Android SDK path is required for any adb/emulator/sdkmanager invocation in the build, so the script refuses to continue.

Source

Thrown at eng/cake/helpers.cake:79

    !String.IsNullOrWhiteSpace(GetAgentName());

bool IsHostedAgent() =>
    GetAgentName().StartsWith("Azure Pipelines") || GetAgentName().StartsWith("Hosted Agent");

T GetBuildVariable<T>(string key, T defaultValue)
{
    // on MAC all environment variables are upper case regardless of how you specify them in devops
    // And then Environment Variable check is case sensitive
    T upperCaseReturnValue = Argument(key.ToUpper(), EnvironmentVariable(key.ToUpper(), defaultValue));
    return Argument(key, EnvironmentVariable(key, upperCaseReturnValue));
}

string GetAndroidSDKPath()
{
    var ANDROID_SDK_ROOT = Argument("android", EnvironmentVariable("ANDROID_SDK_ROOT") ?? EnvironmentVariable("ANDROID_HOME"));

    if (string.IsNullOrEmpty(ANDROID_SDK_ROOT)) {
        throw new Exception("Environment variable 'ANDROID_SDK_ROOT' or 'ANDROID_HOME' must be set to the Android SDK root.");    
    }

    return ANDROID_SDK_ROOT;
}

public void PrintEnvironmentVariables()
{
    var envVars = EnvironmentVariables();

    string path;
    if (envVars.TryGetValue("PATH", out path))
    {
        Information("Path: {0}", path);
    }

    foreach(var envVar in envVars)
    {
        Information(

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Set ANDROID_SDK_ROOT to your SDK root (e.g. ~/Library/Android/sdk on macOS, %LOCALAPPDATA%\Android\Sdk on Windows) in your shell profile and CI variables.
  2. Alternatively pass it at invocation time: `--android=/path/to/sdk`.
  3. Confirm with `echo $ANDROID_SDK_ROOT` (or the platform equivalent) that the value is non-empty in the same shell that runs cake.
  4. If on macOS, remember env var lookup here is case-sensitive — export the exact upper-case name.

Example fix

# before
# (no env var set)
# after
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
export ANDROID_HOME="$ANDROID_SDK_ROOT"
Defensive patterns

Strategy: validation

Validate before calling

var ANDROID_SDK_ROOT = Argument("android",
    EnvironmentVariable("ANDROID_SDK_ROOT") ??
    EnvironmentVariable("ANDROID_HOME") ??
    (IsRunningOnWindows() ? @"%LOCALAPPDATA%\Android\Sdk" : "~/Library/Android/sdk"));
if (string.IsNullOrEmpty(ANDROID_SDK_ROOT) || !DirectoryExists(ANDROID_SDK_ROOT))
    throw new Exception($"Android SDK not found at '{ANDROID_SDK_ROOT}'. Set ANDROID_SDK_ROOT.");

Prevention

When it happens

Trigger: Argument("android", EnvironmentVariable("ANDROID_SDK_ROOT") ?? EnvironmentVariable("ANDROID_HOME")) resolves to an empty/null string. Note the lookup is case-sensitive on non-Mac and relies on the exact upper-case variable names.

Common situations: A new dev machine or CI agent where the Android SDK was installed by Android Studio but no env var was set; a shell rc file that exports the var only for interactive sessions; a typo in the variable name; the var set in a different case on macOS where env lookup is case-sensitive here.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/4f5dd7e4fabe408c. Report an issue: GitHub.