175

After upgrading to Cordova Android 8.0, I am seeing net::ERR_CLEARTEXT_NOT_PERMITTED errors when trying to connect to http:// targets.

Why is that and how can I resolve this?

0

22 Answers 22

290

The default API level in the Cordova Android platform has been upgraded. On an Android 9 device, clear text communication is now disabled by default.

To allow clear text communication again, set the android:usesCleartextTraffic on your application tag to true:

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

As noted in the comments, if you have not defined the android XML namespace previously, you will receive an error: unbound prefix during build. This indicates that you need to add it to your widget tag in the same config.xml, like so:

<widget id="you-app-id" version="1.2.3"
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">
11
  • 44
    I encountered an error (error: unbound prefix.) when trying to build my Cordova application. The solution was to add in my config.xml file, in the my root <widget> element the attribute xmlns:android="http://schemas.android.com/apk/res/android
    – apiaget
    Commented Mar 17, 2019 at 14:49
  • @Der Hochstapler Can you add apiagets comment to your answer?
    – Michael B
    Commented May 28, 2019 at 8:47
  • 1
    @MichaelB Everyone can edit answers and is welcome to do so. But, sure, I'll do it. Commented May 28, 2019 at 8:58
  • 1
    @SteevJames Did you rebuild your Cordova project so that the changes are actually persisted into the Android project structure? Have you verified that the changes have been made to the AndroidManifest.xml in your Android project? Commented Sep 10, 2019 at 7:59
  • 3
    In short, I added this line to my manifest under <application> and it fixed my issue: android:usesCleartextTraffic="true" Commented Dec 20, 2019 at 17:27
54

There are two things to correct in config.xml So the right answer should be adding the xmls:android:

<widget id="com.my.awesomeapp" version="1.0.0" 
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">

plus editing the config to allow:

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

If step 1 is avoided error: unbound prefix. will appear

1
  • 1
    results syntax error in config.xml file
    – zakk616
    Commented May 15, 2022 at 10:07
49

Cleartext here represents unencrypted information. Since Android 9, it is recommended that apps should call HTTPS APIs to make sure there is no eves dropping.

However, if we still need to call HTTP APIs, we can do following:

Platform: Ionic 4

Create a file named: network_security_config.xml under project-root/resources/android/xml

Add following lines:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
     <domain>ip-or-domain-name</domain>
   </domain-config>
</network-security-config>

Now in project-root/config.xml, update following lines:

<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" />
        <application android:networkSecurityConfig="@xml/network_security_config" />
    </edit-config>
    ... other statements...

It should work now.

5
  • 1
    Thanks bro, It worked for me for Ionic 4. I specified domain name <domain>example.com</domain> and it worked. Also got help from this "developer.android.com/training/articles/security-config" Commented Aug 24, 2019 at 2:40
  • Why is the "<edit-config.." OVERWRITTEN each time i run the app ?
    – Jalle
    Commented Oct 12, 2019 at 7:43
  • 5
    This works but I need to add this line in config.xml before the edit-config tag: <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />. Thanks!
    – tyn
    Commented Nov 7, 2019 at 9:39
  • @ZakiMohammed please share your code i am also facing a same problem help!
    – jose
    Commented Nov 7, 2019 at 12:42
  • @jose please have a look to my answer Commented Nov 7, 2019 at 13:31
39

To solve the problem there's other option. in file resources/android/xml/network_security_config.xml. insert:

<network-security-config>
   <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
           <certificates src="system" />
       </trust-anchors>
   </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain>localhost</domain>
        <domain includeSubdomains="true">192.168.7.213:8733</domain>
    </domain-config>
</network-security-config>

Im my case I´m using IP address then base-config is necessary, but if you have a domain. just add the domain.

2
  • 1
    While all the accepted solution was also required, this answer was key for the problem I was facing. Commented Aug 2, 2019 at 10:15
  • Thank you! I'm also using an IP instead of a domain name and your answer fit perfectly!
    – early
    Commented Apr 23, 2020 at 17:09
37

I ran into this problem myself today, and found a really nifty plugin that will save you the hassle of trying to manually allow cleartext traffic in Android 9+ for your Apache Cordova application. Simply install cordova-plugin-cleartext, and the plugin should take care of all the behind the scenes Android stuff for you.

$ cordova plugin add cordova-plugin-cleartext
$ cordova prepare
$ cordova run android
3
  • 1
    For others looking for the answer. I kept getting hit by other errors when applying the config fixes, but this worked straight away. Thanks Topher
    – IvanSt
    Commented Dec 14, 2019 at 10:58
  • Works for me. Thx!
    – iwanuschka
    Commented Jun 7, 2020 at 19:43
  • For those using, Ionic don't forget "ionic cordova prepare" Commented Apr 3, 2021 at 3:17
19

After a few days of struggle, this works for me, and I hope this also works for you.

add this to your CONFIG.XML, top of your code.

<access origin="*" />
<allow-navigation href="*" />

and this, under the platform 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" />
     <application android:networkSecurityConfig="@xml/network_security_config" />
 </edit-config>
 <resource-file src="resources/android/xml/network_security_config.xml" 
 target="app/src/main/res/xml/network_security_config.xml" />

add the follow code to this file "resources/android/xml/network_security_config.xml".

<?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">YOUR DOMAIN HERE/IP</domain>
    </domain-config>
</network-security-config>

enter image description here

enter image description here

6
  • Can you please answer this ?stackoverflow.com/questions/59116787/… Commented Dec 3, 2019 at 12:04
  • 1
    thanks for you solution, It's the only one that worked for me.
    – Lud Osorio
    Commented Dec 3, 2019 at 17:58
  • Thanks, its the only solution one that worked for me to. Commented Jan 28, 2020 at 17:06
  • This was the solution that worked for me. Specifically it seemed the trust-anchors tags was the final component to get all the other stuff working.
    – rolinger
    Commented Mar 15, 2020 at 16:56
  • If you are using phonegab build then keep network_security_config on root folder and give path as resource-file src="network_security_config.xml"
    – mujaffars
    Commented Jul 25, 2020 at 5:38
15

Adding the following attribute within the opening < widget > tag worked for me. Simple and live reloads correctly on a Android 9 emulator. xmlns:android="http://schemas.android.com/apk/res/android"

<widget id="com.my.awesomeapp" version="1.0.0" 
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">
3
  • 2
    I did not realize when I saw this answer, but this is in addition to the edit-config code from the accepted answer (fixes the unbound prefix error)
    – RishiG
    Commented May 20, 2019 at 22:52
  • @RishiG now it is an answer that explains the full right answer
    – zardilior
    Commented May 22, 2019 at 18:09
  • @zardilior Yes, congrats on him for hijacking the answer on an edit... lol Commented Oct 28, 2019 at 15:12
11

Im using IONIC 5.4.13, cordova 9.0.0 ([email protected])

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.

9
  • How is this different from the existing Ionic solution(s)? Commented Jan 23, 2020 at 9:17
  • situation is different. remember people come here from google search. there might be different situation and settings they land in this issue. in my case my project was working fine and it just started giving this error even i have not done any platform update or device update.
    – Rajendra
    Commented Jan 23, 2020 at 14:52
  • 1
    Thanks champ, this worked for me :) -- it's nice to have the confirmation of version updates for working solutions, as time goes by and old solutions no longer work, especially in a volatile ever-changing framework.
    – Grant
    Commented Mar 21, 2020 at 12:44
  • 2
    thanks mate, wanted to say that for me this was the only working solution
    – Anakin001
    Commented May 18, 2020 at 15:54
  • 2
    Thanks. This solved my problem. I tried many other suggestions but was not working.
    – MasterJedi
    Commented Jun 4, 2020 at 2:41
10

After reading the whole discussion looking for a way to authorize communication to all IP addresses as in my case the IP address to where the request will be sent is defined by the user in an input text and can not be defined in the configuration file. Here is how I resolved the issue

here are the configuration

config.xml

<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>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
...
</platform>

resources/android/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

The most important piece of code is <base-config cleartextTrafficPermitted="true" /> in <network-security-config> instead of domain-config

1
  • setting the base-config element resolved the issue, thanks a ton! 🍻
    – Nexus
    Commented Oct 25, 2021 at 21:30
7

you should add

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

to

resources/android/xml/network_security_config.xml

like this

<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-config> </network-security-config>
1
7

Just add this line to platforms/android/app/src/main/AndroidManifest.xml file

<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" **android:usesCleartextTraffic="true"**>
0
6

Following is the solution which worked for me. The files which I updated are as follows:

  1. config.xml (Full Path: /config.xml)
  2. network_security_config.xml (Full Path: /resources/android/xml/network_security_config.xml)

Changes in the corresponding files are as follows:

1. config.xml

I have added <application android:usesCleartextTraffic="true" /> tag within <edit-config> tag in the 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" />
        <application android:networkSecurityConfig="@xml/network_security_config" />
    </edit-config>
    ...
<platform name="android">

2. network_security_config.xml

In this file I have added 2 <domain> tag within <domain-config> tag, the main domain and a sub domain as per my project requirement

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">mywebsite.in</domain>
        <domain includeSubdomains="true">api.mywebsite.in</domain>
    </domain-config>
</network-security-config>

Thanks @Ashutosh for the providing the help.

Hope it helps.

0
3

Following solution worked for me-

goto resources/android/xml/network_security_config.xml Change it to-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
    </domain-config>
</network-security-config>
2

Old ionic cli (4.2) was causing issue in my case, update to 5 solve the problem

1
  • Thank you. This was the solution for me. I had a separate build process running on App Center that was installing ionic to run ionic cli commands. They have deprecated "ionic" in favor of "@ionic/cli". Commented Jun 2, 2020 at 2:31
2

I am running Ionic 5 with Vue and Capacitor 3 and was getting this error using the InAppBrowser for a website that doesn't support https. For Capacitor apps, config.xml isn't used and AndroidManifest.xml is edited directly.

First, create the Network Security Config file here YOUR_IONIC_APP_ROOT\android\app\main\res\xml\network_security_config.xml.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
        <domain>www.example.com</domain>
   </domain-config>
</network-security-config>

Then edit YOUR_IONIC_APP_ROOT\android\app\main\AndroidManifest.xml adding android:networkSecurityConfig="@xml/network_security_config" to application.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.ionic.starter">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        
        android:networkSecurityConfig="@xml/network_security_config"

        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!-- ... -->
    </application>
    <!-- ... -->
</manifest>
1

@Der Hochstapler thanks for the solution.
but in IONIC 4 some customization in project config.xml work for me

Add a line in Widget tag

<widget id="com.my.awesomeapp" version="1.0.0" 
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">

after this, in the Platform tag for android customize some lines check below
add usesCleartextTraffic=true after networkSecurityConfig and resource-file tags

 <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>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>
    </platform>
2
  • How is this different from the existing Ionic solution(s)? Commented Jan 23, 2020 at 9:17
  • i was added "android:usesCleartextTraffic="true" with "android:networkSecurityConfig="@xml/network_security_config" inside single <edit-config> tag so that was not working for me and when i added new <edit-config> tag for "android:usesCleartextTraffic="true"" and also paste after "android:networkSecurityConfig" <edit-config> tag then it's working for me
    – yash
    Commented Jan 24, 2020 at 10:24
0

We are using the cordova-custom-config plugin to manage our Android configuration. In this case the solution was to add a new custom-preference to our config.xml:

    <platform name="android">

        <preference name="orientation" value="portrait" />

        <!-- ... other settings ... -->

        <!-- Allow http connections (by default Android only allows https) -->
        <!-- See: https://stackoverflow.com/questions/54752716/ -->
        <custom-preference
            name="android-manifest/application/@android:usesCleartextTraffic"
            value="true" />

    </platform>

Does anybody know how to do this only for development builds? I would be happy for release builds to leave this setting false.

(I see the iOS configuration offers buildType="debug" for that, but I'm not sure if this applies to Android configuration.)

0

In an Ionic 4 capacitor project, when I packaged and deployed to android phone for testing I got this error. Resolved by re-installing capacitor and updating android platform.

npm run build --prod --release
npx cap copy
npm install --save @capacitor/core @capacitor/cli
npx cap init
npx cap update android
npx cap open android
0

If You have Legacy Cordova framework having issues with NPM and Cordova command. I would suggest the below option.

Create file android/res/xml/network_security_config.xml -

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
    <base-config cleartextTrafficPermitted="true" />
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
    </domain-config>
    </network-security-config>

AndroidManifest.xml -

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ...>
        <uses-permission android:name="android.permission.INTERNET" />
        <application
            ...
            android:networkSecurityConfig="@xml/network_security_config"
            ...>
            ...
        </application>
    </manifest>
3
  • How is this different from the existing Ionic solution(s)? Commented Jan 23, 2020 at 9:17
  • Some legacy projects doesn't follow NPM and Cordova commands which has to be manually updated.
    – Pradeepta
    Commented Jan 24, 2020 at 17:32
  • why <base-config cleartextTrafficPermitted="true" /> and not false? you would like to only allow your custom domain and block others, that's what the domain-config element is for
    – pantos27
    Commented Sep 1, 2020 at 18:44
0

I put manifest -- aplication -- android:usesCleartextTraffic="true"

and build.gradle - defaut config -- useLibrary 'org.apache.http.legacy'

works for me

0

This solution works with ionic 7:

AndroidManifest.xml:

<application  
android:icon="@mipmap/icon" 
android:label="@string/app_name" 
... 
android:usesCleartextTraffic="true" // add this line
>
0

There are plenty of answers/suggestions on this now (kind of too many) but just sharing what worked for me to (finally) get rid of the "ERR_CLEARTEXT_NOT_PERMITTED" issue on Android (as of 11/2023):

    <!-- add this to in the main widget node of your Cordova config.xml -->
    <preference name="android-usesCleartextTraffic" value="true" />

This will add the android:usesCleartextTraffic="true" to the <application> node of your AndroidManifest.xml file when you $ cordova build android.

Not the answer you're looking for? Browse other questions tagged or ask your own question.