Firebase Database does not work on android emulator

Viewed 2556

My app is checking if a certain data is in the Firebase Realtime Database with a addListenerForSingleValueEvent added to a database reference. The emulator in which i started the project works fine and retrieves the data perfectly but when i change the emulator (let's say i switch to PIXEL 3XL) the listener doesn't work. I've seen in another stackoverflow question that someone had the same problem and the database retrieved data but after a long time. Does anyone know why this happens? Should i keep on developing with the default emulator and not care about the realtime database not working on another emulator? Can you explain why this happening?

I use this in the manifest: ...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>...

Gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath 'com.google.gms:google-services:4.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle(Module)

apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "..."
        minSdkVersion 28
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-database:19.3.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

Edit 1: Added the code (It works on the default emulator,pixel 3, but not on another) The logs are just to see if the code is doing what it should.

DatabaseReference database, newRef;

 protected void onCreate(Bundle savedInstanceState) {
(...)
database = FirebaseDatabase.getInstance().getReference();
newRef = database.child(option).child(strSelectedYear).child(strSelectedMonth).child(strSelectedDay);
        newRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot d: dataSnapshot.getChildren()) {
                    hours.remove(d.getKey());
                    Log.i("hAI FRAAA", d.getKey());

                }
                String msg = "";
                for (int j = 0;j<hours.size();j+=1)
                    msg += " " + hours.get(j);
                Log.i("Free hours", msg);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
}
3 Answers

You'll need to do some more troubleshooting on your own to figure out what's going wrong. A few steps I would take:

  1. Stop ignoring possible errors and implement onCancelled. At its minimum that should be public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }.
  2. Ensure the internet access works on the second emulator. So do other apps work?
  3. Enabling debug logging and check in the logcat output what is going on.

I've been dealing with this issue for a few days. Finally connecting my physical android device to computer fixed the issue for me. I guess the emulators internet connection is not as healthy as Firestore expects. The cause of the problem was the emulator for me.

I hade same problem with latest Android Emulator Version, I just replaced with previews version and fixed my problem.

  1. Go to Android SDK
  2. Make Copy of Emulator
  3. Download previews version of Android Emulator from Emulator download archives
  4. Copy Emulator folder inside Android SDK
  5. Copy package.json from old Emulator and replace version with new version: <revision><major>31</major><minor>1</minor><micro>4</micro></revision> That's it.
Related