After learning about various options for loading and displaying images, we'll look at changing the fundamental network stack for Glide. This guide assumes you're using Gradle.
Glide Series Overview
Integrating Network Stacks
An important piece of displaying images from the Internet is downloading them via HTTP/HTTPS. While the standard Android network packages do their job, there have been quite a few developments for improved networking on Android. Each library has its own advantages and disadvantages. In the end, it comes down to project fit and the developer's personal taste.
The developers of Glide don't want to force their preferred network library onto you, so Glide is fairly HTTP/S network agnostic. Theoretically, it can work with any implementation, which fulfills basic network capabilities. Integrating a network with Glide is not completely seamless. It requires an interface setup of Glide's ModelLoader. In order to make your life easier, Glide provides the implementation for two network libraries: OkHttp and Volley.
OkHttp 2
Let's assume you want to integrate OkHttp 2 as the network library of your choice for Glide. The integration can be done manually by declaring a GlideModule
. If you want to avoid to do the implementation by hand, just open your build.gradle
and add the following two lines to your dependencies
:
Glide 4.x
// image loading library Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Glide's OkHttp2 Integration
implementation 'com.github.bumptech.glide:okhttp-integration:4.8.0@aar'
implementation 'com.squareup.okhttp:okhttp:2.7.5'
Glide 3.x
// Glide
compile 'com.github.bumptech.glide:glide:3.8.0'
// Glide's OkHttp2 Integration
compile 'com.github.bumptech.glide:okhttp-integration:1.5.0@aar'
compile 'com.squareup.okhttp:okhttp:2.7.5'
Gradle will automatically merge the necessary GlideModule
into your Android.Manifest
. Glide will recognize the existence of it in the manifest and use OkHttp for all network connections.
Volley
On the other hand, if you rather use Volley, you have to change your build.gradle
dependencies to this:
Glide 4.x
// image loading library Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Glide's Volley Integration
implementation 'com.github.bumptech.glide:volley-integration:4.8.0@aar'
implementation 'com.android.volley:volley:1.0.0'
Glide 3.x
// Glide
compile 'com.github.bumptech.glide:glide:3.8.0'
// Glide's Volley Integration
compile 'com.github.bumptech.glide:volley-integration:1.5.0@aar'
compile 'com.mcxiaoke.volley:library:1.0.19'
This will add Volley and the integration library into your project. The integration library adds the GlideModule
to your Android.Manifest
. Glide will automatically recognize it there and use Volley as the networking library. No further configuration is required!
Warning: if you declare both libraries in your build.gradle
, both will get added. Since Glide doesn't load them in any particular order, you'll be an unstable situation, where it's not clear which networking library is picked. Make sure you only add one integration library.
OkHttp 3
If you want to use the new OkHttp 3 as the network stack, integrate it via the provided integration library:
Glide 4.x
// image loading library Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Glide's OkHttp3 Integration
implementation 'com.github.bumptech.glide:okhttp3-integration:4.8.0@aar'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
Glide 3.x
// Glide
compile 'com.github.bumptech.glide:glide:3.8.0'
// Glide's OkHttp3 Integration
compile 'com.github.bumptech.glide:okhttp3-integration:1.5.0@aar'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
Other Networking Libraries
If you're a fan of another networking library, you're out of luck. Glide does not come with an automatic configuration besides Volley, OkHttp2 & OkHttp3. However, feel free to integrate your favorite networking library and open a pull request on GitHub. The implementation for Volley, OkHttp2 and OkHttp3 might give you a direction.
Summary
As you can see, integrating the network libraries is fairly easy, if you're lucky enough to use Gradle as your build system and don't want any further customization. If you don't use Gradle, please take a look here. In the near future, we'll take a look at the GlideModule
for even more advanced customizations. Keep learning!