How can I solve "Type com.google.protobuf.Any$1 is defined multiple times" problem on Android Studio

huangapple go评论92阅读模式
英文:

How can I solve "Type com.google.protobuf.Any$1 is defined multiple times" problem on Android Studio

问题

I am upgrading my Android app to support API 33.
For that, I also upgraded protobuf & grpc libraries.
Everything is upgraded and the app works if I build & run it directly from Android Studio to my phone.
However, I get the following error if I try to build a bundle for publishing the app.

Type com.google.protobuf.Any$1 is defined multiple times: C:\Users\ilker\.gradle\caches\transforms-3\156cd40978d20d5791c06b9e4b80c4fc\transformed\jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class, C:\Development\workspace\android_studio\VectorCTRLPRO\app\build\intermediates\javac\release\classes\com\google/protobuf/Any$1.class My build.gradle (app level) is like this :

apply plugin: 'com.google.protobuf'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 125
        versionName "12.5"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
            firebaseCrashlytics {
                mappingFileUploadEnabled true
            }
        }
    }
    lintOptions {
        disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
        textReport true
        textOutput "stdout"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.7.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'io.grpc:grpc-okhttp:1.52.1'
    implementation 'io.grpc:grpc-protobuf-lite:1.52.1'
    implementation 'io.grpc:grpc-stub:1.52.1'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.23.2'
    protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
    implementation 'com.google.android.gms:play-services-ads:22.1.0'
    implementation 'com.google.firebase:firebase-analytics:21.3.0'
    implementation 'com.google.firebase:firebase-crashlytics:18.3.7'
    implementation 'com.google.firebase:firebase-messaging:23.1.2'
    implementation ('com.google.firebase:firebase-inappmessaging-display:20.3.2') {
        exclude group: 'com.google.firebase', module: 'protolite-well-known-types'
    }
    implementation 'org.jcodec:jcodec:0.2.5'
    implementation 'org.jcodec:jcodec-android:0.2.5'
    implementation 'org.jcodec:jcodec-javase:0.2.5'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation 'com.google.android.ads.consent:consent-library:1.0.8'
    implementation 'com.google.android.play:core:1.10.3'
    implementation 'com.google.guava:guava:28.2-android'
    implementation project(path: ':nativetemplates')

    constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
            because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
        }
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
        }
    }
}

configurations.implementation {
    exclude group: 'com.google.guava', module: 'listenablefuture'
}


protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.23.2' }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.52.1' }
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins{
                java { option 'lite' }
            }
            task.plugins {
                grpc { option 'lite' }
            }
        }
    }
} ``` Tried removing following lines but I got the same error:

``` protobuf 'com.google.protobuf:protobuf-java:3.23.2'
protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3' ``` Dependency list created by gradle is as below:

[https://pastebin.com/raw/PdYSg8bm][1]

[1]: https://pastebin.com/raw/PdYSg8bm

<details>
<summary>英文:</summary>

I am upgrading my Android app to support API 33.
For that, I also upgraded protobuf &amp; grpc libraries.
Everything is upgraded and the app works if I build &amp; run it directly from Android Studio to my phone.
However, I get the following error if I try to build a bundle for publishing the app.

Type com.google.protobuf.Any$1 is defined multiple times: C:\Users\ilker.gradle\caches\transforms-3\156cd40978d20d5791c06b9e4b80c4fc\transformed\jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class, C:\Development\workspace\android_studio\VectorCTRLPRO\app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class


My build.gradle (app level) is like this :

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
compileSdkVersion 33
defaultConfig {
applicationId "com.test.app"
minSdkVersion 21
targetSdkVersion 33
versionCode 125
versionName "12.5"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
firebaseCrashlytics {
mappingFileUploadEnabled true
}
}
}
lintOptions {
disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
textReport true
textOutput "stdout"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/io.netty.versions.properties'
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.8.0'
implementation 'io.grpc:grpc-okhttp:1.52.1'
implementation 'io.grpc:grpc-protobuf-lite:1.52.1'
implementation 'io.grpc:grpc-stub:1.52.1'
implementation 'javax.annotation:javax.annotation-api:1.2'
protobuf 'com.google.protobuf:protobuf-java:3.23.2'
protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
implementation 'com.google.android.gms:play-services-ads:22.1.0'
implementation 'com.google.firebase:firebase-analytics:21.3.0'
implementation 'com.google.firebase:firebase-crashlytics:18.3.7'
implementation 'com.google.firebase:firebase-messaging:23.1.2'
implementation ('com.google.firebase:firebase-inappmessaging-display:20.3.2') {
exclude group: 'com.google.firebase', module: 'protolite-well-known-types'
}
implementation 'org.jcodec:jcodec:0.2.5'
implementation 'org.jcodec:jcodec-android:0.2.5'
implementation 'org.jcodec:jcodec-javase:0.2.5'
implementation 'com.google.android.gms:play-services-vision:20.1.3'
implementation 'com.google.android.ads.consent:consent-library:1.0.8'
implementation 'com.google.android.play:core:1.10.3'
implementation 'com.google.guava:guava:28.2-android'
implementation project(path: ':nativetemplates')

constraints {
    implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0&quot;) {
        because(&quot;kotlin-stdlib-jdk7 is now a part of kotlin-stdlib&quot;)
    }
    implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0&quot;) {
        because(&quot;kotlin-stdlib-jdk8 is now a part of kotlin-stdlib&quot;)
    }
}

}

configurations.implementation {
exclude group: 'com.google.guava', module: 'listenablefuture'
}

protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.23.2' }
plugins {
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.52.1' }
}

generateProtoTasks {
    all().each { task -&gt;
        task.builtins{
            java { option &#39;lite&#39; }
        }
        task.plugins {
            grpc { option &#39;lite&#39; }
        }
    }
}

}




tried removing following lines but I got the same error:

   

protobuf 'com.google.protobuf:protobuf-java:3.23.2'
protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'


Dependency list created by gradle is as below:

[https://pastebin.com/raw/PdYSg8bm][1]


  [1]: https://pastebin.com/raw/PdYSg8bm

</details>


# 答案1
**得分**: 0

以下是翻译好的部分:

似乎您依赖于`io.grpc:grpc-protobuf-lite`来使用`com.google.protobuf:protobuf-java`和`com.google.protobuf:protobuf-javalite`。

错误消息中提到的相关类是:
`jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class`和`app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class`。

请参考https://developer.android.com/build/dependencies#view-dependency-tree 获取更多关于如何处理此问题的信息。您需要调查是什么导致了多个不同的protobuf副本,然后找出如何调整您的依赖关系图。

<details>
<summary>英文:</summary>

It seems like you are depending on `com.google.protobuf:protobuf-java` and `com.google.protobuf:protobuf-javalite` via `io.grpc:grpc-protobuf-lite`.

The hint was in the error message where the relevant classes were:
`jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class` and `app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class`.

Refer to https://developer.android.com/build/dependencies#view-dependency-tree for more info on what to do about this. You&#39;ll need to investigate to see what is pulling in multiple different protobuf copies and then figure out how to iron out your dependency graph.

</details>



# 答案2
**得分**: 0

@ejona86 来自 Github 在这个问题上帮助了我:
https://github.com/google/protobuf-gradle-plugin/issues/731

问题出在旧版本的 "googleapis-common-protos:0.0.3" 组件上。我将它更新到了版本 2.23.0,但仍然存在问题。

然后有人建议我手动注入 descriptor.proto 文件:
1.

     //protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
        protobuf ('com.google.api.grpc:proto-google-common-protos:2.23.0') {
            exclude group: 'com.google.protobuf'
        }

2. 将文件添加到源码中:
app/src/main/proto/google/protobuf/descriptor.proto

经过这些修改,我的问题解决了。
感谢 @ejona86。

<details>
<summary>英文:</summary>

@ejona86 from Github helped me with this issue here:
https://github.com/google/protobuf-gradle-plugin/issues/731

It turns out that the problem is with the old &quot;googleapis-common-protos:0.0.3&quot; artifact. I updated it to version 2.23.0 but it still has issues.

Then I was advised to manually inject descriptor.proto doing;
1.
   

     //protobuf &#39;com.google.api.grpc:googleapis-common-protos:0.0.3&#39;
        protobuf (&#39;com.google.api.grpc:proto-google-common-protos:2.23.0&#39;) {
            exclude group: &#39;com.google.protobuf&#39;
        }

2. add the file to the source:
app/src/main/proto/google/protobuf/descriptor.proto

After these changes, my issue is gone.
Thanks to @ejona86



</details>



huangapple
  • 本文由 发表于 2023年6月15日 03:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定