0
# Modules and Configuration
1
2
Global configuration and extension system for customizing Glide behavior, including custom loaders, decoders, cache implementations, and module registration.
3
4
## Capabilities
5
6
### Glide Modules
7
8
Base interfaces for creating configuration modules that customize Glide's behavior.
9
10
```java { .api }
11
/**
12
* Legacy module interface for global Glide configuration
13
*/
14
public interface GlideModule {
15
/**
16
* Apply global configuration options
17
* @param context Application context
18
* @param builder Glide builder for configuration
19
*/
20
void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder);
21
22
/**
23
* Register custom components
24
* @param context Application context
25
* @param glide Glide instance
26
* @param registry Component registry
27
*/
28
void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry);
29
}
30
31
/**
32
* Application-level module for configuration
33
*/
34
public abstract class AppGlideModule extends GeneratedAppGlideModule {
35
/**
36
* Apply global configuration options
37
* @param context Application context
38
* @param builder Glide builder for configuration
39
*/
40
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
41
// Override to customize configuration
42
}
43
44
/**
45
* Register custom components
46
* @param context Application context
47
* @param glide Glide instance
48
* @param registry Component registry
49
*/
50
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
51
// Override to register custom components
52
}
53
54
/**
55
* Control manifest parsing
56
* @return true to enable parsing AndroidManifest.xml for modules
57
*/
58
public boolean isManifestParsingEnabled() {
59
return true;
60
}
61
62
/**
63
* Get excluded module classes
64
* @return Set of module classes to exclude
65
*/
66
@NonNull
67
public Set<Class<?>> getExcludedModuleClasses() {
68
return Collections.emptySet();
69
}
70
}
71
72
/**
73
* Library-level module for component registration
74
*/
75
public abstract class LibraryGlideModule implements RegistersComponents {
76
/**
77
* Register custom components
78
* @param context Application context
79
* @param glide Glide instance
80
* @param registry Component registry
81
*/
82
@Override
83
public abstract void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry);
84
}
85
```
86
87
**Usage Examples:**
88
89
```java
90
// AppGlideModule implementation
91
@GlideModule
92
public final class MyAppGlideModule extends AppGlideModule {
93
@Override
94
public void applyOptions(Context context, GlideBuilder builder) {
95
// Custom memory cache
96
int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb
97
builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
98
99
// Custom disk cache
100
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, 100 * 1024 * 1024));
101
102
// Request options
103
RequestOptions requestOptions = new RequestOptions()
104
.format(DecodeFormat.PREFER_RGB_565)
105
.disallowHardwareConfig();
106
builder.setDefaultRequestOptions(requestOptions);
107
}
108
109
@Override
110
public void registerComponents(Context context, Glide glide, Registry registry) {
111
// Custom model loader
112
registry.append(MyCustomModel.class, InputStream.class, new MyModelLoaderFactory());
113
114
// Custom decoder
115
registry.register(InputStream.class, MyResource.class, new MyResourceDecoder());
116
}
117
}
118
119
// LibraryGlideModule implementation
120
public class MyLibraryGlideModule extends LibraryGlideModule {
121
@Override
122
public void registerComponents(Context context, Glide glide, Registry registry) {
123
// Register library-specific components
124
registry.prepend(MyLibraryModel.class, InputStream.class, new MyLibraryModelLoader.Factory());
125
}
126
}
127
```
128
129
### Glide Builder
130
131
Configuration builder for customizing Glide's global behavior and components.
132
133
```java { .api }
134
/**
135
* Builder for configuring Glide instances
136
*/
137
public final class GlideBuilder {
138
/**
139
* Set custom bitmap pool
140
* @param bitmapPool Custom bitmap pool implementation
141
* @return GlideBuilder for chaining
142
*/
143
@NonNull public GlideBuilder setBitmapPool(@Nullable BitmapPool bitmapPool);
144
145
/**
146
* Set custom array pool
147
* @param arrayPool Custom array pool implementation
148
* @return GlideBuilder for chaining
149
*/
150
@NonNull public GlideBuilder setArrayPool(@Nullable ArrayPool arrayPool);
151
152
/**
153
* Set custom memory cache
154
* @param memoryCache Custom memory cache implementation
155
* @return GlideBuilder for chaining
156
*/
157
@NonNull public GlideBuilder setMemoryCache(@Nullable MemoryCache memoryCache);
158
159
/**
160
* Set custom disk cache factory
161
* @param diskCacheFactory Custom disk cache factory
162
* @return GlideBuilder for chaining
163
*/
164
@NonNull public GlideBuilder setDiskCache(@Nullable DiskCache.Factory diskCacheFactory);
165
166
/**
167
* Set custom execution engine
168
* @param engine Custom engine implementation
169
* @return GlideBuilder for chaining
170
*/
171
@NonNull public GlideBuilder setEngine(@Nullable Engine engine);
172
173
/**
174
* Set default request options
175
* @param requestOptions Default options for all requests
176
* @return GlideBuilder for chaining
177
*/
178
@NonNull public GlideBuilder setDefaultRequestOptions(@Nullable RequestOptions requestOptions);
179
180
/**
181
* Set default transition options for specific resource type
182
* @param clazz Resource class
183
* @param options Transition options
184
* @return GlideBuilder for chaining
185
*/
186
@NonNull public <T> GlideBuilder setDefaultTransitionOptions(@NonNull Class<T> clazz, @Nullable TransitionOptions<?, T> options);
187
188
/**
189
* Set logging level
190
* @param logLevel Android log level (Log.VERBOSE, Log.DEBUG, etc.)
191
* @return GlideBuilder for chaining
192
*/
193
@NonNull public GlideBuilder setLogLevel(int logLevel);
194
195
/**
196
* Set whether to log request origins for debugging
197
* @param isEnabled true to enable request origin logging
198
* @return GlideBuilder for chaining
199
*/
200
@NonNull public GlideBuilder setLogRequestOrigins(boolean isEnabled);
201
202
/**
203
* Set image header parser registry
204
* @param imageHeaderParsers Custom header parsers
205
* @return GlideBuilder for chaining
206
*/
207
@NonNull public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled);
208
}
209
```
210
211
**Configuration Examples:**
212
213
```java
214
@Override
215
public void applyOptions(Context context, GlideBuilder builder) {
216
// Memory configuration
217
int memoryCacheSizeBytes = (int) (Runtime.getRuntime().maxMemory() / 8);
218
builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
219
220
// Disk cache configuration
221
long diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB
222
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
223
224
// Bitmap pool configuration
225
int bitmapPoolSizeBytes = memoryCacheSizeBytes / 2;
226
builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
227
228
// Default request options
229
RequestOptions requestOptions = new RequestOptions()
230
.format(DecodeFormat.PREFER_RGB_565)
231
.timeout(30000);
232
builder.setDefaultRequestOptions(requestOptions);
233
234
// Debugging
235
builder.setLogLevel(Log.DEBUG)
236
.setLogRequestOrigins(true);
237
}
238
```
239
240
### Registry System
241
242
Component registry for registering custom loaders, decoders, encoders, and transformations.
243
244
```java { .api }
245
/**
246
* Registry for Glide components
247
*/
248
public class Registry {
249
/**
250
* Append model loader factory
251
* @param modelClass Model class
252
* @param dataClass Data class
253
* @param factory Loader factory
254
* @return Registry for chaining
255
*/
256
@NonNull public <Model, Data> Registry append(@NonNull Class<Model> modelClass, @NonNull Class<Data> dataClass, @NonNull ModelLoaderFactory<Model, Data> factory);
257
258
/**
259
* Prepend model loader factory
260
* @param modelClass Model class
261
* @param dataClass Data class
262
* @param factory Loader factory
263
* @return Registry for chaining
264
*/
265
@NonNull public <Model, Data> Registry prepend(@NonNull Class<Model> modelClass, @NonNull Class<Data> dataClass, @NonNull ModelLoaderFactory<Model, Data> factory);
266
267
/**
268
* Replace model loader factory
269
* @param modelClass Model class
270
* @param dataClass Data class
271
* @param factory Loader factory
272
* @return Registry for chaining
273
*/
274
@NonNull public <Model, Data> Registry replace(@NonNull Class<Model> modelClass, @NonNull Class<Data> dataClass, @NonNull ModelLoaderFactory<Model, Data> factory);
275
276
/**
277
* Register resource decoder
278
* @param dataClass Input data class
279
* @param resourceClass Output resource class
280
* @param decoder Resource decoder
281
* @return Registry for chaining
282
*/
283
@NonNull public <Data, TResource> Registry register(@NonNull Class<Data> dataClass, @NonNull Class<TResource> resourceClass, @NonNull ResourceDecoder<Data, TResource> decoder);
284
285
/**
286
* Register resource encoder
287
* @param resourceClass Resource class
288
* @param encoder Resource encoder
289
* @return Registry for chaining
290
*/
291
@NonNull public <TResource> Registry register(@NonNull Class<TResource> resourceClass, @NonNull ResourceEncoder<TResource> encoder);
292
293
/**
294
* Register transformation
295
* @param resourceClass Resource class
296
* @param transformation Transformation implementation
297
* @return Registry for chaining
298
*/
299
@NonNull public <TResource> Registry register(@NonNull Class<TResource> resourceClass, @NonNull Transformation<TResource> transformation);
300
}
301
```
302
303
**Registry Examples:**
304
305
```java
306
@Override
307
public void registerComponents(Context context, Glide glide, Registry registry) {
308
// Custom model loader for special URL format
309
registry.append(CustomUrl.class, InputStream.class, new CustomUrlLoader.Factory());
310
311
// Custom decoder for special image format
312
registry.register(InputStream.class, SpecialImage.class, new SpecialImageDecoder());
313
314
// Custom encoder for special format
315
registry.register(SpecialImage.class, new SpecialImageEncoder());
316
317
// Custom transformation
318
registry.register(Bitmap.class, new BlurTransformation());
319
320
// Replace existing loader
321
registry.replace(GlideUrl.class, InputStream.class, new MyHttpUrlLoader.Factory());
322
}
323
```
324
325
### Custom Components
326
327
Interfaces for creating custom loaders, decoders, and encoders.
328
329
```java { .api }
330
/**
331
* Factory for creating model loaders
332
*/
333
public interface ModelLoaderFactory<T, Y> {
334
/**
335
* Build model loader instance
336
* @param multiFactory Multi model loader factory
337
* @return Model loader instance
338
*/
339
@NonNull ModelLoader<T, Y> build(@NonNull MultiModelLoaderFactory multiFactory);
340
341
/**
342
* Cleanup resources
343
*/
344
void teardown();
345
}
346
347
/**
348
* Decoder for converting data to resources
349
*/
350
public interface ResourceDecoder<T, Z> {
351
/**
352
* Check if decoder can handle the data
353
* @param source Data source
354
* @param options Request options
355
* @return true if can decode
356
*/
357
boolean handles(@NonNull T source, @NonNull Options options) throws IOException;
358
359
/**
360
* Decode data to resource
361
* @param source Data source
362
* @param width Target width
363
* @param height Target height
364
* @param options Request options
365
* @return Decoded resource
366
*/
367
@Nullable Resource<Z> decode(@NonNull T source, int width, int height, @NonNull Options options) throws IOException;
368
}
369
370
/**
371
* Encoder for persisting resources to disk
372
*/
373
public interface ResourceEncoder<T> {
374
/**
375
* Encode resource to file
376
* @param data Resource to encode
377
* @param file Output file
378
* @param options Request options
379
* @return true if successful
380
*/
381
boolean encode(@NonNull Resource<T> data, @NonNull File file, @NonNull Options options);
382
383
/**
384
* Get encoding strategy
385
* @param options Request options
386
* @return Encoding strategy
387
*/
388
@NonNull EncodeStrategy getEncodeStrategy(@NonNull Options options);
389
}
390
```
391
392
## Types
393
394
```java { .api }
395
// Component interfaces
396
public interface RegistersComponents {
397
void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry);
398
}
399
400
// Encoding strategy
401
public enum EncodeStrategy {
402
NONE, SOURCE, TRANSFORMED
403
}
404
405
// Generated module base class
406
public abstract class GeneratedAppGlideModule {
407
// Generated by annotation processor
408
}
409
```