Android emulator cannot trust your local ASP.NET Core HTTPS API
Trust anchor for certification path not found.Short answer
The local ASP.NET Core HTTPS certificate is self-signed, and Android does not trust it. In a debug build, give HttpClient a handler that accepts the localhost certificate, call the API through 10.0.2.2, and trust the dev certificate on your machine. Or use plain HTTP with cleartext allowed for the debug build.
Fix it fast
1Find out which cause you have
Check which address and scheme the app calls. An https URL to 10.0.2.2 or localhost means the self-signed certificate is the problem; anything else is a different error.
dotnet dev-certs https --check --trust grep -rn "10.0.2.2\|localhost" --include=*.cs .2Match what you see, then jump to its fix
- 1
CertPathValidatorException with the trust anchor message when the app calls https://10.0.2.2:<port>The HttpClient does not accept the self-signed development certificateFix - 2
The request goes to https://localhost:<port> from the Android emulatorThe app calls localhost, which is the emulator itselfFix - 3
The dev-certs check reports that no trusted HTTPS development certificate was foundThe development certificate is not trusted on your computerFix - 4
Switching the debug URL to http://10.0.2.2:<port> fails because Android blocks cleartext trafficYou only need HTTP for local developmentFix
- 1
What does the error look like?
Trust anchor for certification path not found.java.security.cert.CertPathValidatorException
Cause 1 of 4
The HttpClient does not accept the self-signed development certificate
1Check
Look at how the HttpClient is created. If it is built with the default handler, Android validates the server certificate against its own store, which does not contain the ASP.NET Core development certificate.
grep -rn "new HttpClient\|HttpClientHandler" --include=*.cs .2Fix
Create the client with an HttpClientHandler whose ServerCertificateCustomValidationCallback accepts the localhost certificate, and compile it only into debug builds so production keeps normal validation.
var handler = new HttpClientHandler();
#if DEBUG
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
};
#endif
var client = new HttpClient(handler);3Verify
Run the app in the emulator again and repeat the request.
dotnet build -f net9.0-android -c DebugConfirmed by learn.microsoft.com
Cause 2 of 4
The app calls localhost, which is the emulator itself
1Check
Inside the Android emulator, 10.0.2.2 is the alias of your computer's loopback address. The iOS simulator shares the host network, so localhost works there only.
grep -rn "localhost" --include=*.cs .2Fix
Pick the address by platform with DeviceInfo.
public static string BaseAddress =
DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:5001" : "https://localhost:5001";3Verify
Open the same URL from the app and see a response instead of a certificate or connection error.
curl -k https://localhost:5001/api/todoitems/Confirmed by learn.microsoft.com
Cause 3 of 4
The development certificate is not trusted on your computer
1Check
Installing the .NET SDK installs the ASP.NET Core HTTPS development certificate, but it is not trusted until you trust it once.
dotnet dev-certs https --check --trust2Fix
Trust the certificate with the dev-certs tool. This is a one-time step.
dotnet dev-certs https --trust3Verify
Open the API in a desktop browser without a warning, then retest in the emulator.
dotnet dev-certs https --check --trustConfirmed by learn.microsoft.com
Cause 4 of 4
You only need HTTP for local development
1Check
Apps targeting Android 9 (API 28) or higher block cleartext HTTP unless the app opts in. Check whether the Application attribute sets UsesCleartextTraffic.
grep -rn "UsesCleartextTraffic" Platforms/Android/2Fix
Run the API with an http profile, call http://10.0.2.2:<port>, and allow cleartext for debug builds only.
#if DEBUG
[Application(UsesCleartextTraffic = true)]
#else
[Application]
#endif
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership) { }
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}3Verify
Start the API with the http launch profile and call it from the emulator.
dotnet run --launch-profile httpConfirmed by learn.microsoft.com
How do I stop it coming back?
- Keep the certificate bypass inside #if DEBUG so it can never ship in a release build.
- Keep the base address in one place and choose it by platform.
- Use a certificate from a real certificate authority for any staging or production API.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
Related fixes
Mobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix