Compile time error for setServiceAccount() with Fi

2019-09-09 18:03发布

问题:

I was working my Android app for sending push notifications using Firebase cloud messaging. I was setting up my server referring the guide https://firebase.google.com/docs/server/setup#prerequisites.

My project level build.gradle is like :

dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.google.gms:google-services:3.0.0'
}

App level build.grade :

dependencies {   
compile 'com.firebase:firebase-client-android:2.5.2'
compile 'com.firebase:geofire:1.1.0'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.0'
compile 'com.google.firebase:firebase-server-sdk:[3.0.0,)'
compile 'com.google.firebase:firebase-core:9.0.0'

}

I have created the service account credentials on the console as well but while I am initializing the SDK referring https://firebase.google.com/docs/server/setup#add_the_sdk

FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount
(new  FileInputStream("path/to/serviceAccountCredentials.json"))
.setDatabaseUrl("https://databaseName.firebaseio.com/")
.build();

I am getting compile Error:(116, 21) error: cannot find symbol method setServiceAccount(FileInputStream)

I have checked many places but I am unable to find what I am doing wrong. Any help would be highly appreciated.

回答1:

In your server project you need to use only the dependency:
com.google.firebase:firebase-server-sdk:[3.0.0,) and remove:

compile 'com.firebase:firebase-client-android:2.5.2'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.0'
compile 'com.google.firebase:firebase-core:9.0.0'

The first is a server sdk while the others are client sdk and they will have conflicts if you try to use them together.



回答2:

You're including a different version of the Firebase Database SDK than what you have for the other Firebase features:

compile 'com.firebase:firebase-client-android:2.5.2'

If you change it to:

compile 'com.google.firebase:firebase-database:9.0.0'

It will work better.

For more information, see the Firebase documentation for Android developers, from where I copied the line above.