Glide SSL Handshake exception

How to fix Android Glide SSL exception?

Mustafa Yanık
2 min readMay 24, 2020

--

Hello everyone,

I decided to write about the Android Glide third-party library. We generally use this library in all projects. I’m going to talk about Glide SSL Handsake exception fix.

Important: This method supports above version 4th .

First of all, we will add the necessary section to build.gradle.

implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.github.bumptech.glide:annotations:4.11.0'
implementation "com.github.bumptech.glide:okhttp3-integration:4.11.0"
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

After, we will sync project and we will add 2 class. Firstly, we will add MyGlideModule annotation class.

MyGlideModule.java

import android.content.Context;


import androidx.annotation.NonNull;


import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import java.io.InputStream;
import okhttp3.OkHttpClient;


@GlideModule
public class MyGlideModule extends AppGlideModule {


@Override
public void applyOptions(Context context, GlideBuilder builder) {
super.applyOptions(context, builder);
}


@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
OkHttpClient okHttpClient= UnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}

Then, we will insert UnsafeOkHttpClient class in project.

UnsafeOkHttpClient.java

import java.security.cert.CertificateException;


import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import okhttp3.OkHttpClient;


public class UnsafeOkHttpClient {

public static OkHttpClient getUnsafeOkHttpClient() {
try {
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}


@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}


@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};


// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());


// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();


OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier((hostname, session) -> true);


OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Yes. Now, we will clean&build project and use like this.

GlideApp.with(context).load("Image Url").into(imageView);

As a result, we will not receive an SSL Handshake exception and we will be able to use this library stably.

--

--