-1

I'm trying to retrieve the registration token from my device using Firebase in an app which has a TextView that has as a purpose to contain the value of the token in it, when running the app everytime i get a java.lang.RuntimeException error, i added this code FirebaseApp.initializeApp(this); as instructed but i still get such error, i'm quite new to Android Studio and Firebase and i'm still experimenting.

MainActivity.java

package com.gci.gestioncapteursincendie;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

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

        textView= findViewById(R.id.textViewToken);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                      if(task.isSuccessful()){
                          String token = task.getResult().getToken();
                          textView.setText("Token : " + token);
                        }
                        else
                      {
                          textView.setText("Token Not Generated");
                      }
                    }
                });
    }

}

Log Trace:

03-31 19:10:19.203 11562-11562/com.gci.gestioncapteursincendie E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gci.gestioncapteursincendie, PID: 11562
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gci.gestioncapteursincendie/com.gci.gestioncapteursincendie.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
        at com.gci.gestioncapteursincendie.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7406) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.google.gms:google-services:4.1.0'
        classpath 'io.fabric.tools:gradle:1.26.1'


        // 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
}
Amine
  • 901
  • 2
  • 14
  • 33
  • btw its really helpful to have line numbers for source code examples – jonathan Heindl Mar 31 '19 at 18:32
  • 2
    Possible duplicate of [Default FirebaseApp is not initialized](https://stackoverflow.com/questions/40081539/default-firebaseapp-is-not-initialized) – Zoe Mar 31 '19 at 18:33
  • post your project build gradle file here too – Masoom Badi Mar 31 '19 at 18:33
  • @Sam. original post edited. – Amine Mar 31 '19 at 18:36
  • @Zoe Please consider unmarking my question as a duplicate, it's obvious that i possibly have all the needed lines of code in my build.gradle for Firebase implementation. – Amine Mar 31 '19 at 18:41
  • @Amine you might wanna read the answer before you post a comment that shows you didn't. – Zoe Mar 31 '19 at 18:42
  • @Zoe If you're talking about FirebaseApp.initializeApp(this); this line of code i have it added already, and i already tested connection between Firebase and my app and it checked fine – Amine Mar 31 '19 at 18:43
  • @Amine that has nothing to do with code in build.gradle, like you mentioned in your last comment... – Zoe Mar 31 '19 at 18:43
  • You need to initialize the `FirebaseApp.initializeApp(this);` from class which extends **Application**. Create new class which extends **Application** and do initialization part there, don't forget to add that class reference to `manifest` file – Masoom Badi Mar 31 '19 at 18:43
  • Fun fact: that's [mentioned in an answer on the dupe target](https://stackoverflow.com/a/45735303/6296561) – Zoe Mar 31 '19 at 18:45
  • @Zoe `classpath 'com.google.gms:google-services:4.1.0'` is required... and there is an issue which can be solved by downgrading version from **4.1.0** to **4.0.1** – Masoom Badi Mar 31 '19 at 18:45
  • @Zoe i did state that i'm a newbie in this and i would obviously go for the highest upvoted answers, the answer you posted has 0 upvotes. – Amine Mar 31 '19 at 18:48
  • @Amine Whether you're new or not does not matter - and the highest upvoted answer isn't necessarily the best, or for that matter a correct or updated one. If you think you can use "I'm new" to either SO or a tech as an excuse for anything, you are *very* wrong. – Zoe Mar 31 '19 at 18:50
  • @Amine no, I care about keeping SO what it's intended - [a high-quality library of questions and answers](https://meta.stackoverflow.com/a/382032/6296561). Which includes closing duplicates. Also, I've flagged your last comment. – Zoe Mar 31 '19 at 18:54
  • 1
    @Sam. Thanks it worked! – Amine Mar 31 '19 at 22:22

2 Answers2

1

From the docs:

For a vast majority of apps, FirebaseInitProvider will handle the initialization of Firebase for the default project that it's configured to work with, via the data contained in the app's google-services.json file.

In the event that an app requires access to another Firebase project in addition to the default project, initializeApp(Context, FirebaseOptions, String) must be used to create that relationship programmatically.

If you are using this app in two different projects then you need to use initializeApp().

https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp

Also update your Google service version to 4.2.0 as 4.1.0 had configuration issues.

classpath 'com.google.gms:google-services:4.2.0'
Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

Add apply plugin: 'com.google.gms.google-services' at the end of app gradle:

 dependencies {
     classpath 'com.android.tools.build:gradle:3.3.2'
     classpath 'com.google.gms:google-services:4.2.0'
     ...
 }
 apply plugin: 'com.google.gms.google-services'
Md Shahbaz Ahmad
  • 345
  • 4
  • 12