Home > Blockchain >  Android : How to create a library without conflicting dependencies
Android : How to create a library without conflicting dependencies

Time:10-09

I am creating an android library which will eventually be used my many apps. Assuming this library has a dependency X with version 1.0. What if the main app wants to use the dependency X with version 0.5? I don't want the main app to go thro the process of excluding dependencies thro' gradle. Is there any other way? I am thinking like changing the name or giving an alias to a dependencies in my library? Is this even possible?

CodePudding user response:

If gradle finds two (or more) versions of a dependency in the dependency graph it will choose the latest version by default. Most libraries guarantee backwards compatibility so this usually works fine. Gradle also offers lots of hooks to let the client choose a different strategy.

If you really want consumers of your utility to have two versions of the (shared/transitive) dependency on their classpath you'll need to repackage the dependency so that each class moves to a different package since you can only have one version of each (fully qualified) class name on the classpath

To repackage a jar, take a look at the shadow plugin. Many libraries (eg hibernate and spring) choose to repackage/shadow the ASM jar to avoid conflicts.

Please note that 99% of the time repackaging the jar is the wrong choice and you should just let the normal dependency resolution process take place

See understanding dependency resolution

  • Related