Open3

Android:com.google.firebaseとcom.google.cloud:google-cloud-storageをimplementaionしたときに依存関係が発生した

たくろーどんたくろーどん

直面した問題

下記のようにcom.google.firebasecom.google.cloud:google-cloud-storageをimplementationしてビルドしたところエラーが発生した。

    implementation platform('com.google.firebase:firebase-bom:32.0.0')
    implementation 'com.google.firebase:firebase-firestore'
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-functions'
    implementation 'com.google.cloud:google-cloud-storage:2.41.0'

修正

最終的にこのように修正した。

    implementation ('com.google.cloud:google-cloud-storage:2.41.0'){
        exclude group: 'com.google.protobuf', , module: "protobuf-java"
        exclude group: "com.google.api.grpc", module: "proto-google-common-protos"
    }

調査

ログを一部抜粋すると、下記のように表示があった。
ビルドする際、クラスcom.google.api.Adviceが異なる2つのモジュール(com.google.api.grpc:proto-google-common-protos:2.42.0com.google.firebase:protolite-well-known-types:18.0.0)で重複して定義されているためエラーが発生した。

Duplicate class com.google.api.Advice found in modules jetified-proto-google-common-protos-2.42.0 (com.google.api.grpc:proto-google-common-protos:2.42.0) and jetified-protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
Duplicate class com.google.api.Advice$1 found in modules jetified-proto-google-common-protos-2.42.0 (com.google.api.grpc:proto-google-common-protos:2.42.0) and jetified-protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)

そこで、どちらかのライブラリから、重複しているモジュールを除外する必要がある。

調べていると、stack overflowで同様の問題に直面した質問があったので参考にした。

https://stackoverflow.com/questions/64026660/while-implementing-gcs-duplicate-class-found-error-after-adding-implementation

結果、この問題は下記ようにして解決した。
ライブラリgoogle-cloud-storageが依存するモジュールproto-google-common-protosを除外する。

    implementation ('com.google.cloud:google-cloud-storage:2.41.0'){
        exclude group: "com.google.api.grpc", module: "proto-google-common-protos"
    }

クラス com.google.protobufでも同様のことが発生している。

Duplicate class com.google.protobuf.AbstractMessageLite found in modules jetified-protobuf-java-3.25.3 (com.google.protobuf:protobuf-java:3.25.3) and jetified-protobuf-javalite-3.25.1 (com.google.protobuf:protobuf-javalite:3.25.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules jetified-protobuf-java-3.25.3 (com.google.protobuf:protobuf-java:3.25.3) and jetified-protobuf-javalite-3.25.1 (com.google.protobuf:protobuf-javalite:3.25.1)

そのため、追加でこう記述する

    implementation ('com.google.cloud:google-cloud-storage:2.41.0'){
        exclude group: 'com.google.protobuf', module: "protobuf-java"
        exclude group: "com.google.api.grpc", module: "proto-google-common-protos"
    }

これでgradleをsyncしてbuildしなおすと、エラーが出なくなった。

たくろーどんたくろーどん

ライブラリ内のライブラリを除外する場合

implementation ('com.google.cloud:google-cloud-storage:2.41.0'){
        exclude module: "protobuf-java"
}

ライブラリに含まれるモジュール内のライブラリを除外

    implementation ('com.google.cloud:google-cloud-storage:2.41.0'){
        exclude group: 'com.google.protobuf', module: "protobuf-java"
    }