没有静态方法 decodeBase64 - Google Credential

问题描述

我构建了一个带有服务帐户的 Android 应用程序,用于在 Google Drive 上上传文件,该应用程序在模拟器和 Android Nougat 上完美运行。现在,我在装有 Android 8.1 的三星 galaxy Tab A 上进行了尝试,在实施服务帐户之前一切正常。现在,当我尝试将文件保存在 Google Drive 上时,出现错误

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)

有问题的代码是我登录的地方,特别是在 fromStream 方法中:

private Credential authorize () throws IOException {
    InputStream in = getAssets().open("credentials.json");
    return GoogleCredential.fromStream(in)
            .createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));
}

这些是实现的新库,我添加了 commons-codec:commons-codec:1.15,因为我读到它可能是由它引起的,但它仍然不起作用:

//API Google Drive
implementation 'com.google.android.gms:play-services-auth:19.0.0'
implementation('com.google.api-client:google-api-client-android:1.26.0') {
    exclude group: 'org.apache.httpcomponents'
    exclude module: 'guava-jdk5'
}
// https://mvnrepository.com/artifact/com.google.apis/google-api-services-drive
implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') {
    exclude group: 'org.apache.httpcomponents'
    exclude module: 'guava-jdk5'
}

implementation 'com.google.http-client:google-http-client-gson:1.26.0'

implementation 'commons-codec:commons-codec:1.15'

我该如何解决

编辑:这些是我在整个项目中使用的唯一 apaches 库,但它们已被用于在本地编写 .xlsx 文件,并且在实施服务之前从未在 galaxt Tab A 上给我任何类型的问题凭据:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

解决方法

这是一个 known issue,其中包含您正在使用的 google-api-client-android 版本。解决方案是将版本从 1.26 升级到 1.28(或将其降级到 1.25,请参阅 this comment)。

我能够使用 Empty Activity 项目和具有 API 级别 26 和 Target Android 8.0 (Google API) 的虚拟设备重现该问题

Build.gradle 内容:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    
    //changing the version here from 1.26.0 to 1.28.0 resolves the issue 
    implementation('com.google.api-client:google-api-client-android:1.26.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude module: 'guava-jdk5'
    }
    implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude module: 'guava-jdk5'
    }
    implementation 'commons-codec:commons-codec:1.15'
}

MainActivity.java 内容:

package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.drive.DriveScopes;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            authorize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Credential authorize () throws IOException {
        InputStream in = getAssets().open("credentials.json");
        return GoogleCredential.fromStream(in)
                .createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));
    }
}