-4

I'm doing a new project in Android Studio. I'd like to make my project in latest version and with Firebase Firestore to keep data.

I know that i need to put apply plugin: 'com.google.gms.google-services' but i don't find how .. and with lot of research, someone said that it is included in the 'com.android.application'..

I have included my google-services.json in the app document

I also tried to dowgrade gradle version and google.gms version, but unsuccessful

Here are my configuration :

app :

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.eating"
        minSdkVersion 26
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}



dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.2'
    implementation 'androidx.navigation:navigation-ui:2.3.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

    implementation platform('com.google.firebase:firebase-bom:26.2.0')
    implementation 'com.google.firebase:firebase-firestore:18.0.0' // Tried to force same value of firebase-core ?
    implementation 'com.google.firebase:firebase-core:18.0.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

My other build.gradle :

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.google.gms:google-services:4.3.4'

        // 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
}


And my MainActivity.java :

package com.example.eating;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.FirebaseApp;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

public class MainActivity extends AppCompatActivity {

    private String TAG = "MainAct";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Context mContext = this;
        FirebaseApp.initializeApp(this);
        FirebaseFirestore db = FirebaseFirestore.getInstance();

        db.collection("restaurant")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                    }
                });



        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

}

Just in order to get data from my databse (which i populated before)

So, when install and execute my application on my computer, i got this error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eating/com.example.eating.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.eating. Make sure to call FirebaseApp.initializeApp(Context) first.

I don't know how to solve this.

Dusk
  • 11
  • 2
  • This may help: https://stackoverflow.com/questions/47887880/make-sure-to-call-firebaseapp-initializeappcontext-first – Jems Jan 11 '21 at 15:40
  • Hi ! Thanks for your answer, i've already tried this solution to make an other class I've also tried to change gradle and google.gms's version (Maybe i used bad version ?). I've done a project about 6 months with firestore with gradle : 3.3.2 and gms 4.3.3 (which worked at time) but unsuccess too – Dusk Jan 11 '21 at 16:01
  • 3
    Does this answer your question? [Make sure to call FirebaseApp.initializeApp(Context) first](https://stackoverflow.com/questions/47887880/make-sure-to-call-firebaseapp-initializeappcontext-first) – Aiuspaktyn Jan 11 '21 at 16:03
  • Follw this, https://stackoverflow.com/questions/45977847/make-sure-to-call-firebaseapp-initializeappcontext-first-in-android/50101829, and yes `FirebaseApp.initializeApp(this);` should be called from a class extending the Application class not from and `Activity` – rahat Jan 11 '21 at 18:09
  • Hi ! Thanks, i've tried, but same error as below, ```Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package name 'com.example.eating'``` – Dusk Jan 12 '21 at 07:47

1 Answers1

1

build.gradle(module:app)

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

AndroidManifest.xml

<applicaton
       android:name=".MyApplication"

MyApplication.java

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();

   FirebaseApp.initializeApp(this);
}
Marsad
  • 859
  • 1
  • 14
  • 35
  • Hi! Thanks for your answer, i've done what you purpose to me, so, i was thinking it was because i've forget the "." before MyApplication. So, i've tried to add the line in 'plugins' and a new error : ```Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package name 'com.example.eating'``` – Dusk Jan 12 '21 at 07:28
  • @Dusk Check your package name on your google-services.json it should be the same as the local package name of your app ```"client_info": { "mobilesdk_app_id": "1:6596814400689:android:65d6f25f5", "android_client_info": { "package_name": "com.example.eating" }``` again download google-services.json file and add in your app (also check your package name) – Marsad Jan 12 '21 at 08:43
  • Ok, so, you find something ahah ! i have delete my first application in order to do an other, and now, an other error ahah ```Execution failed for task ':app:mergeExtDexDebug'. > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade > java.nio.file.NoSuchFileException: C:\Users\dusk\AndroidStudioProjects\eating\app\build\intermediates\external_file_lib_dex_archives\debug``` – Dusk Jan 12 '21 at 09:15
  • So tried to delete build folder, add ```multiDexEnabled true``` in my build.gradle's default config and clean project, when rebuild i have the same error – Dusk Jan 12 '21 at 09:25
  • 1
    @Dusk https://stackoverflow.com/a/57607026/12901786 – Marsad Jan 12 '21 at 09:28