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)

Most upvoted comments

@colinrtwhite Just tested with 3.1.0-SNAPSHOT, without -dontwarn coil3.PlatformContext r8 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 the connectivityChecker explicitly.

connectivityChecker = { ctx -> ConnectivityChecker(ctx) }

It seems that even an explicit connectivityChecker = ::ConnectivityChecker works.

I’m hoping Kotlin 2.1 might fix this issue. @yschimke if you use the latest snapshot (which builds with Kotlin 2.1) does it fix the issue?

This isn’t fixed with Kotlin 2.1 and Coil 3.0.4 either.

ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /runner/_work/path/to/project/app/build/outputs/mapping/release/missing_rules.txt.

> Task :app:minifyReleaseWithR8 FAILED
ERROR: R8: Missing class coil3.PlatformContext (referenced from: coil3.network.ConnectivityChecker coil3.network.okhttp.OkHttpNetworkFetcher$OkHttpNetworkFetcherFactory$9.invoke(coil3.PlatformContext) and 1 other context)

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.2 in the next day or two.

I found a fix by simply adding the connectivityChecker explicitly.

This is working. But we still get the R8 error for release builds:

09:59:42  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.
09:59:42  ERROR: R8: Missing class coil3.PlatformContext (referenced from: coil3.network.ConnectivityChecker coil3.network.okhttp.OkHttpNetworkFetcher$OkHttpNetworkFetcherFactory$6.invoke(coil3.PlatformContext) and 1 other context)

When I build with -dontwarn coil3.PlatformContext the 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:

fun OkHttpNetworkFetcherFactory(
    callFactory: () -> Call.Factory = ::OkHttpClient,
    cacheStrategy: () -> CacheStrategy = { CacheStrategy.DEFAULT },
    connectivityChecker: (PlatformContext) -> ConnectivityChecker = ::ConnectivityChecker,
)

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 the DebugLogger I see the following:

🚨 Failed - https://example.com/redacted.jpeg - java.lang.NoClassDefFoundError: Failed resolution of: Lcoil3/PlatformContext;

The throwable for the log call is null. 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.

./gradlew assembleRelease

Even after adding the mentioned service loader targets keep rules, the issue appears.

➜  git:(main) ./gradlew assembleRelease                                                                                                      

> Task :app:minifyReleaseWithR8 FAILED
ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /Users/gupta.anirudhzomato.com/android-studio-projects/MyApplication9/app/build/outputs/mapping/release/missing_rules.txt.
ERROR: R8: Missing class coil3.PlatformContext (referenced from: coil3.network.ConnectivityChecker coil3.network.okhttp.OkHttpNetworkFetcher$OkHttpNetworkFetcherFactory$9.invoke(coil3.PlatformContext) and 1 other context)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:minifyReleaseWithR8'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.R8Task$R8Runnable
   > Compilation failed to complete

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 4s
43 actionable tasks: 5 executed, 38 up-to-date

R8 has a built in optimization that automatically rewrites the service loader implementation to call the class directly. Info about it is linked from the service loader JVM implementation. As a result Coil does not need to ship extra R8 rules in its artifact. To be clear, you do not need to add -keep rules for any of Coil’s classes.

For exclude META-INF/* I don’t think there’s anything Coil can do to guard against this. Adding that line will break all service loader implementations including kotlinx.coroutine’s MainDispatcher and Ktor’s engines. If this is an issue, you can declare your dependencies manually using ImageLoader.Builder.components {}. @colinrtwhite

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.Factory into their callsite:

add(
  Fetcher.Factory<Uri> { data, options, imageLoader ->
    if (data.scheme == "http" || data.scheme == "https") {
      NetworkFetcher(
        url = data.toString(),
        options = options,
        networkClient = networkClient,
        diskCache = lazy { imageLoader.diskCache },
        cacheStrategy = CacheStrategy.DEFAULT,
        connectivityChecker = ConnectivityChecker.ONLINE,
      )
    } else {
      null
    }
  }
)

@colinrtwhite Without the workaround we’re still getting this issue with 3.0.4 🤔

add(OkHttpNetworkFetcherFactory( { OkHttpClient.Builder().build() }))

@colinrtwhite I switched to 3.1.0-SNAPSHOT and removed any custom proguard rules and the workaround from https://github.com/coil-kt/coil/issues/2637#issuecomment-2461587997. So we again only set

add(OkHttpNetworkFetcherFactory(cacheStrategy = { CacheControlCacheStrategy() }))

The release builds does not show any R8 errors anymore and images are loading (so no runtime errors). Looks like your changes fixed the issue! 🎊