java.lang.NoSuchMethodError: No static method fromStream

Hello,

I am making new extension to create google OAuth token to call google API.
In java it works fine. it makes token well.

and I could build the extension successfully with ANT.
but when I run this extension, runtime error occured.

java.lang.NoSuchMethodError: No static method fromStream(Ljava/io/InputStream;)Lcom/google/api/client/googleapis/auth/oauth2/GoogleCredential; in class Lcom/google/api/client/googleapis/auth/oauth2/GoogleCredential; or its super classes (declaration of ‘com.google.api.client.googleapis.auth.oauth2.GoogleCredential’ appears in /data/app/edu.mit.appinventor.aicompanion3-km_HupvZItefA–SrsXeJQ==/base.apk:classes2.dex)

full java source code is here.

package hellosoft.GoogleAccessToken;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.*;

//new
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

import android.util.Log;

@DesignerComponent(version = GoogleAccessToken.VERSION,
description = "Get google access token " + “by service account key file”,
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = “”)

@SimpleObject(external = true)
public class GoogleAccessToken extends AndroidNonvisibleComponent implements Component {

public static final int VERSION = 1;

private String keyfilepath = "";

private ComponentContainer container;

public GoogleAccessToken(ComponentContainer container) {
    super(container.$form());
    this.container = container;
}


// Call Block
@SimpleFunction(description = "Make Google Token.")
public static String getAccessToken(String keyfilepath) {

	FileInputStream serviceAccount;
	String token = "Error";

	try {
		serviceAccount = new FileInputStream(keyfilepath);
		
		GoogleCredential googleCred = GoogleCredential.fromStream(serviceAccount);
        
		GoogleCredential scoped = googleCred.createScoped(
		    Arrays.asList(
		      "https://www.googleapis.com/auth/cloud-platform"
		    )
		);

		scoped.refreshToken();
		token = scoped.getAccessToken();

	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		token = "file not found " + keyfilepath;
		Log.e("GoogleAccessToken", "File not found " + keyfilepath);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		token = "IOexception";
		Log.e("GoogleAccessToken", "IOException ");
	}
        
   return token;
}

}

appinventor script is here.

error message is here.

I included all of google libraries.

I guess two things.

  1. make method static
  2. libraries version

Please help me.

I think the error is because of trying to access non-static variable from a static method.
First option seems to be correct.

I'm sorry, I do not kow the answer to your question, I just wanted to suggest you to follow the naming conventions, which is UpperCamelCase for property, method and event names (i.e. the first letter should be a capital letter) and lowerCamelCase for parameter names

example:
naming

Taifun