ERR_CLEARTEXT_NOT_PERMITTED in debug app on Android

Im using IONIC 5.4.13, cordova 9.0.0 (cordova-lib@9.0.1)

I might be repeating information but for me problem started appearing after adding some plugin (not sure yet). I tried all above combinations, but nothing worked. It only started working after adding:

   <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
           <certificates src="system" />
       </trust-anchors>
   </base-config>

to file in project at

resources/android/xml/network_security_config.xml

so my network_security_config.xml file now looks like:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
           <certificates src="system" />
       </trust-anchors>
   </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.1.25.10</domain>
    </domain-config>
</network-security-config>

Thanks to all.

5 Likes

thanks worked for me

Thank you, this worked for me.

With the base-config section added, it works for me. thank you very much.

Funciono para mi, que no me ejecutaba los api que habia echo en .net

Create a new file /resources/android/xml/network_security_config.xml

Add the following to this file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">com</domain>
    <domain includeSubdomains="true">net</domain>
    <domain includeSubdomains="true">org</domain>
  </domain-config>
</network-security-config>

You may add or remove subdomains according to your needs.

Add the following to your config.xml inside of <platform name="android">

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

This worked for me.
I updated widget to this
<widget id="com.kenyatv.live" version="3.4.8" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

Add xmlns:android=“http://schemas.android.com/apk/res/android

Then added

    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>

Add below snippet inside your config.xml under the platforms tag:

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

Remember to add xmlns:android=“http://schemas.android.com/apk/res/android” attribute in edit-config tag or else it gives error for android platform>=8.

1 Like

for those who use capacitor this answer resolves however you need to use the following command: ionic capacitor run android --livereload --external

1 Like

Lots of suggestion here, regarding lots of problem that leads to the same error i.e. ERR_CLEARTEXT_NOT_PERMITTED .
If you are facing this issue when trying to connect to an api hosted on http rather than https, you are likely to face this error.
for that do these 2 things (assuming ionic4, cordova - 5.4.X)

edit the resources\android\xml\network_security_config.xml like this…

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">dev.somename.com</domain>
        <!-- note this is without http -->
    </domain-config>
</network-security-config>

add this to your config.xml file…

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:usesCleartextTraffic="true" /> <!-- this is the new line to be added -->
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
      <!-- other config goes here -->
</platform>

after this you should be able to call apis from your http:/dev.somename.com/api/vX/anyurl/

Please Note : above configuration is only needed for http, it should work for https without these configuration as well…try hitting this https://swapi.co/api/films/ from your application.

for more detail description of this refer to this Stackoverflow Discussion. and this particular answer

1 Like

Thanks, it worked for me. :smiling_face_with_three_hearts: :heart_eyes:

just run this command :

ionic cordova run android -l --ssl

Thank you so much! It works on Android 9 and 10!

Have you got the solution for same?

Thanks.
1- does network_security_config.xml already exist or does it need to be created? Does matter where?

2- in Config.xml does usesCleartextTraffic=“true” needed? I thought it was turned on by default. Its more if users want to turn it off?

Thanks for your help.

  1. network_security_config.xml is created itself in resources/android/xml, when you add android as a platform for the application
  2. yes the userCleartextTraffic = “true” is given by default just verify if it is like this or not.

this helped me alot thanks for your post.

Finally I got it working by fixing the issue from website’s end then playing around with the apk code.

In case you wish to resolve it from the website code side following tutorial helped : https://www.basezap.com/fix-leartext-error-for-websites/

err_cleartext_not_permitted

This basically explains the basic issue on why this error pops up rather than workaround with android code. A good read.

Fiz isso e resolveu pra mim, obrigado amigo !!!

Hi! I have a solution: You’ve to set text traffic to true and add the ip that you will use instead of localhost.

How? Check if you have network security config file such as:

network_security_config.xml"

In my case the file is in resources/android/xml

Then set the code like this:

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain> //Default domain
        <domain includeSubdomains="true">your_new_ip(ex. 11.0.2.3)</domain> //put the ip that your are using 
    </domain-config>

    <base-config cleartextTrafficPermitted="false"/>
</network-security-config> 

what you have done has been adding a new ip (11.0.2.3) to the security configuration.
Also check that cleartextTrafficPermitted is true

Hope it helps!