Android的Firebase管理SDK,未找到方法 [英] Firebase Admin SDK for Android, methods not found

查看:114
本文介绍了Android的Firebase管理SDK,未找到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个由Firebase应用程序支持的Android应用程序,我希望能够创建一个可以编辑或删除其他用户帐户的管理员帐户。如果我的理解正确,Firebase Admin SDK应该允许我这样做。所以我按照这里的指示。



在我的应用程序中设置Admin SDK。我在build.app中添加了以下内容:

  compile'c​​om.google.firebase:firebase-admin:4.1.1'

在我的Application类中,我添加了这个:

  FileInputStream serviceAccount = null; 
try {
serviceAccount = new FileInputStream(app /< MY-DATABASE> .json);
} catch(FileNotFoundException e){
e.printStackTrace();

$ b $ if(serviceAccount!= null){
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl(https://< MY-APP> .firebaseio.com /)
.build();

FirebaseApp.initializeApp(options);



$ b $ p
$ b

然而,它告诉我:


  • FirebaseOptions.Builder中没有setCredential()方法,并且FirebaseApp.initializeApp()需要一个Context对象,而不是FirebaseOptions 。


根据文档, FirebaseOptions.Builder.setCredential()是一个新的方法,它取代了已弃用的 FirebaseOptions.Builder.setServiceAccount()。但是 setServiceAccount()也不存在。



这是怎么回事?您不能在Firebase Android客户端库旁边的Android应用中使用Firebase Admin SDK。 SDK提供的类和包名和类名完全相同,所以它们不可能同时使用它们(编译器如何知道你打算在你的应用程序中构建哪一个?)。例如,看一下Android客户端库中FirebaseOptions Builder的javadoc:

com.google.firebase.FirebaseOptions.Builder <现在看一下java Admin SDK中的同一个类(注意URL不同):



$ b $ =https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/FirebaseOptions.Builder =nofollow noreferrer> com.google.firebase.FirebaseOptions.Builder



你可以亲眼看到他们是不同的东西,即使他们有相同的名字。因此,您的编译器正在查看Android SDK定义,而不是管理SDK定义。



正如Frank所说,您可能不希望在Android中使用管理库应用程序。如果您想使用管理员SDK,请使用您所控制的服务器,并在需要时与您的Android应用进行通信。


I'm building an Android app backed by a Firebase app, and I would like to be able to create an Admin account that can edit or delete other user accounts. If I'm understanding correctly, the Firebase Admin SDK should allow me to do this. So I followed the instructions here.

To set up the Admin SDK in my app. I added the following to build.app:

compile 'com.google.firebase:firebase-admin:4.1.1'

And in my Application class, I added this:

FileInputStream serviceAccount = null;
try {
    serviceAccount = new FileInputStream("app/<MY-DATABASE>.json");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

if (serviceAccount != null) {
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
            .setDatabaseUrl("https://<MY-APP>.firebaseio.com/")
            .build();

    FirebaseApp.initializeApp(options);
}

However, it's telling me that:

  • There is no method called setCredential() in FirebaseOptions.Builder, and
  • FirebaseApp.initializeApp() takes a Context object, not FirebaseOptions.

According to the docs, FirebaseOptions.Builder.setCredential() is a new method, which replaces the deprecated FirebaseOptions.Builder.setServiceAccount(). But setServiceAccount() doesn't exist either.

What's going on here?

解决方案

You can't use the Firebase Admin SDK in an Android app alongside the Firebase Android client libraries. The SDKs both provide classes with the exact same package and class name, so it wouldn't possibly be able to use them both at the same time (how would the compiler know which one you intend to build into your app?).

As an example, take a look at the javadoc for FirebaseOptions Builder in the Android client library:

com.google.firebase.FirebaseOptions.Builder

Now look at the same class from the java Admin SDK (note the URL is different):

com.google.firebase.FirebaseOptions.Builder

You can see for yourself that they're different things, even though they have the same name. Your compiler is therefore looking at the Android SDK definition and not the admin SDK definition.

As Frank said, you probably don't want to use the Admin library within your Android app. If you want to use the admin SDK, use it from a server you control, and have your Android app communicate with that if needed.

这篇关于Android的Firebase管理SDK,未找到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆