Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamutkarshtiwari/6178d2b663448f8439de8c86e2b890ca to your computer and use it in GitHub Desktop.
Save iamutkarshtiwari/6178d2b663448f8439de8c86e2b890ca to your computer and use it in GitHub Desktop.
How to create Custom Glide Module with OkHttp3 integration?

This is the only answer the solved my this issue for me - https://stackoverflow.com/a/46638213

This configuration worked for me -

  • In app module gradle -
    implementation "com.github.bumptech.glide:glide:${glide_version}"
    implementation "com.github.bumptech.glide:okhttp3-integration:${glide_version}"
    kapt "com.github.bumptech.glide:compiler:${glide_version}"
  • In app proguard -
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
  • My Custom Glide module class -
package com.github.iamutkarshtiwari.glidedemoapp;

import android.content.Context;

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 final class OkHttp3GlideModule extends AppGlideModule {

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

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .build();

        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);

        glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
    }
}

Note-

  • Glide 4.0 need not have "GlideModule" declaration in AndroidMinifest.xml
  • You need to Make project in order to generate GlideApp class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment