Android Studio - SSLHandshakeException: Trust anchor for certification path not found
Encountering a javax.net.ssl.SSLHandshakeException with the specific message "Trust anchor for certification path not found" usually signals one of two distinct scenarios: your application is attempting to verify a certificate against an unknown root, or you are inadvertently trying to use SSL/TLS encryption for a connection that isn't actually secured. The latter is often the most overlooked cause, particularly when developing against local development environments.
When connecting to a server like https://192.168.0.159/LoginRegister/signup.php, Android's default security model expects a valid certificate chain signed by a trusted Certificate Authority (CA). If you are running this URL from your own computer and accessing it via HTTP, but your code specifies HTTPS, the handshake fails immediately because there is no SSL certificate to validate against a trust anchor. The client initiates an encrypted session request, but the server responds with plain text or rejects the upgrade, resulting in the exception.
As noted in developer discussions, a common pitfall involves a simple typo: attempting to access https:// when the backend is only serving http://. If your local PHP application on 192.168.0.159 does not have an SSL certificate installed or configured, forcing an HTTPS request will trigger this exact exception. The error log might look clean, but the underlying cause is often that you are trying to encrypt a connection to a resource that expects unencrypted traffic.
Configuring Network Security for Local Development
If you confirm that your server is indeed serving content over SSL/TLS but Android still rejects itâperhaps because you are using a self-signed certificate or a development CA not in the system trust storeâyou must explicitly configure your app to bypass strict certificate validation for localhost.
In Android Gradle Plugin version 7.0.4 and above, relying on android:usesCleartextTraffic="true" is insufficient for HTTPS connections. That attribute only enables cleartext traffic for HTTP; it does not disable SSL verification. To resolve this, you need to define a custom network security configuration.
Create an XML file in your src/main/res/xml directory named network_security_config.xml. Populate it to allow cleartext traffic and explicitly permit connections to your specific IP address without certificate validation:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.0.159</domain>
<!-- If you have a custom CA, add the certificate path below -->
<!-- <trust-anchors>
<certificates src="file" path="certificate.crt"/>
</trust-anchors> -->
</domain-config>
</network-security-config>
Next, apply this configuration to your AndroidManifest.xml:
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config">
<!-- Your activities and other components -->
</application>
Understanding the Trade-offs
While this approach resolves the immediate exception, it introduces a significant security risk. By setting cleartextTrafficPermitted="true" or disabling certificate validation within the domain config, your application accepts any certificate presented by the server, including expired ones, self-signed certificates with incorrect hostnames, or malicious man-in-the-middle attacks.
This configuration should strictly be limited to development environments (e.g., local IP addresses like 192.168.x.x or localhost). For production builds targeting real users, you must ensure that your servers present valid certificates from trusted CAs (like Let's Encrypt) and remove any bypass configurations. Production apps should never trust arbitrary certificates found on a network.
Addressing the AVD Error Log
You may also see an unrelated error in your logs, such as:avc: denied { find } for pid=5704 uid=10342 name=tethering scontext=u:r:permissioncontroller_app:s0...
This specific Android Virtual Device (AVD) denial is typically related to the emulator's networking stack attempting to access tethering services or DNS resolution issues within the simulated environment. It does not directly cause the SSLHandshakeException. You can usually ignore this in local development unless your app specifically requires tethering permissions, which would require granting the ACCESS_NETWORK_STATE permission and potentially running the app on a physical device for full functionality.
Final Verification
After updating the manifest and XML files, you must rebuild the project and ensure the new configuration is installed on your emulator or device. Sometimes Android Studio caches old build artifacts; a clean rebuild ensures the network security config takes effect. If the issue persists, double-check that your URL scheme matches your server's protocol exactly. If you are running PHP directly on localhost without an SSL stack, simply change your connection URL from https://192.168.0.159 to http://192.168.0.159.