As you may acknowledge Glide is one of powerful image libraries which was released by Bump Technologies. In our day, most of companies and developers benefit from this neat library already.
However, Glide has been changed a lot with the v4 version. You would check out these changes by clicking here.
Glide offers two types of modules which are AppGlideModule and LibraryGlideModule. Since we bear with AppGlideModule, let’s look at that now.
AppGlideModule
All applications must add a AppGlideModule implementation, even if the Application is not changing any additional settings or implementing any methods in AppGlideModule. The AppGlideModule implementation acts as a signal that allows Glide’s annotation processor to generate a single combined class with with all discovered LibraryGlideModules.
There can be only one AppGlideModule implementation in a given application (having more than one produce errors at compile time). As a result, libraries must never provide a AppGlideModule implementation.
@GlideModule
In order for Glide to properly discover AppGlideModule and LibraryGlideModule implementations, all implementations of both classes must be annotated with the @GlideModule annotation. The annotation will allow Glide’s annotation processor to discover all implementations at compile time.
When we extend our class from AppGlideModule, we can override two methods to have changes. These are applyOptions and registerComponents.
applyOptions(Context, GlideBuilder)
For this example, applications can customize the MemoryCache size in their AppGlideModule with the applyOptions(Context, GlideBuilder) method by configuring MemorySizeCalculator:
registerComponents()
Components are registered using the Registry class in the registerComponents() method of AppGlideModules and LibraryGlideModules.
Let’s create our own SampleAppGlideModule. We will work on AppGlideModule because we won’t develop a library.
Before let’s add below dependencies:
implementation ‘com.github.bumptech.glide:glide:4.5.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.5.0’
As you may know, we specify several features related to images loaded with RequestOptions class hence. This is why we defined a method which returns an instance of RequestOptions class. I won’t mention each of features because I assume you know already. We are now able to apply these changes along with applyOptions method to let it impact on our images.
In registerComponents() method, we can specify timeout durations for fetching images especially from an API. To do so, we have to work with OkHttp integration. Just add below dependency to your build.gradle:
implementation “com.github.bumptech.glide:okhttp3-integration:4.5.0”
With the @GlideModule annotation, you need to build your project to let Glide generate background files in order to use it later. Glide will create GlideApp class after building. This class will perform same things as you do with Glide when parsing images.
Let’s see how we can fetch images both with GlideAppModule and without it.
Before GlideAppModule
After GlideAppModule
As you realize, we just used with, load and into attributes. Other ones are handled by our module library.
That’s all. Here is our output! Both of the activities work in the same way!
You can reach to the repository as follows:
I also used Data Binding and if you wonder about it, you can also check my repository about it.
Best,