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?

Gradle の Version Catalogs を使っているプロジェクトをローカル参照する

Last updated at Posted at 2025-04-22

実行環境

  • gradle-8.11.1

Android アプリ開発で Gradle の Version Catalogs を使用していると、同じく Version Catalog を使用している Android のプロジェクトをローカルで参照したときにエラーが発生します。

e: file:///Users/[user.name]/AndroidStudioProjects/[application]/build.gradle.kts:3:24: Unresolved reference: [library.name]
e: file:///Users/[user.name]/AndroidStudioProjects/[application]/build.gradle.kts:7:24: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 

これは例えば、アプリを開発しているプロジェクトから、別のプロジェクトで開発されているライブラリをローカル参照したときに発生します。

対応策

対応策として、プロジェクトごとに Version Catalog の参照名を変更します。

まず、参照元のプロジェクトでは以下の資料を参考に libs.versions.toml ファイルで依存バージョンを管理しているとします。

この場合、参照先のプロジェクトでも libs.versions.toml ファイルで依存バージョンを管理しようとすると、ローカル参照したときに参照名が競合して上記のエラーが発生します。

そこで、toml ファイルを任意の名前(ここでは sample.versions.toml とします)に変更し、以下の変更を追加します。

settings.gradle.kts
dependencyResolutionManagement {
    versionCatalogs {
        // 参照名を toml ファイルの名称と同じにする必要は無いが、同じであるほうがわかりやすい。
        create("sample") {
            from(files("gradle/sample.versions.toml"))
        }
    }
}
app/build.gradle.kts
plugins {
    // 参照名をすべて libs.~ から sample.~ に変更する
    alias(sample.plugins.android.application)
    ...
}

これで Version Catalog の参照名が競合することは無くなりました。

ただし、sample.versions.toml でバージョン管理しているプロジェクトをローカル参照する場合は、以下の設定を「参照元」のプロジェクトに追加する必要があります。

settings.gradle.kts
dependencyResolutionManagement {
    versionCatalogs {
        create("sample") {
            from(files("../[参照するプロジェクト名]/gradle/sample.versions.toml"))
        }
    }
}
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?