适用于 Android 的 Firebase Admin SDK,未找到方法 [英] Firebase Admin SDK for Android, methods not found

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

问题描述

我正在构建一个由 Firebase 应用支持的 Android 应用,我希望能够创建一个可以编辑或删除其他用户帐户的管理员帐户.如果我理解正确,Firebase Admin SDK 应该允许我这样做.所以我按照此处的说明进行操作.

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

编译'com.google.firebase:firebase-admin:4.1.1'

在我的应用程序类中,我添加了这个:

FileInputStream serviceAccount = null;尝试 {serviceAccount = new FileInputStream("app/.json");} catch (FileNotFoundException e) {e.printStackTrace();}如果(服务帐户!= null){FirebaseOptions 选项 = new FirebaseOptions.Builder().setCredential(FirebaseCredentials.fromCertificate(serviceAccount)).setDatabaseUrl("https://.firebaseio.com/").建造();FirebaseApp.initializeApp(options);}

但是,它告诉我:

  • FirebaseOptions.Builder 中没有名为 setCredential() 的方法,并且
  • FirebaseApp.initializeApp() 采用 Context 对象,而不是 FirebaseOptions.

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

这是怎么回事?

解决方案

您不能在 Android 应用中将 Firebase Admin SDK 与 Firebase Android 客户端库一起使用.SDK 都提供具有完全相同的包和类名的类,因此它不可能同时使用它们(编译器如何知道您打算将哪个构建到您的应用程序中?).

举个例子,看看 Android 客户端库中 FirebaseOptions Builder 的 javadoc:

com.google.firebase.FirebaseOptions.Builder

现在从 java Admin SDK 中查看同一个类(注意 URL 不同):

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 Admin SDK,未找到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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