1

I built an Android application with a service account on it to upload files on Google Drive, which works perfectly on the emulator and Android Nougat. Now, I tried it on a Samsung Galaxy Tab A with Android 8.1 and before implementing the service account everything worked fine. Now, when I try to save a file on google Drive from it, I get the 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)

The problematic code is where I log in, in particular in the fromStream method:

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

These are the new libraries implemented, I add commons-codec:commons-codec:1.15 since I read that it can be caused by it but it still doesn't work:

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

How can I solve it?

Edit: these are the only apaches' libraries that I used in the whole project, but they have been used to write the .xlsx file in local and never gave me any kind of problem on the Galaxt Tab A before implementing the service credentials:

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;
Fabio
  • 635
  • 6
  • 17
  • Read the comment about Android having its own Bas64 library: https://stackoverflow.com/q/54879286/8016720 – John Hanley Jul 22 '21 at 21:30
  • @JohnHanley Yes, this is the guide I followed. I implemented 'commons-codec:commons-codec:1.10' and the method decodeBase64 inside the problematic class but nothing changed. – Fabio Jul 23 '21 at 08:20
  • Why are you not using the Android libraries on Android? – John Hanley Jul 23 '21 at 08:56
  • @JohnHanley I never used or declared the library 'org.apache.commons.codec.binary.Base64', I don't know where is he getting it – Fabio Jul 23 '21 at 11:02
  • Your question does not show your code, imports, etc. That means I have to guess. – John Hanley Jul 23 '21 at 16:04

1 Answers1

1

This is a known issue with the version of google-api-client-android you're using. The solution is to upgrade the version from 1.26 to 1.28 (or downgrade it to 1.25, see this comment).

I was able to reproduce the issue using an Empty Activity project and a virtual device with an API Level 26 and a Target Android 8.0 (Google APIs)

Build.gradle content :

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 content:

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));
    }
}
Mohamed AMAZIRH
  • 1,159
  • 7
  • 26