title: "FCM" App Crashes on this line getToken() "java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process"
body: <h1>FCM (Firebase Cloud Messaging) initialize issue</h1>
  <p>I started working on FCM module, I fallowed as per the firebase guidelines. i'm getting two exceptions like RuntimeException and IllegalStateException. Firebase init default but why and what is the issue in FCM.</p>
  <h3>1. From Android Studio tools -&gt; Firebase -&gt; Cloud Messaging.</h3>
  <h3>2. I fallowed Set up Firebase Cloud Mesasging.</h3>
  <p>I fallowed below link:</p>
  <p><a href="https://firebase.google.com/docs/cloud-messaging/android/client" rel="nofollow noreferrer">https://firebase.google.com/docs/cloud-messaging/android/client</a></p>
  <p>App : Build.gradle</p>
  <pre><code>dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      //noinspection GradleCompatible
      implementation 'com.android.support:appcompat-v7:28.0.0'
      implementation 'com.android.support.constraint:constraint-layout:1.1.3'
      implementation 'com.google.firebase:firebase-messaging:17.4.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  }
  
  apply plugin: 'com.google.gms.google-services'
  
  </code></pre>
  <p>Project : Build.gradle</p>
  <pre><code>buildscript {
      repositories {
          google()
          jcenter()
          
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:3.3.2'
          classpath 'com.google.gms:google-services:4.1.0'
  
          // 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
  }
  </code></pre>
  <p>In MainActivity.class, i trying to get token but app crashes.</p>
  <pre><code> public class MainActivity extends AppCompatActivity {
  
      public static final String TAG = MainActivity.class.getSimpleName();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          String tokenId = FirebaseInstanceId.getInstance().getToken(); //Crashes in this line 
          Log.d(&quot;SH&quot;, tokenId);
      }
  }
  </code></pre>
  <p>in logcat i got two exceptions</p>
  <pre><code> E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.package PID: 29368
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.testfcm/com.package.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.package. Make sure to call FirebaseApp.initializeApp(Context) first.
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
          at android.app.ActivityThread.-wrap11(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6626)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.package. Make sure to call FirebaseApp.initializeApp(Context) first.
          at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
          at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:1)
          at com.package.MainActivity.onCreate(MainActivity.java:20)
          at android.app.Activity.performCreate(Activity.java:7032)
          at android.app.Activity.performCreate(Activity.java:7023)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) 
          at android.app.ActivityThread.-wrap11(Unknown Source:0) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6626) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
  </code></pre>
  <p>Finally i found solutions, Now i able to get FCM Token.</p>
  <h3>In project build.gradle file you need add single line code code i.e., mavenLocal().</h3>
  <p>Fallow as per the below solutions cross check the project : build.gradle file and app : build.gradle file.</p>
  <p>Project : build.gradle</p>
  <pre><code>buildscript {
      repositories {
          mavenLocal()  //This is the line you need to add 
          google()
          jcenter()
  
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:3.3.2'
          classpath 'com.google.gms:google-services:4.2.0'
  
          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
      }
  }
  
  allprojects {
      repositories {
          mavenLocal() //This is the line you need to add 
          google()
          jcenter()
  
      }
  }
  
  task clean(type: Delete) {
      delete rootProject.buildDir
  }
  
  </code></pre>
  <p>App : build.gradle</p>
  <pre><code>apply plugin: 'com.android.application'
  
  
  android {
      compileSdkVersion 28
      defaultConfig {
          applicationId &quot;com.example.testfcm&quot;
          minSdkVersion 21
          targetSdkVersion 28
          versionCode 1
          versionName &quot;1.0&quot;
          testInstrumentationRunner &quot;android.support.test.runner.AndroidJUnitRunner&quot;
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      //noinspection GradleCompatible
      implementation 'com.android.support:appcompat-v7:28.0.0'
      implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  
  
      implementation 'com.google.firebase:firebase-core:16.0.8'
      implementation 'com.google.firebase:firebase-iid:17.1.1'
      implementation 'com.google.firebase:firebase-messaging:17.5.0'
  
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  }
  apply plugin: 'com.google.gms.google-services'
  </code></pre>