How enable Tls 1.1 and 1.2 in react native (android)

Viewed 2930

Well, I'm developing an app for android and IOS platform, inside my app I'm fecthing data from an API. I realised that my app can't fetch data in some android versions. I tested in andoroid 4.1, 4.4 and 5.1. These versions do not fecth data. But Android 7, fetch and I do not have problems.

I was looking for the solution, I found that androids versions using minSdkVersion 16, they do not have TLS 1.1 and 1.2 enable by default. (This is implemented from 20 sdkVersion) And to enable them, I will have to do with Java. I'm just working with react-native 3 months ago and I'm not a java programmer. So the stuffs i'm seeing around, do not tell me exactly what I'll have to do. Can any one tell me steps to enable TLS 1.1 and TLS 2.2 in a react-native android app. I just see exemples that give the code to add, they do not explain or tell the steps. And I also read a comment that says by enabling the TLS 1.1 and 1.2 the app can't be uploaded in play store.

2 Answers

add this library in android build.gradle

implementation 'com.google.android.gms:play-services-safetynet:+'

and add this code to your MainApplication.java

import android.content.Intent;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.security.ProviderInstaller;
import com.google.android.gms.security.ProviderInstaller.ProviderInstallListener;

@Override
  public void onCreate() {
    super.onCreate();
    upgradeSecurityProvider();
    SoLoader.init(this, /* native exopackage */ false);
  }

  private void upgradeSecurityProvider() {
    ProviderInstaller.installIfNeededAsync(this, new ProviderInstallListener() {
      @Override
      public void onProviderInstalled() {

      }

      @Override
      public void onProviderInstallFailed(int errorCode, Intent recoveryIntent) {
//        GooglePlayServicesUtil.showErrorNotification(errorCode, MainApplication.this);
        GoogleApiAvailability.getInstance().showErrorNotification(MainApplication.this, errorCode);
      }
    });
  }

After numerous attempts to solve it, there is only one way that worked for me. Essentially you need to add a Java security package from Google to enable TLSv1/ TLSv1.1/ TLSv1.2 on old Android versions (< 5.0). This will also add support for TLSv1.3 as well as increase the apk size, but I guess we'll have to live with it.

Add the package in app/build.gradle:

implementation 'org.conscrypt:conscrypt-android:2.1.0'

Update MainApplication.java to use the new security provider:

import java.security.Security;
import javax.net.ssl.SSLContext;
import org.conscrypt.OpenSSLProvider;
...

@Override
public void onCreate() {
super.onCreate();

Security.insertProviderAt(new org.conscrypt.OpenSSLProvider(), 1);

//...
} 
Related