How to isolate the current version of the library in android gradle

Viewed 77

I am developing an SDK that connects as a dependency inside other projects via gradle. My SDK contains other dependencies. How to make sure that the SDK dependencies do not conflict with the dependencies inside the application itself? How can dependencies be isolated so that the application does not even see them?

The problem is that the library starts using a dependency from the app level. And I want to make sure that the library uses only what was written in its own gradle file.

Example, SDK level:

implementation "io.insert-koin:koin-android:3.2.0"

App level:

implementation "io.insert-koin:koin-android:2.0.1"

How to do this?

2 Answers

I've never had such a problem before, but here is some input.

The problem you are experiencing in Gradle terms is touching two topics - dependency resolution and resolution strategy. By default, the resolution strategy is as such - Gradle takes the highest version of the library found among sub-projects and artifacts.

You have several options for the ResolutionStrategy. You can force users of your SDK to use a higher version of the library.

configurations.all { 
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'io.insert-koin' && details.requested.name == 'koin-android' && details.requested.version < '3.2.0') {
            details.useVersion '3.2.0' 
            details.because "Breaking changes within Koin library version $details.requested.version use version 3.2.0 or higher"
        }
    }
}

The idea for the code snippet above is from here.

How to make sure that the SDK dependencies do not conflict with the dependencies inside the application itself?

You do this through ResolutionStrategy

How can dependencies be isolated so that the application does not even see them?

This isn't actually possible to hide library versions from your app do to the way the gradle build system resolves dependencies.

So, the issue here is actually an expected one. Gradle resolves your dependencies and includes only one version in your final APK.

You can help gradle resolve the dependency by including the following block in your app:

configurations.all {
  resolutionStrategy {
    // use either applications, or libraries version
    // your build can't include both
    force 'io.insert-koin:koin-android:3.2.0'
  }
}
Related