How do I fixed ERR_CLEARTEXT_NOT_PERMITTED on cordova.

It’s quite frustrating when something unexpectedly stops working, right?

There are many topics out there on the subject but for some reason they don’t seem to work as expected.

So let’s understand why we are having this issue. Since sdkVersion 28 the property “usesClearTextTraffic” changed from “true” to “false.

If you want to allow the app to comunicate with certain domains in non HTTPS there are solution for this (solution 3). If you simply don’t need to target the version 28, then there another solution (solution 1), or simply whitelist all domains by setting this property to true again.

Make sure you are using cordova-android  v8.1.0 – (v8.0.0 was not working for me!!) 

(Check on package.json)

Solution 1:

Add this to config.xml

 

<preference name="android-targetSdkVersion" value="27" />

Then run cordova prepare android to propagate changes to your platform/android folder.

 

Solution 2:

Add this to config.xml

 

<edit-config 
xmlns:android="http://schemas.android.com/apk/res/android" 
file="app/src/main/AndroidManifest.xml" 
mode="merge" 
target="/manifest/application"> 
   <application android:usesCleartextTraffic="true" /> 
</edit-config>

 

Then run cordova prepare android to propagate changes to your platform/android folder.

Solution 3:

Whilelist certain domains or IPS using  the network_security_config.xml file.

If you don’t have one yet, create it at the root of your project, then add the content below (update it to match your needs).

 

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!--    https://developer.android.com/training/articles/security-config -->
    <domain-config cleartextTrafficPermitted="true">

<!-- example where I whitelist a non HTTPS domain -->
        <domain includeSubdomains="true">my_domain.io</domain>

<!-- example where I whitelist a local IP -->
        <domain includeSubdomains="true">192.168.0.190</domain>

<!-- you can add as much as you want -->

    </domain-config>
</network-security-config>

 

Then update the config.xml

 

<platform name="android">
    <edit-config xmlns:android="http://schemas.android.com/apk/res/android" file="app/src/main/AndroidManifest.xml"
                 mode="merge" target="/manifest/application">
        <application android:networkSecurityConfig="@xml/network_security_config"/>
    </edit-config>
    <resource-file src="network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml"/>
</platform>

 

 

Leave a Reply

Close Menu