Android v1p1beta1语音-java.lang.NoSuchMethodError:无静态方法encodeBase64

时间:2018-12-24 20:28:52

标签: java android nosuchmethoderror google-cloud-speech apache-commons-codec

当我使用google v1语音库在Android Studio中构建测试语音应用时,它可以正常运行。使用v1p1beta1语音库,当它访问json凭证文件和旧式通用编解码器并生成以下错误时,就会发生运行时错误:

  

java.lang.NoSuchMethodError:没有静态方法encodeBase64(Ljava / lang / String;)[B Lorg / apache / commons / codec / binary / Base64;中的B或其上级类(“ org.apache.commons.codec.binary.Base64”的声明出现在/system/framework/org.apache.http.legacy.boot.jar中)

如何使v1p1beta1语音库不使用旧版公共图书馆版本?

以下是gradle文件,其中引用了最新的v1p1beta1语音和grpc库:

    apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

ext {
    supportLibraryVersion = '25.4.0'
    grpcVersion = '1.4.0'
}

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.3'

    defaultConfig {
        applicationId "com.google.cloud.android.speech"
        targetSdkVersion 24
        versionCode 1
        versionName '1.0'
    }

    packagingOptions {
        pickFirst  '**'
    }

    signingConfigs {
        release {
            storeFile file(project.properties.storeFile)
            storePassword project.properties.storePassword
            keyAlias project.properties.keyAlias
            keyPassword project.properties.keyPassword
        }
    }

    productFlavors {
        dev {
            // Minimum version with platform multi-dex support
            minSdkVersion 22
        }
        prod {
            // Minimum version that can run gRPC (TLS extension)
            minSdkVersion 16
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            multiDexEnabled true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }

    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
        resolutionStrategy.force "com.android.support:support-annotations:$supportLibraryVersion"
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.3.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Support libraries
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"

    // Google Cloud Speech beta
    compile "com.google.cloud:google-cloud-speech:0.74.0-beta"
    compile "com.google.api.grpc:grpc-google-cloud-speech-v1p1beta1:0.39.0"

    // gRPC
    compile "io.grpc:grpc-okhttp:$grpcVersion"
    compile "io.grpc:grpc-protobuf-lite:$grpcVersion"
    compile "io.grpc:grpc-stub:$grpcVersion"
    compile 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.3.1'

    // OAuth2 for Google API
    compile('com.google.auth:google-auth-library-oauth2-http:0.12.0') {
        exclude module: 'httpclient'
    }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Support libraries
    //compile 'commons-codec:commons-codec:1.11'
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"

    // Google Cloud Speech beta
    compile "com.google.cloud:google-cloud-speech:0.74.0-beta"
    compile "com.google.api.grpc:grpc-google-cloud-speech-v1p1beta1:0.39.0"

    // gRPC
    compile "io.grpc:grpc-okhttp:$grpcVersion"
    compile "io.grpc:grpc-protobuf-lite:$grpcVersion"
    compile "io.grpc:grpc-stub:$grpcVersion"
    compile 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.3.1'

    // OAuth2 for Google API
    compile('com.google.auth:google-auth-library-oauth2-http:0.12.0') {
        exclude module: 'httpclient'
    }

这是堆栈跟踪错误输出:

  

由以下原因引起:java.lang.NoSuchMethodError:没有静态方法encodeBase64(Ljava / lang / String;)[B> Lorg / apache / commons / codec / binary / Base64;中的B或其超类(在/system/framework/org.apache.http.legacy.boot.jar中显示“ org.apache.commons.codec.binary.Base64”的声明)                         在com.google.api.client.util.Base64.decodeBase64(Base64.java:101)                         在com.google.api.client.util.PemReader.readNextSection(PemReader.java:106)                         在com.google.api.client.util.PemReader.readFirstSectionAndClose(PemReader.java:135)                         在com.google.auth.oauth2.ServiceAccountCredentials.privateKeyFromPkcs8(ServiceAccountCredentials.java:296)                         在com.google.auth.oauth2.ServiceAccountCredentials.fromPkcs8(ServiceAccountCredentials.java:286)                         在com.google.auth.oauth2.ServiceAccountCredentials.fromJson(ServiceAccountCredentials.java:210)                         在com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:174)                         在com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:141)                         在com.google.cloud.android.speech.SpeechService $ AccessTokenTask.doInBackground(SpeechService.java:365)                         在com.google.cloud.android.speech.SpeechService $ AccessTokenTask.doInBackground(SpeechService.java:341)

1 个答案:

答案 0 :(得分:0)

该版本的库似乎存在问题。 Here是@ajaaym的解决方法:

从依赖项中删除:

compile "com.google.api.grpc:grpc-google-cloud-speech-v1p1beta1:0.39.0"

添加以下代码:

FixedCredentialsProvider fixedCredentialsProvider = FixedCredentialsProvider.create(GoogleCredentials.create(null));
    try(SpeechClient client = SpeechClient.create(SpeechSettings.newBuilder().setCredentialsProvider(fixedCredentialsProvider).build())) {
    } catch (IOException e) {
      e.printStackTrace();
    }

我建议使用可用的latest