簡體   English   中英

沒有靜態方法 decodeBase64 - Google 憑據

[英]No static method 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;

這是您使用的google-api-client-android版本的一個已知問題 解決方案是將版本從1.26升級到1.28 (或將其降級到1.25 ,請參閱此評論)。

我能夠使用Empty Activity項目和具有API 級別 26和目標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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM