Search code examples
androidandroid-libraryandroid-glideandroid-module

Android App and Library that both use Glide - appPackage1.GlideRequests cannot be cast to appPackage2.GlideRequests


I'm hoping someone has run into this before and its just an easy setting that i'm missing. Basically i've built a Kotlin Library that uses Glide, and uses dagger to set up my glide instance in that library. This library by itself, and in a dummy app works perfect, no issues.

I have an old Java project that i'm testing out using this library in. Everything seems ok except when I hit the screen with the Glide functionality. Glide is being used in both the App and the Library and they seem to be colliding.

This is the error:

java.lang.ClassCastException: appPackage1.Utilities.GlideRequests cannot be cast to appPackage2.di.module.GlideRequests
        at appPackage2.di.module.GlideApp.with(GlideApp.java:88)

In the old Java Application Glide exists as:

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}

In my Kotlin Library Glide exists as:

@GlideModule
class AppGlideModule : AppGlideModule()

And it throws the error when it is called like below from my library:

GlideApp.with(requireContext()).load

Solution

  • Looks like I needed to dig into the documentation a little closer.

    http://bumptech.github.io/glide/doc/configuration.html#avoid-appglidemodule-in-libraries

    Avoid AppGlideModule in libraries

    Libraries must not include AppGlideModule implementations. Doing so will prevent any applications that depend on the library from managing their dependencies or configuring options like Glide’s cache sizes and locations.

    In addition, if two libraries include AppGlideModules, applications will be unable to compile if they depend on both and will be forced to pick one or other other.

    This does mean that libraries won’t be able to use Glide’s generated API, but loads with RequestOptions will still work just fine (see the options page for examples).

    Also a relevant Github Issue on it: https://github.com/bumptech/glide/issues/2393

    Hopefully if anyone else runs into this, this answer will help.