APK errors

CLEARTEXT communication not permitted

Error text

java.net.UnknownServiceException: CLEARTEXT communication to api.example.com not permitted by network security policy

Cause

From Android 9 up, http:// requests are blocked by default. The app is trying to reach an unencrypted address.

Step-by-step fix

  1. Best fix: serve the backend over https:// and change the address.
  2. For local testing only, create network_security_config.xml and allow the specific domain (10.0.2.2, localhost).
  3. Reference the file in <application android:networkSecurityConfig=...>.
  4. Do not set usesCleartextTraffic="true" for all traffic; keep it in the debug build only.
  5. Test the release APK on a real phone: a network error can turn into a white screen there.

Example commands

<!-- res/xml/network_security_config.xml -->
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">10.0.2.2</domain>
  </domain-config>
</network-security-config>
<application android:networkSecurityConfig="@xml/network_security_config" ...>

How to prevent it

Use HTTPS only in release and keep the http allowance in the debug source set.

Author and license: Nibras Code