Java

How to allow all Network connection types HTTP and HTTPS in Android 9 Pie

27 September 2026 · 10 min read

How to allow all Network connection types HTTP and HTTPS in Android 9 Pie

Developing Android applications often requires handling network connections, and on Android (9) Pie, security enhancements made allowing both HTTP and HTTPS connections a bit more complex. By default, Android Pie prioritizes secure (HTTPS) connections, which is a good practice for protecting user data. However, there are legitimate reasons why a developer might need to allow HTTP connections, particularly during development or when interacting with legacy systems. This guide provides a comprehensive walkthrough on how to allow all network connection types HTTP and HTTPS in Android (9) Pie, covering various methods and best practices to ensure your application functions as intended while maintaining a reasonable level of security. We’ll explore configuration options, code snippets, and troubleshooting tips to help you navigate this process smoothly. Understanding these nuances is crucial for building robust and flexible Android applications that can adapt to different network environments.

Understanding Android Network Security Configuration

Android Pie introduced stricter network security configurations to enhance user privacy and prevent man-in-the-middle attacks. By default, applications targeting API level 28 (Android 9) and above are required to use HTTPS for all network traffic. This means that if your application attempts to connect to a server using HTTP, it will be blocked, and you’ll likely encounter a Cleartext HTTP traffic not permitted error. This change was implemented to encourage developers to adopt secure communication protocols and protect sensitive data transmitted over the network. According to Google’s official documentation [Android Network Security Configuration], developers can customize these settings through a network security configuration file.

The network security configuration file allows you to specify which domains can use cleartext traffic (HTTP) and which must use secure traffic (HTTPS). This provides a granular level of control over your application’s network security policies. You can define different configurations for different domains, allowing you to selectively enable HTTP for specific servers while enforcing HTTPS for others. The file is an XML resource that you include in your application’s res/xml directory, and it’s referenced in your application’s manifest file. This mechanism offers a flexible approach to managing network security, accommodating various development and deployment scenarios. For instance, you might allow HTTP for a local development server but enforce HTTPS for production environments.

Consider a scenario where you are developing an application that needs to communicate with a legacy server that only supports HTTP. Instead of completely rewriting the server to support HTTPS (which may not be feasible in the short term), you can use the network security configuration file to allow cleartext traffic for that specific server. This allows your application to function correctly while you work on migrating the legacy server to a more secure protocol. However, it’s crucial to understand the security implications of allowing cleartext traffic and to implement appropriate measures to mitigate any potential risks. Remember, allowing HTTP connections can expose your users’ data to interception and tampering.

Configuring Network Security Policy to Allow HTTP

To allow HTTP traffic in your Android Pie application, you need to create and configure a network security configuration file. This file will override the default security settings and allow your application to connect to servers using HTTP. The process involves creating an XML file, specifying the domains for which HTTP is allowed, and referencing this file in your application’s manifest. This approach provides a controlled way to manage network security exceptions.

Here’s how to create and configure the network security configuration file:

  1. Create a new XML file named network_security_config.xml in the res/xml directory of your Android project.
  2. Add the following content to the file:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">yourdomain.com</domain> </domain-config> </network-security-config> 
  1. Replace yourdomain.com with the actual domain for which you want to allow HTTP traffic. The includeSubdomains=“true” attribute ensures that all subdomains of the specified domain are also allowed to use HTTP.
  2. If you need to allow HTTP for all domains (which is generally not recommended for production apps), you can use the following configuration:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config> 
  1. Finally, reference the network_security_config.xml file in your application’s manifest file by adding the android:networkSecurityConfig attribute to the tag:
<application android:networkSecurityConfig="@xml/network_security_config" ...> <!-- Your activities and other components --> </application> 

This configuration allows cleartext traffic for the specified domain(s) while maintaining the default HTTPS requirement for all other connections. Remember to exercise caution when allowing HTTP traffic, especially in production environments. Always prioritize secure communication protocols whenever possible. Refer to the official Android documentation [Android APK Analyzer] for more information on debugging and analyzing your application’s network traffic.

Best Practices and Security Considerations

While allowing HTTP traffic might be necessary in some situations, it’s essential to be aware of the security implications and follow best practices to mitigate potential risks. Allowing cleartext traffic exposes your users’ data to interception and tampering, especially on public networks. Therefore, it’s crucial to carefully evaluate the need for allowing HTTP and implement appropriate security measures.

Here are some best practices and security considerations to keep in mind:

  • Minimize the use of HTTP: Only allow HTTP for specific domains or scenarios where it’s absolutely necessary.
  • Use HTTPS whenever possible: Prioritize secure communication protocols to protect user data.
  • Implement end-to-end encryption: Even if you allow HTTP for some connections, consider using end-to-end encryption to protect sensitive data.
  • Educate users about the risks: Inform users about the potential risks of using your application on insecure networks.
  • Monitor network traffic: Regularly monitor your application’s network traffic for any suspicious activity.

For instance, if you’re using HTTP to communicate with a legacy API, consider implementing a proxy server that encrypts the data before sending it over the network. This can help protect user data even when communicating with an insecure backend. Additionally, you should regularly update your application’s dependencies and security configurations to address any newly discovered vulnerabilities. Proper security hygiene is essential for maintaining a secure and trustworthy application.

Consider a real-world example where a financial application needs to communicate with a payment gateway that only supports HTTP. In this scenario, allowing HTTP traffic would be a significant security risk. Instead, the application should use a secure intermediary server that handles the communication with the payment gateway over HTTP and then securely transmits the data to the application using HTTPS. This approach minimizes the exposure of sensitive financial data to potential attackers. “Security is not a product, but a process,” as Bruce Schneier famously said, highlighting the ongoing nature of security efforts.

Troubleshooting Common Issues

Even with careful configuration, you might encounter issues when allowing HTTP traffic in your Android Pie application. Common problems include the Cleartext HTTP traffic not permitted error, certificate validation failures, and unexpected network behavior. Troubleshooting these issues requires a systematic approach and a good understanding of Android’s network security configuration.

Here are some common issues and troubleshooting tips:

  • Cleartext HTTP traffic not permitted error: This error indicates that your application is attempting to connect to a server using HTTP, but the network security configuration is blocking cleartext traffic. Double-check your network_security_config.xml file and ensure that the domain for which you’re allowing HTTP is correctly specified. Also, verify that the android:networkSecurityConfig attribute is correctly set in your application’s manifest file.
  • Certificate validation failures: If you’re using HTTPS, ensure that the server’s SSL certificate is valid and trusted by the Android system. Invalid or self-signed certificates can cause connection errors. You can use tools like SSL Labs [SSL Labs] to test the validity of your server’s SSL certificate.
  • Unexpected network behavior: If you’re experiencing unexpected network behavior, such as slow connections or intermittent failures, check your application’s network code for any potential issues. Use debugging tools to monitor network traffic and identify any bottlenecks or errors.

For example, if you’re encountering certificate validation failures, you might need to add the server’s certificate to your application’s trust store. However, this should only be done as a last resort, as it can weaken the security of your application. A better approach is to ensure that the server’s SSL certificate is correctly configured and trusted by the Android system. Another common issue is related to the Android emulator. Sometimes, the emulator’s network configuration can interfere with your application’s network traffic. Try restarting the emulator or using a physical device to test your application.

Featured Snippet: To allow HTTP traffic in Android Pie, create a network_security_config.xml file in your res/xml directory. Within this file, define a domain-config with cleartextTrafficPermitted=“true” for the specific domain you need to access via HTTP. Then, reference this file in your app’s manifest using android:networkSecurityConfig="@xml/network_security_config" within the tag. This config allows developers to selectively enable HTTP connections for specific domains while maintaining HTTPS enforcement for others. This is especially useful for development or when interacting with legacy systems.

Infographic here
FAQ ---
**Q: Why is HTTP traffic blocked by default in Android Pie?**
A: Android Pie enforces HTTPS by default to enhance user privacy and security by preventing man-in-the-middle attacks and ensuring data integrity.
**Q: Is it safe to allow HTTP traffic in my application?**
A: Allowing HTTP traffic introduces security risks, as data transmitted over HTTP is not encrypted and can be intercepted. It's generally recommended to use HTTPS whenever possible.
**Q: How do I allow HTTP traffic for all domains in my application?**
A: You can allow HTTP traffic for all domains by setting cleartextTrafficPermitted="true" in the base-config of your network security configuration file. However, this is generally not recommended for production apps.
**Q: What are the alternatives to allowing HTTP traffic?**
A: Alternatives include migrating your server to HTTPS, using a secure proxy server, or implementing end-to-end encryption.
**Q: How can I test if my network security configuration is working correctly?**
A: You can use debugging tools to monitor your application's network traffic and verify that HTTP connections are allowed for the specified domains.
Allowing all network connection types, both HTTP and HTTPS, in Android Pie requires careful consideration and a thorough understanding of the security implications. By following the steps outlined in this guide, you can configure your application to allow HTTP traffic while maintaining a reasonable level of security. Remember to prioritize HTTPS whenever possible and implement appropriate measures to protect user data. Understanding your app's networking needs is vital, as is being able to quickly adjust to changing requirements.

Now that you’re equipped with the knowledge to handle network connections in Android Pie, consider exploring other aspects of Android development, such as data persistence and user interface design. Dive deeper into secure coding practices to fortify your applications against potential threats. Take the next step and explore advanced network security techniques to further enhance the robustness and security of your Android applications.

Question & Answer :
From Android 9 Pie now, requests without encryption will never work. And by default, the System will expect you to use TLS by default.You can read this feature here So if you only make requests via HTTPS you are safe. But what about apps that make requests through different sites, for instance, browser-like apps.

How can I enable requests to all types of connections HTTP and HTTPS in Android 9 Pie?

The easy way to implement this is to use this attribute to your AndroidManifest.xml where you allow all http for all requests:

<application android:usesCleartextTraffic="true"> </application> 

But in case you want some more configurations for different links for instance, allowing http for some domains but not other domains you must provide res/xml/networkSecurityConfig.xml file.

To do this in Android 9 Pie you will have to set a networkSecurityConfig in your Manifest application tag like this:

<?xml version="1.0" encoding="utf-8"?> <manifest ... > <application android:networkSecurityConfig="@xml/network_security_config"> </application> </manifest> 

Then in your xml folder you now have to create a file named network_security_config just like the way you have named it in the Manifest and from there the content of your file should be like this to enable all requests without encryptions:

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

From there you are good to go. Now your app will make requests for all types of connections. For additional information on this topic read here.