1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

使用しているライブラリの依存関係に問題があり、excludeの指定が必要にある場合があります。

build.gradle.kts
implementation("com.example:foo:1.2.3") {
    exclude(group = "com.example", module = "bar")
}

しかし、gradle version catalogを使っている場合、excludeの指定もgradle version catalogの値を使いたいなと思って調べました。

まあ、当然、以下のような指定はそのままではできません。

build.gradle.kts
implementation(libs.foo) {
    exclude(libs.bar)
}

gradle version catalogで定義した、libs.foo の型は Provider<MinimalExternalModuleDependency> となっていて、ライブラリの情報へアクセスするには、Providerのget() メソッドを使って MinimalExternalModuleDependency を取得し、MinimalExternalModuleDependencyのメソッドを呼び出します。
要するに以下のように記述することで groupmodule の値を指定することができます。

build.gradle.kts
implementation(libs.foo) {
    exclude(group = libs.bar.get().group, module = libs.bar.get().name)
}

ただ、すこし冗長なので、let を使って以下などどうでしょう?

build.gradle.kts
implementation(libs.foo) {
    libs.bar.get().let { exclude(it.group, it.name) }
}

もしくは、以下のような拡張関数を定義しておけば

build.gradle.kts
fun <T : ModuleDependency> T.exclude(dependency: Provider<MinimalExternalModuleDependency>): T =
    dependency.get().let { exclude(it.group, it.name) }

以下のような記述が可能です。

build.gradle.kts
implementation(libs.foo) {
    exclude(libs.bar)
}

gradle version catalogなのにバージョン情報使ってないやんというツッコミはなしでお願いします。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?