Android 11: Cleartext communication not enabled for client

Viewed 524

I am creating an android application where user has to login via otp and to get the otp a request has to be made to an HTTP url.

It's working perfectly in all android versions except ANDROID 11 stating that:

CLEARTEXT communication not enabled for client

I added a network security config and have set usesCleartextTraffic="true" in the manifest file. The network security config is working fine for android 9 and 10 but it doesn't work on android 11. I am using OKHTTP to make the request.

Any help would be much appreciated.

2 Answers

Ok , I solved it and keeping this question alive so that it may help someone with same problem. I solved it by changing the OkHttpClient client = new OkHttpClient()

to

OkHttpClient client = new OkHttpClient.Builder() .connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT,ConnectionSpec.MODERN_TLS)) .build();

Add this to the application tag of AndroidManifest.xml

android:usesCleartextTraffic="true"

Final look,

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.App"
    android:usesCleartextTraffic="true"> // add this line
Related