If you’re developing or using an Android app and encounter the error message “net err_cleartext_not_permitted,” it indicates that your app is trying to access a network resource over HTTP instead of HTTPS, which is now restricted by default for security reasons. This error prevents your app from making cleartext (unencrypted) network requests, ensuring user data remains protected. Fortunately, fixing this issue involves updating your app’s network security configuration or code to permit cleartext traffic where appropriate. This guide will walk you through the steps to resolve “net err_cleartext_not_permitted” on Android devices, whether you’re a developer or a user troubleshooting an app problem.
What You’ll Need
- Access to your Android app’s source code (for developers)
- Android Studio installed (for developers)
- Basic understanding of Android development and manifest files
- Device or emulator with the app installed (for testing)
- Optional: Knowledge of network security configuration
Step-by-Step
- Understand the Cause of the Error
When your app attempts to connect to a URL over HTTP (not HTTPS), Android’s default security policy blocks this request, resulting in the “net err_cleartext_not_permitted” error. This is especially common when connecting to local servers, testing environments, or legacy APIs that do not support HTTPS. - Check Your App’s Network Requests
Review your app’s code to identify where network requests are made. Look for URLs starting with “http://”. If you find such requests, consider whether they should be updated to “https://”. If not, you need to explicitly allow cleartext traffic for those domains. - Update Your AndroidManifest.xml
To permit cleartext traffic globally or for specific domains, you need to modify your app’s manifest file. Open `AndroidManifest.xml` and add or update the `android:usesCleartextTraffic` attribute in the “ tag.For allowing cleartext traffic globally (not recommended for production):
xml
…
For more granular control, proceed to step 4. - Create a Network Security Configuration (Optional but Recommended)
For better security practices, especially in production, define a custom network security configuration file.– Create a new XML file in `res/xml/`, e.g., `network_security_config.xml`.
– In this file, specify which domains are permitted cleartext traffic.Example `network_security_config.xml`:
xml
yourdomain.com
– In your `AndroidManifest.xml`, reference this configuration:
xml
… - Rebuild and Test Your App
Save all changes, rebuild your app in Android Studio, and deploy it to your device or emulator. Test to see if the network request now succeeds without errors. - Verify the Fix
Use Chrome’s DevTools or Android’s logcat to monitor network requests and ensure the app can connect over HTTP where permitted. Check that the “net err_cleartext_not_permitted” error no longer appears.
Common Mistakes
- Forgetting to set `android:usesCleartextTraffic=”true”` in the manifest when testing locally.
- Not updating URLs from HTTP to HTTPS, which is the more secure and recommended solution.
- Using broad permissions that allow all cleartext traffic in production, which can pose security risks.
- Failing to create a proper network security configuration file for selective domain access.
- Not rebuilding or reinstalling the app after making changes to the manifest or security config.
Tips
- Prefer updating your server to support HTTPS—it’s safer and more aligned with modern security standards.
- Use a local or staging environment with cleartext traffic enabled only temporarily for testing.
- Always restrict cleartext traffic permissions to specific domains rather than enabling it globally in production.
- Leverage Android Studio’s network profiler and logcat to debug network issues effectively.
- Keep your app and dependencies updated to ensure compatibility with latest security policies.
Troubleshooting
If the error persists after following the steps above, consider these troubleshooting tips:
- Ensure you have correctly added the `android:usesCleartextTraffic=”true”` attribute or network security config and rebuilt your app.
- Check the exact URL your app is requesting and verify if it’s over HTTP or HTTPS. Switch to HTTPS if possible.
- Use Android Studio’s Logcat to look for detailed network errors and confirm if the app is attempting to access disallowed domains.
- Test on different devices or emulators to rule out device-specific issues.
- If you’re using a custom network security configuration, verify the XML syntax and domain entries.
- Ensure your device or emulator’s network settings are correct and that no firewall or proxy is blocking HTTP traffic.
- Consult the official Android developer documentation on network security and cleartext traffic policies for updates or changes.
Frequently Asked Questions
1. Why does my app show “net err_cleartext_not_permitted”?
This error occurs because Android’s security policies block HTTP requests, requiring apps to use HTTPS or explicitly permit cleartext traffic.
2. Is it safe to enable cleartext traffic in my app?
Enabling cleartext traffic can expose data to eavesdropping. It’s recommended only for testing or with secure, trusted domains, and avoid in production.
3. How do I enable cleartext traffic only for specific domains?
Create a custom network security configuration XML and reference it in your manifest, allowing cleartext only for approved domains.
4. Should I always switch to HTTPS instead of fixing this error?
Yes, HTTPS is the preferred solution as it encrypts data in transit, providing better security and compliance with modern standards.
5. Can I fix this error without modifying my app code?
If you’re a user experiencing this error in an app, contact the developer. If you’re a developer, updating the manifest or security config is necessary.
6. Does this error affect all Android versions?
The error primarily affects Android 9 (Pie) and above, where cleartext traffic is disabled by default, but older versions may behave differently.
This comprehensive guide should help you understand, fix, and prevent the “net err_cleartext_not_permitted” error on Android, ensuring your app communicates securely and effectively. Remember always to prioritize HTTPS and secure network practices for the best user experience and data protection.