Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Admin SDK for Android, methods not found

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?

like image 483
Russell Stewart Avatar asked Feb 07 '17 18:02

Russell Stewart


2 Answers

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.

like image 196
Doug Stevenson Avatar answered Sep 26 '22 15:09

Doug Stevenson


Now FirebaseOptions class taken from other dependencies, May you can remove firebase components from other dependencies as below using exclude tag.

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

compile ('com.google.firebase:firebase-messaging:9.6.1'){
    exclude module: 'firebase-common'
}
compile ('com.google.firebase:firebase-auth:9.6.1'){
    exclude module: 'firebase-common'
}
compile ('com.google.firebase:firebase-database:9.6.1'){
    exclude module: 'firebase-common'
}
compile ('com.firebase:firebase-client-android:2.5.0'){
    exclude module: 'firebase-common'
}
like image 23
Ebin Joy Avatar answered Sep 23 '22 15:09

Ebin Joy