Android v1p1beta1 Speech - java.lang.NoSuchMethodError: No static method decodeBase64

424 Views Asked by At

When I build test speech app with Android Studio using google v1 speech library it works fine. Using v1p1beta1 speech library, a run time error occurs when it accesses the json credentials file and legacy commons codec and generates the following error:

java.lang.NoSuchMethodError: No static method decodeBase64 (Ljava/lang/String;)[B in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/org.apache.http.legacy.boot.jar)

How do I get v1p1beta1 speech library to not use the legacy commons library version?

Here is the gradle file with references to the latest v1p1beta1 speech and grpc libraries:

    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'
    }

Here is the stack trace error output:

Caused by: java.lang.NoSuchMethodError: No static method decodeBase64(Ljava/lang/String;)[B in class >Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/org.apache.http.legacy.boot.jar) at com.google.api.client.util.Base64.decodeBase64(Base64.java:101) at com.google.api.client.util.PemReader.readNextSection(PemReader.java:106) at com.google.api.client.util.PemReader.readFirstSectionAndClose(PemReader.java:135) at com.google.auth.oauth2.ServiceAccountCredentials.privateKeyFromPkcs8(ServiceAccountCredentials.java:296) at com.google.auth.oauth2.ServiceAccountCredentials.fromPkcs8(ServiceAccountCredentials.java:286) at com.google.auth.oauth2.ServiceAccountCredentials.fromJson(ServiceAccountCredentials.java:210) at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:174) at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:141) at com.google.cloud.android.speech.SpeechService$AccessTokenTask.doInBackground(SpeechService.java:365) at com.google.cloud.android.speech.SpeechService$AccessTokenTask.doInBackground(SpeechService.java:341)

1

There are 1 best solutions below

0
On

It seems that there was an issue with that version of the library. Here is a workaround by @ajaaym:

Remove from dependencies:

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

Add the below code:

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

I would recommend using the latest library available