Android 8: Cleartext HTTP traffic not permitted


Android 8: Cleartext HTTP Traffic Not Permitted 😱
Are you experiencing issues with your Android 8 app not showing content from the backend feed? 😩 Don't worry, you're not alone! Many users have reported encountering the following Exception on Android 8:
08-29 12:03:11.246 11285-11285/ E/: [12:03:11.245, main]: Exception: IOException java.io.IOException: Cleartext HTTP traffic to * not permitted
But fear not! I'm here to help you troubleshoot and resolve this problem. Let's dive in! 🏊♂️
Understanding the Exception 😕
The exception message is pretty clear - "Cleartext HTTP traffic to * not permitted." This means that Android 8 (Oreo) has stricter security measures in place and does not allow unencrypted HTTP connections by default. This change aims to enhance the overall security of the operating system. 🛡️
Solution 1: Setting the android:usesCleartextTraffic
Flag 🚩
Some developers may find success by explicitly setting the android:usesCleartextTraffic
flag in the app's manifest file. However, in most cases, this flag is set to true
by default, so manually setting it may not provide a solution. 📝
Solution 2: Using Network Security Configuration 🌐
Android 8 introduced a new approach to handle network security called Network Security Configuration. This configuration file lets you specify which domains or URLs can be accessed using cleartext traffic. By default, it blocks all HTTP traffic unless explicitly allowed.
To get your app working again, follow these steps:
Create a new XML file called
network_security_config.xml
in your app's resource directory (e.g.,res/xml/network_security_config.xml
).Add the following code to the
network_security_config.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">your-domain.com</domain>
</domain-config>
</network-security-config>
In this example, we allow cleartext traffic for the domain your-domain.com
. Replace it with the actual domain(s) your app needs to access.
Modify your app's manifest file (
AndroidManifest.xml
) to include the following lines of code:
<application
android:networkSecurityConfig="@xml/network_security_config"
...
>
...
</application>
Rebuild and run your app, and voila! Your app should now be able to access the backend feed and display the content on Android 8 devices. 🥳
Conclusion and Call-to-Action ✅
In this blog post, we explored the common issue of Cleartext HTTP traffic not being permitted on Android 8 devices. We provided two solutions: setting the android:usesCleartextTraffic
flag and using Network Security Configuration.
If you're facing this problem, give these solutions a try and let us know if they worked for you. Share your experience and any additional insights in the comments below. Happy coding! 🚀👩💻👨💻
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
