1

Project Gradle:

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

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


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

allprojects {
    repositories {
        jcenter()
    }
}

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

App Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        packagingOptions {
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE-FIREBASE.txt'
            exclude 'META-INF/NOTICE'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    //compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.google.android.gms:play-services:9.8.0'
    compile 'com.google.firebase:firebase-core:9.8.0'

    testCompile 'junit:junit:4.12'
}

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

Activity:

private DatabaseReference mDatabase;
...
mDatabase = FirebaseDatabase.getInstance().getReference(Config.FIREBASE_URL); //public static final String FIREBASE_URL = "https://myapp.firebaseio.com/ ";
...
mDatabase.setValue(edittextName.getText().toString());

Class:

public class Config {
    public static final String FIREBASE_URL = "https://myapps.firebaseio.com/ ";
}

I am getting the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.myapp, PID: 30211
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: FirebaseApp object has no DatabaseURL in its FirebaseOptions object.
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
                  at android.app.ActivityThread.access$800(ActivityThread.java:147)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5253)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
               Caused by: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: FirebaseApp object has no DatabaseURL in its FirebaseOptions object.
                  at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                  at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                  at com.myapp.MainActivity.onCreate(MainActivity.java:103)
                  at android.app.Activity.performCreate(Activity.java:5975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:147) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5253) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

How can I resolve the issue.

The link provided does not resolve my issue!

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Si8
  • 9,141
  • 22
  • 109
  • 221
  • Well, what is `Config.FIREBASE_URL`? It's crashing in `getInstance`, by the way, not `getReference` – OneCricketeer Nov 02 '16 at 01:36
  • How can I resolve it? I updated my question. – Si8 Nov 02 '16 at 01:38
  • 3
    Have you seen this post? http://stackoverflow.com/questions/37363791/firebase-database-getinstance-crashes-app – OneCricketeer Nov 02 '16 at 01:43
  • Yep... Re downloaded but same thing. – Si8 Nov 02 '16 at 01:50
  • Please post your MainActivity code. The error is in that file. You didn't supply the correct URL for your firebase initialization. – Sub 6 Resources Nov 02 '16 at 02:13
  • I copied the Url from the firebase .json file and it also matches in firebase console. – Si8 Nov 02 '16 at 02:24
  • 2
    You are misusing `FirebaseDatabase.getInstance().getReference()`. The argument to `getReference()` is a path string, not a URL. Maybe you intended to use `getReferenceFromUrl()`? But that is a secondary problem. The root problem is that your `FirebaseApp` object is not being initialized correctly. My guess is that there is a problem with your google-services.json file. – Bob Snyder Nov 02 '16 at 04:14
  • Sounds like an answer qbix! :-) – Frank van Puffelen Nov 02 '16 at 13:56

1 Answers1

0

Per @qbix, I had to use getReferenceFromUrl() and the app didn't crash.

Si8
  • 9,141
  • 22
  • 109
  • 221