2

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.

CodingM
  • 350
  • 3
  • 18
Hriday Sarma
  • 139
  • 8

2 Answers2

2

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();

Hriday Sarma
  • 139
  • 8
1

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
Shahriar Nasim Nafi
  • 1,160
  • 15
  • 19
  • That is not the solution i tried it and it does not work in android 11 to send request . Additional parameters have to be added to make network requests – Hriday Sarma Apr 15 '21 at 18:46