Android Studio - Cannot resolve symbol 'fireba

2019-01-09 14:49发布

问题:

I'm currently upgrading an app to the new Firebase version. I followed the guide, included classpath 'com.google.gms:google-services:3.0.0' in the dependencies of my project build.gradle as well as compile 'com.google.firebase:firebase-core:9.0.1' among others in the dependencies of my module build.gradle and also apply plugin: 'com.google.gms.google-services' at the end of that file.

I get a "cannot resolve symbol 'firebase'" in my imports i.e. import com.google.firebase.database.DatabaseReference;. Those are not errors that appear when building, so this seems to be working, but they are visible in the code editor of Android Studio.

The imports worked just fine a couple of days ago (except for FirebaseAuth, which was under maintenance). I did not change anything about the code since then (except trying to upgrade to 9.0.2, which lead to the same result). The only thing I did was update some components of the Android SDK, but I can't remember which. The Android SDK as well as Google Repository and Google Play Services are of the newest version. Rebuilding, cleaning and invalidate caches / restart had no effect.

Any ideas how to fix this?

回答1:

You need to add this dependency in your build.gradle(app)

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


回答2:

Now with New Android Studio ,Its so easy to add Firebase to your Project.

Below are Simple Steps-

1. On Android Studio’s Tools menu, you’ll see an entry that reads Firebase.

2. Select this, and a Firebase Assistant pane will open to the side of your code editor:

3. On this pane, click the arrow beside ‘Authentication’, and you’ll see a step through for ‘Email and Password Authentication’.

4. Click the ‘Connect to Firebase’ button.

Your browser will open with a ‘Request for Permissions’ dialog:

5.Click ‘Allow’, and then after a ‘Success!’ screen, you’ll be given a dialog with which to connect to Firebase.

6.You will see Connected .Its Done !

For more on Firebase refer here.



回答3:

You can open and use the Assistant window in Android Studio by following these steps:

  1. Click Tools > Firebase to open the Assistant window. And it will guide you for sync firebase and your project.


回答4:

Make sure you fulfill the following prerequisites before adding firebase to your project.

  1. For working with Firebase you should install Android Studio 1.5 or higher.
  2. Download the latest Google Play services SDK from through Android SDK Manager.
  3. The device should be running Android 2.3 (Gingerbread) or newer, and Google Play services 9.2.0 or newer.

I find out all this after hours of struggle, so thought of sharing with others.

Source: Adding Firebase to your Android App



回答5:

To use the Firebase Messaging service you need to add the following dependencies to your app's build.gradle file:

            compile 'com.google.firebase:firebase-messaging:9.4.0'

I had the same problem but thanks to this answer:

https://stackoverflow.com/a/39353961/4836759



回答6:

Add following in your gradle file under dependencies:

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

If you are getting a build error complaining about duplicate files you can choose to exclude those files by adding the packagingOptions directive to your build.gradle file:

android {
    ...
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}


回答7:

After adding it in from the built in Firebase tool and also following the following a tutorial, the imports did not work on one of my classes so all you have to do is sync the gradle files

Tutorial I've used: https://www.androidtutorialpoint.com/firebase/firebase-cloud-messaging-tutorial/

Tools/Android/Sync Project with Gradle Files.

Works 100% now



回答8:

In the new SDK, it's no longer necessary to call Firebase.setAndroidContext() so you can remove it from your code.

In the new SDK, Firebase references are replaced by DatabaseReference and you use the FirebaseDatabase class to get an initial reference to your database. So you can get the database reference in your code as follows:

BEFORE Firebase rootRef = new Firebase("https://.firebaseio.com/"); AFTER DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

Note that your Database URL is automatically determined from the google-services.json file you downloaded, so you don't need to specify it. If you want to specify it though, you still can (which might be convenient for migration purposes):

BEFORE Firebase ref = new Firebase("https://.firebaseio.com/path/to/data"); AFTER DatabaseReference ref = FirebaseDatabase.getInstance() .getReferenceFromUrl("https://.firebaseio.com/path/to/data");

22



回答9:

Invalidating cache didn't work for me. But deleting .idea/libraries worked like magic.

More info here: https://stackoverflow.com/a/50129167/971972



回答10:

You need the firebase client library. Add this to your app gradle:

dependencies {

    // Firebase
    implementation 'com.firebase:firebase-client-android:2.5.2'