How to fix "Cleartext HTTP ... not permitted" for Android development

I met Cleartext HTTP traffic to not permitted error when i tried to stream mp3 files with the Exoplayer library.

2019-10-06 18:22:28.363 8724-9511/com.codechacha.exoplayer E/ExoPlayerImplInternal: Source error.$HttpDataSourceException: Unable to connect to
     Caused by: Cleartext HTTP traffic to not permitted

The cause of the problem is that Android doesn't allow HTTP access by default. Accessing HTTPS is no problem, but if the site doesn't support HTTPS, or if you need to access HTTPS, you should handle this.

According to Opt out of cleartext traffic in Android Developer, Starting with Android 9 (API level 28), cleartext support is disabled by default.

So, you have to configure about cleartext HTTP to get access to HTTP sites with API level 28 or above.

Allow all connections to HTTP URLs

You can allow all connections to HTTP URLs if you set the usesCleartextTraffic property to true in AndroidManifest.xml


Allow connections to specific sites.

If you want to allow connections to specific sites. then make a /res/xml/network_security_config.xml file and add some sites.

if you add a domain-config like below, you can allow connections to

<?xml version="1.0" encoding="utf-8"?>
    <domain-config cleartextTrafficPermitted="false">
        <domain includeSubdomains="true"></domain>

Also, you have to let the system know where the config file is. Add following code to your AndroidManifest.xml.



codechachaCopyright ©2019 codechacha