feat(coil): Configure Coil-cache to no longer leak memory (#1031)

Configure Coil-cache to no longer leak memory, causing the app to slow down and eventually crash

This configures the memory caching in the way presented by the official documentation at
https://coil-kt.github.io/coil/image_loaders/
This commit is contained in:
Luca Auer 2025-12-09 10:29:30 +01:00 committed by GitHub
parent 51c1f95791
commit 617c8788d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -20,6 +20,9 @@ import androidx.work.Configuration
import androidx.work.WorkManager import androidx.work.WorkManager
import coil3.ImageLoader import coil3.ImageLoader
import coil3.SingletonImageLoader import coil3.SingletonImageLoader
import coil3.disk.DiskCache
import coil3.disk.directory
import coil3.memory.MemoryCache
import coil3.network.okhttp.OkHttpNetworkFetcherFactory import coil3.network.okhttp.OkHttpNetworkFetcherFactory
import coil3.request.allowRgb565 import coil3.request.allowRgb565
import coil3.request.crossfade import coil3.request.crossfade
@ -266,6 +269,19 @@ class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.Factor
// SY <-- // SY <--
} }
memoryCache(
MemoryCache.Builder()
.maxSizePercent(context, 0.25)
.build(),
)
diskCache(
DiskCache.Builder()
.directory(context.cacheDir.resolve("image_cache"))
.maxSizePercent(0.02)
.build(),
)
crossfade((300 * this@App.animatorDurationScale).toInt()) crossfade((300 * this@App.animatorDurationScale).toInt())
allowRgb565(DeviceUtil.isLowRamDevice(this@App)) allowRgb565(DeviceUtil.isLowRamDevice(this@App))
if (networkPreferences.verboseLogging().get()) logger(DebugLogger()) if (networkPreferences.verboseLogging().get()) logger(DebugLogger())