How to enable TLSv1.3 for OkHttp 3.12.x on Android 8/9?

As shown in official link,
TLSv1.3 is supported from Android 10(Api Level 29) on wards. So to support TLSv1.3 in previous versions we can integrate the conscrypt library. Conscrypt security provider includes a public API for TLS functionality. For that we have to add the dependency,

dependencies {
  implementation 'org.conscrypt:conscrypt-android:2.2.1'
}

Here also we need OkHttp client as it supports conscrypt.

As documented in OkHttp,

OkHttp uses your platform’s built-in TLS implementation. On Java
platforms OkHttp also supports Conscrypt, which integrates BoringSSL
with Java. OkHttp will use Conscrypt if it is the first security
provider.

After adding conscrypt dependency, in application class we just have to mention,

Security.insertProviderAt(Conscrypt.newProvider(), 1);

This can be helpful to provide support and enable TLS 1.3 in older android version (Api level <29).

Leave a Comment