【问题标题】:Execution failed for task when using google cloud API使用谷歌云API时任务执行失败
【发布时间】:2021-06-24 12:46:01
【问题描述】:

我想使用谷歌云语音转文字 API。

我关注了谷歌云doc 将语音音频文件转换为文本,但是 构建应用程序时给我错误。

我的 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\1.29.0\6bc5320615edb4b07aa827d47a5950b99714aa9e\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\0.82.0\65231ca155c5d74169de383519331991bb70f5b3\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\1.29.0\5256778459fff4f84d9c0172a6d103a35364afca\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\2.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.

我该如何解决?


【问题讨论】:

    标签: java android google-cloud-speech


    【解决方案1】:

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

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
    }
    

    【讨论】:

    • 仍然给我错误,无法将请求的类放入单个 dex 文件中(#方法:155797 > 65536)
    • 添加最新版本implementation 'androidx.multidex:multidex:latest_version'
    • build.gradle'sdefaultConfig部分添加:multiDexEnabled true
    • 谢谢兄弟,你拯救了我的一天?
    • 快乐编码 :)
    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多