coil: Coil 3.0.0 R8 missing classes detected
Describe the bug After switching to Coil 3.0.0 the R8 task fails with the following error:
12:07:37 ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /home/jenkins/agent/workspace/app_android-client_feature_coil3/app/build/outputs/mapping/release/missing_rules.txt.
12:07:37 ERROR: R8: Missing class coil3.PlatformContext (referenced from: coil3.network.ConnectivityChecker coil3.network.okhttp.OkHttpNetworkFetcher$OkHttpNetworkFetcherFactory$6.invoke(coil3.PlatformContext) and 1 other context)
The missing_rules.txt. contains -dontwarn coil3.PlatformContext. So maybe this should just be shipped as a consumer R8/ProGuard rule?
To Reproduce Migrate to Coil 3.0, try to build minified Android app.
Version Coil 3.0.0, AGP 8.7.2
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 42 (28 by maintainers)
@colinrtwhite Just tested with
3.1.0-SNAPSHOT, without-dontwarn coil3.PlatformContextr8 is still crashing with the same error.I’ve created a repository with detailed commits, for my issue with the debug build. Haven’t tried a release build: https://github.com/JanMalch/coil3bug
As @lmiskovic mentioned, it only occurs after adding a
cacheStrategy. I found a fix by simply adding theconnectivityCheckerexplicitly.It seems that even an explicit
connectivityChecker = ::ConnectivityCheckerworks.This isn’t fixed with Kotlin 2.1 and Coil 3.0.4 either.
I don’t know if the below is directly related to
PlatformContext, but it is related to R8 and the title of the issue.Coil 3 (3.0.4) is currently using Java’s ServiceLoader (you can see ServiceLoaderComponentRegistry here) in order to make it easier for developers to not have to add the fetchers and decoders manually by default.
It seems ServiceLoader relies on the presence of a META-INF/services folder (which Coil 3 has added for each dependency). Because of the qualified class name requirement (package name + class name), it is very probable that the classes extending either FetcherServiceLoaderTarget or DecoderServiceLoaderTarget must be kept by ProGuard/R8 (maybe -keepnames), and I have not seen proguard rules being included with the dependencies. The class OkHttpNetworkFetcherServiceLoaderTarget implements FetcherServiceLoaderTarget.
Also to note that the META-INF folder should not be excluded it seems (which was the case for me), as it was a popular solution to do with
exclude "META-INF/*"before to avoid conflict when generating apks. Maybe using merge for META-INF/services should work.Awesome, thanks for checking! Will ship this in
3.0.2in the next day or two.This is working. But we still get the R8 error for release builds:
When I build with
-dontwarn coil3.PlatformContextthe release build runs through and everything seems to work (images are loaded).@JanMalch Thanks for the repro project! I’m able to reproduce the error locally. It seems like there’s some kind issue with the code generated for this function specifically:
Hopefully we can work around the generated code issue. For now folks can use your work around.
I’m also having issues with the
coil3.PlatformContext. In fact, images aren’t loading in a regular debug build. With theDebugLoggerI see the following:The
throwablefor the log call isnull. The app is not a KMP app in any way.@colinrtwhite This is a sample project which reproduces the issue: https://github.com/firefinchdev/coil-minify-issue-demo
Perform a normal assemble will reproduce it.
Even after adding the mentioned service loader targets keep rules, the issue appears.
Hello, I am a beginner in KMP. I heard that coil can display images across platforms, but I have encountered the same problem. It runs well in Android, iOS, and Desktop-jvm (Debug), but Desktop-jvm (Release) cannot display images. My code is just like this:
AsyncImage(model = "https://my_internet_picture.webp", contentDescription = null)When I try to set
compose.desktop { application { buildTypes { release { proguard { isEnabled = false } } } } }it will display normally. I guess it’s caused by R8, but my app doesn’t have a proguard file configured. As a beginner, I don’t know how to configure it.
Then, I provide the error message for the onError that cannot display images in the release version: It seems to be related to ServiceLoader and ktor.
2024.12.19
I found a way to set up the proguard file. I only need
--keep class coil3.** {*;}to ensure that compose desktop runs under release. May I ask what the smallest solution should be@ychescale9 Ah yep there was another report of this on ASG as well unfortunately. They were able to work around the issue by inlining the
Fetcher.Factoryinto their callsite:@colinrtwhite Without the workaround we’re still getting this issue with 3.0.4 🤔
@colinrtwhite I switched to
3.1.0-SNAPSHOTand removed any custom proguard rules and the workaround from https://github.com/coil-kt/coil/issues/2637#issuecomment-2461587997. So we again only setThe release builds does not show any R8 errors anymore and images are loading (so no runtime errors). Looks like your changes fixed the issue! 🎊