Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I preserve older versions of library used by a dependency?

I have an Android project where Glide v4 is one of its dependency.

This project has another dependency, let's call it dependency A , where it depends on Glide v3 instead.
I don't know if it matters, but dependency A can only be included as an aar.

So this is part of my build.gradle:

implementation(name: 'dependency_a', ext: 'aar')
implementation ("com.github.bumptech.glide:glide:4.7.1") {
    exclude group: "com.android.support"
}
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

The app can be compiled; but when code in dependency A runs that uses Glide v3:

Glide.with(context).load(imageUrl).asBitmap().into(new SimpleTarget<Bitmap>() {...}

The app crashes with this message:

java.lang.NoSuchMethodError: No virtual method load(Ljava/lang/String;)Lcom/bumptech/glide/DrawableTypeRequest; in class Lcom/bumptech/glide/RequestManager; or its super classes (declaration of 'com.bumptech.glide.RequestManager' appears in /data/app/{my.package.name}}-LItMzBkBqXw3lyYYdKp-SA==/base.apk:classes15.dex)

I am finding a way to preserve Glide v3 in dependency A, but still use Glide v4 for my app and other dependencies.

Is it even possible?

Why don't I simply use Glide v3 for my app as well

This is because another dependency B needs me to use Glide v4.

like image 542
Sira Lam Avatar asked Jul 17 '18 06:07

Sira Lam


2 Answers

Gradle's dependency resolution is about choosing one version when multiple alternatives are available or required.

Ultimately there can only be one Class for a given class full name in a given ClassLoader, so the possibilities are :

  • Change the package name. For example Spongy Castle moved from org.bouncycastle.* to org.spongycastle.* to avoid conflicts with the platform's version.

  • Use multiple class loaders. I believe that Android support custom class loader, but this would probably involve quite a bit of work with subtle pitfalls.

I think that unfortunately, none of this is a practical solution in your case.

like image 188
bwt Avatar answered Oct 04 '22 06:10

bwt


Gradle resolves version conflicts by picking the highest version of a module as mentioned here. So if you have v3 and v4 as your depencency, v4 will be used.

You are getting the crash since there are major changes from v3 to v4 for Glide, Dependency A cannot use methods of v4.

Solution 1 - Dependency B has to use v3 to avoid conflicts. Upgrade to v4 when Dependency A has upgraded to v4.

Solution 2 - If Dependency A can function normally without Glide dependency then, Glide can be excluded from Dependency A.

like image 31
Kedar Tendolkar Avatar answered Oct 04 '22 06:10

Kedar Tendolkar