androidgradlegradle-kotlin-dsl

exclude a library in dependencies block of apply function in Plugin class


I use build-logic package as Gradle Convention Plugin and version catalog in my android modular project,

In one of my Plugin in build-logic package, as my plugin code is :

class MyConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            pluginManager.apply {
                apply("...")
                apply("...")
            }

            dependencies {
                add("implementation", libs.findLibrary("androidx.hilt.navigation.compose").get())
            }
        }
    }
}

I want to exclude a library in dependencies block of apply function in Plugin class or version catalog file, Is there any way to exclude a library from dependencies of androidx.hilt.navigation.compose here?

like what we can do in KotlinDslGradle of each module with implementation function as following:

implementation(libs.androidx.hilt.navigation.compose) {
        exclude(group = "androidx.navigation", module = "navigation-runtime") 
}

B.T.W, I tried snippet code above in KotlinDslGradle of app module and it didn't work.


Solution

  • You can do the following:

    with(target) {
        val libs = the<VersionCatalogsExtension>().named("libs")
        dependencies {
            addProvider<MinimalExternalModuleDependency, MinimalExternalModuleDependency>(
                "implementation",
                libs.findLibrary("androidx.hilt.navigation.compose").get()
            ) {
                exclude(group = "androidx.navigation", module = "navigation-runtime")
            }
        }
    }