使用 google 云 API 时任务执行失败

Execution failed for task when using google cloud API

我想使用 google 云语音发送文本 API。

I followed the google cloud doc for converting speech audio file to text, but when building the app give me error.

我的 Gradle 依赖关系:

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation platform('com.google.cloud:libraries-bom:20.6.0')

    implementation 'com.google.cloud:google-cloud-speech'
}

我的代码:

 public void run(String path) throws IOException {
        // Instantiates a client
        try (SpeechClient speechClient = SpeechClient.create()) {

            // The path to the audio file to transcribe
            String gcsUri = path;

            // Builds the sync recognize request
            RecognitionConfig config =
                    RecognitionConfig.newBuilder()
                            .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                            .setSampleRateHertz(16000)
                            .setLanguageCode("en-US")
                            .build();
            RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();

            // Performs speech recognition on the audio file
            RecognizeResponse response = speechClient.recognize(config, audio);
            List<SpeechRecognitionResult> results = response.getResultsList();

            for (SpeechRecognitionResult result : results) {
                // There can be several alternative transcripts for a given chunk of speech. Just use the
                // first (most likely) one here.
                SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
                System.out.printf("Transcription: %s%n", alternative.getTranscript());
            }
        }
    }

但是下面还是报错

> Task :app:mergeDebugJavaResource FAILED
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 4 files found with path 'META-INF/INDEX.LIST' from inputs:
      - C:\Users\Adham\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-speech.29.0bc5320615edb4b07aa827d47a5950b99714aa9e\google-cloud-speech-1.29.0.jar
      - C:\Users\Adham\.gradle\caches\modules-2\files-2.1\com.google.api.grpc\proto-google-cloud-speech-v1p1beta1[=14=].82.0231ca155c5d74169de383519331991bb70f5b3\proto-google-cloud-speech-v1p1beta1-0.82.0.jar
      - C:\Users\Adham\.gradle\caches\modules-2\files-2.1\com.google.api.grpc\proto-google-cloud-speech-v1.29.056778459fff4f84d9c0172a6d103a35364afca\proto-google-cloud-speech-v1-1.29.0.jar
      - C:\Users\Adham\.gradle\caches\modules-2\files-2.1\com.google.api.grpc\proto-google-common-protos.3.2\a35fd6ed973f752604fce97a21eb1e09d6afc467\proto-google-common-protos-2.3.2.jar
     Adding a packagingOptions block may help, please refer to
     https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
     for more information

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

How can I solve that ?


yourProject/app/build.gradle 里面 android{}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}