如何解决谷歌API建议中的“程序类型已存在”问题?

5

我正在开发一款Android语音识别应用程序。使用了Google Cloud Speech API和Firebase Cloudstore(作为数据库)。

当我构建我的项目时,出现以下错误::

Error: Program type already present: com.google.api.Advice$1

我不知道如何解决这个问题。

我的步骤
我在stackoverflow上搜索了类似的问题,但没有特别针对我的问题。

也就是说,我不知道需要排除哪些包来解决依赖冲突。

请帮助我。

我的Gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.google.cloud.examples.speechrecognition"
        minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations {
        compile.exclude group: 'com.google.protobuf', module: 'protobuf-lite'

    }
    // exclude files that are not needed from the cloud client libraries
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/INDEX.LIST'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
// https://mvnrepository.com/artifact/io.grpc/grpc-grpclb
    compile group: 'io.grpc', name: 'grpc-grpclb', version: '1.19.0'

    // https://mvnrepository.com/artifact/io.grpc/grpc-auth
    compile group: 'io.grpc', name: 'grpc-auth', version: '1.19.0'

    // https://mvnrepository.com/artifact/io.grpc/grpc-alts
    compile group: 'io.grpc', name: 'grpc-alts', version: '1.19.0'
// https://mvnrepository.com/artifact/io.grpc/grpc-android
    compile group: 'io.grpc', name: 'grpc-android', version: '1.19.0'

    // add these dependencies for the speech client
    implementation 'io.grpc:grpc-okhttp:1.19.0'
    implementation 'com.google.cloud:google-cloud-speech:0.83.0-beta'

    implementation 'com.google.firebase:firebase-firestore:18.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

task copySecretKey(type: Copy) {
    File secretKey = file "$System.env.GOOGLE_APPLICATION_CREDENTIALS"
    from secretKey.getParent()
    include secretKey.getName()
    into 'src/main/res/raw'
    rename secretKey.getName(), "credential.json"
}
preBuild.dependsOn(copySecretKey)

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
1个回答

1

我遇到了同样的错误,以下是我如何解决问题的方法:
1)将Google服务和Firebase服务更新到最新版本。
2)如果你收到一个新的Gradle冲突错误,像这样:
Error: Program type already present: com.google.type.PostalAddressOrBuilder 那么就要把它排除掉。我通过输入 com.google.type.PostalAddressOrBuilder 在谷歌上进行了快速搜索,并找出要排除的库。以下是我的最终代码。

implementation  ('com.google.cloud:google-cloud-translate:1.21.0'){
        exclude group: 'io.grpc', module: 'grpc-all'
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
        exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
        exclude group: 'com.google.api.grpc', module: 'proto-google-common-protos'

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接