Android Firebase Realtime "DatabaseDefault FirebaseApp is not initialized" - android

I'm trying to connect to Firebase realtime DB from android.
SDK Version 29
public void init(Context context)
{
FirebaseApp.initializeApp(context);
mDatabase = FirebaseDatabase.getInstance("https://xxxx.europe-west1.firebasedatabase.app/").getReference();
I get the error:
Default FirebaseApp is not initialized in this process com.xxx.xxxx. Make sure to call FirebaseApp.initializeApp(Context) first.
firebase.json is
{
"database": {
"rules": "secrules.json"
}
}
secrules.json is
{
"rules": {
".read": "now < 1619046000000", // 2021-4-22
".write": "now < 1619046000000", // 2021-4-22
}
}
build gradle module app level is
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:26.8.0')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database'
build gradle project level is
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
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
}
I've tried to follow latest guidelines and a few suggestions in stack overflow with no difference.
Project was initialised from the firebase console. I'm not using an emulator database.

The primary firebase projects and related assets including database are initialized automatically, no need to specify anything, just add google services json to project. If for some reason you need to initialize it, best place is the class extending Application.
public ZAONE extends Application {
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
database = FirebaseDatabase.getInstance();
}}
And then add that class to the manifest. A good resource is here, documentation on setting up on Android

Related

Firebase authentication Signup with Phone Number error

I am using Firebase authentication to sign up using phone number. But any sms not being send.
I am getting this error
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod()
returned Gms: com.google.firebase.auth.api.internal.zzak#d5a4a6d
My gradle file
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.13-beta-3'
androidTestImplementation 'androidx.test:runner:1.3.0-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha01'
}
I understand you have missed setting up Firebase in your application.
Follow the whole process from here
Make sure you have google-services.json file in in the module (app-level) directory of your app.
Your root-level (project-level) Gradle file (build.gradle) must have these lines.
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
// ...
// Add the following line:
classpath 'com.google.gms:google-services:4.2.0' // Google Services plugin
}
}
allprojects {
// ...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
// ...
}
}
Then In your module (app-level) Gradle file (usually app/build.gradle), add a line to the bottom of the file.
apply plugin: 'com.android.application'
android {
// ...
}
// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
In your module (app-level) Gradle file (usually app/build.gradle), add the dependency for the core Firebase SDK:
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:17.0.0'
// Getting a "Could not find" error? Make sure that you've added
// Google's Maven repository to your root-level build.gradle file
}

error: cannot find symbol import com.firebase.ui.database.FirebaseRecyclerAdapter;

I have the right library and dependencies the build gradle(app) file. But I'm still etting this error.
According to some answers here, I have already tried all of them. I already checked if i have the right version, whether i have included the package correctly, whether the package name is same in google-service.json file, in manifest file, or in gradle file. I have already done all these.
// All google dependencies
implementation 'com.google.android.gms:play-services-maps:12.0.1'
implementation 'com.google.android.gms:play-services-location:12.0.1'
implementation 'com.google.android.gms:play-services-places:12.0.1'
implementation 'com.google.android.gms:play-services-auth:12.0.1'
implementation 'com.google.firebase:firebase-database:12.0.1'
implementation 'com.google.firebase:firebase-auth:12.0.1'
implementation 'com.google.firebase:firebase-messaging:12.0.1'
implementation 'com.firebaseui:firebase-ui:3.3.0'
implementation 'com.firebase:firebase-client-android:2.5.2'
implementation 'com.google.firebase:firebase-core:12.0.1'
implementation 'com.google.android.gms:play-services-gcm:12.0.1'
implementation 'com.google.android.gms:play-services:12.0.1'
implementation 'com.github.ar-android:DrawRouteMaps:1.0.0'
these are backdated on purpose.
as this answer(https://stackoverflow.com/a/39217164/11686135) suggests, i have already done that:
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
and this is the error its showing: Cannot resolve symbol 'database'
maybe somehow the dependencies not working...?
i am really novice at this. thanks in advance
Perhaps you are missing a dependency, please refer to the
GitHub project for FirebaseUI,
you might be missing just one more dependency such as any of these:
dependencies {
// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:5.0.0'
// FirebaseUI for Cloud Firestore
implementation 'com.firebaseui:firebase-ui-firestore:5.0.0'
// FirebaseUI for Firebase Auth
implementation 'com.firebaseui:firebase-ui-auth:5.0.0'
// FirebaseUI for Cloud Storage
implementation 'com.firebaseui:firebase-ui-storage:5.0.0'
}
if someone is getting same issue then try searching for Firebase Dependencies latest version
and add it issue will be resolved
These are Latest dependencies add accordingly for use
To learn more
click here
dependencies {
// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:6.3.0'
// FirebaseUI for Cloud Firestore
implementation 'com.firebaseui:firebase-ui-firestore:6.3.0'
// FirebaseUI for Firebase Auth
implementation 'com.firebaseui:firebase-ui-auth:6.3.0'
// FirebaseUI for Cloud Storage
implementation 'com.firebaseui:firebase-ui-storage:6.3.0'
}
Firebaserecycleroption.
Basically android studio will change their version.
but build.gradle old dependencies are working in new version. So guys try to all dependecies in gradle.
gradle build dependencies:
compile 'com.firebaseui:firebase-ui-database:2.0.0'
implementation 'com.firebaseui:firebase-ui-database:4.3.2'
implementation 'com.firebaseui:firebase-ui-firestore:4.3.1'
implementation 'com.firebaseui:firebase-ui-database:5.0.0'
implementation 'com.firebaseui:firebase-ui-database:6.3.0'
//( Below two dependencies are same apply both )
compile 'com.firebaseui:firebase-ui-database:0.4.0'
compile'com.firebaseui:firebase-ui-database:1.1.1'
implementation 'com.firebaseui:firebase-ui-database:8.0.1'
( must try it )

Make sure to call FirebaseApp.initializeApp(Context) first. The error is not sending message to firebase and application terminated

I have only three line of code to send msg to firebase and when I run app it automatically terminated. I added firebase through Tools menu.
I have tried internet permission to manifest file but didn't work.
var firebaseDatabase = FirebaseDatabase.getInstance()
var databaseRef = firebaseDatabase.reference
databaseRef.setValue("Hello There")
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-alpha01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.1.0'
}
}
"I/FirebaseInitProvider: FirebaseApp initialization unsuccessful"
"Unable to start activity ComponentInfo{.MainActivity}:
java.lang.IllegalStateException: Default FirebaseApp is not
initialized in this process ... Make sure to call
FirebaseApp.initializeApp(Context) first."
As the error says, you need to init the FirebaseApp instance before calling its methods. This it the line:
FirebaseApp.initializeApp(this); // "this" is a context
A common way to do it - to add this line to onCreate() method of your custom Application class.
And here, on Codepath you can find how to create your custom Application class to put that line in.
Update
This will work after you configure the Firebase App project in Firebase Console as well. Don't forget to do the steps from here, Firebase docs

setvalue method does not write in firebase database

I have set up a firebase database and connected to that with the following details, but setValue method is not writing anything. onComplete event is not not even called. What is the reason?
Android Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
Code base:
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("users");
User user = new User("A","B","C");
mFirebaseDatabase.child(usr.getUsername()).setValue(usr);
build.gradle script:
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:4.1.0'
And the main thing which gradle does not accept 11.8.0 which firebase assistant suggests . mine is :
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
implementation 'com.google.firebase:firebase-auth:9.6.1'
implementation 'com.google.firebase:firebase-core:9.6.1'
implementation 'com.google.firebase:firebase-database:9.6.1'
implementation project(':andengine:AndEngine-GLES2-AnchorCenter')
and the database rules are :
"rules": {
".read": true,
".write": true
}
Are there any settings in Firebase or Android studio?
I found finally the answer. Everything was OK except on my country's IP. My country is under sanction and by VPN it solved. but when disconnecing VPN it is not workinf even after compile.

Cannot resolve symbol default_web_client_id in Firebase's Android Codelab

I am trying to learn Firebase, so I went through the Android Codelab. The project they gave me however, had an error:
Cannot resolve symbol default_web_client_id
And I didn't know how to solve it, since I didn't know the value of default_web_client_id or what it is. It is in the onCreate() method:
SigninActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
mFirebaseAuth = FirebaseAuth.getInstance();
// Assign fields
mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
// Set click listeners
mSignInButton.setOnClickListener(this);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
I have no idea what it is, what's its value is, and why is it giving me this error. I haven't changed anything so far except for adding the google-services.json. I have added my SHA-1 and enabled Google in the console.
Sometimes there is issue while parsing google-services.json. I have reported this issue with to concerned team.
Meanwhile follow below step to fix this issue to get going further -
1) Open google-services.json file -> client -> oauth_client -> client_id
2) Copy this client ID and hardcode this .requestIdToken("your ID")
It would allow to request "IdToken" via GoogleSignInAccount post successful google login and to authorize your credential with firebase.
EDIT
Try deleting and recreating the project and re-importing new google-service.jsonin your Android project
A more generic solution would be to add the google-services.json into the app's root directory.
And add
apply plugin: 'com.google.gms.google-services at the end of build.gradle file.
Explanation
When the app builds the key value pair strings from google-services.json config file are then placed into the values.xml file to make them globally available for use from anywhere in your code. This saves us from hard coding the client_id in your code.
Note
Do not add the default_web_client_id with client_id as its value in the strings.xml in order to avoid the error of duplication, Error: Duplicate resourceslater on when you run your code.
google-services.json in ./app/ folder
Add in project-level build.gradle the following:
buildscript {
...
dependencies {
...
classpath 'com.google.gms:google-services:4.3.5'
}
In app-level build.gradle, apply the plugin:
apply plugin: 'com.google.gms.google-services'
This is the annoying thing I found. Upgrading it from 4.3.5 to anything higher than that makes Android Studio unable to detect the generated values.xml file.
After a time searching the "smart" fix without insert directly the client_id, following this answer from FirebaseUI project I just need to add the next line in app/build.gradle:
implementation 'com.firebaseui:firebase-ui-auth:4.3.2'
Apparently R.string.default_web_client_id is generated from the IDE build
I had assumed we are supposed to manually add it - time consuming mistake
https://developers.google.com/android/guides/google-services-plugin
The google-services plugin has two main functions: 1) Process the
google-services.json file and produce Android resources that can be
used in your application's code.
~~~~
The main result of the JSON processing is to produce two XML files
which you can reference as Android resources in your Java code.
And so - after successful build, if you search the IDE for string default_web_client_id , you will see one result is values.xml under the /generated folder, and there it has the values for your firebase config, like the example below.
Actually seeing that file, helped to clarify things here
<resources>
<string name="default_web_client_id" translatable="false">123.apps.googleusercontent.com</string>
<string name="firebase_database_url" translatable="false">https://123.firebaseio.com</string>
<string name="gcm_defaultSenderId" translatable="false">123</string>
<string name="google_api_key" translatable="false">123</string>
<string name="google_app_id" translatable="false">123</string>
</resources>
**The main issue with this right now for me was to make sure to download the json file from the same location. If the initial one came from the firebase console do not use the api console to get the file, and vise versa. The files are not the same **
I already have google-services.json downloaded and parsed, but still it doesn't find the string.
I noticed that my oauth_client had a key with client_type of 1 and that's all. In the Google API console, I only had an Android key.
So, you need to go to the API console and generate a Web Server key. Then, download your google-services.json again, and you'll have a oauth_client with a type of 3.
Now, the plugin will generate a string called default_web_client_id.
I had the same problem or similar,
Make sure that in your google-services.json you have:
...
"client": [
...
"oauth_client": [
...
{
"client_id": "YOUR WEB CLIENT ID",
"client_type": 3
}
...
For some reason the file downloaded from firebase console doesn't include it.
After adding the entry in the google-services.json file, everything started working as expected.
google-services-plugin documentation
In addition to the Dexto's Answer I would like to mention one more thing
In the JSON file you will get two kind of client id
One Which is having client_type value 1 and Another with the client_type value 3
Make sure you specified the client_id of client_type which has value of 3
classpath 'com.google.gms:google-services:4.1.0'
has a problem. instead use:
classpath 'com.google.gms:google-services:4.2.0'
Download your newest google-services.json. List of client_id is present for OAuth 2.0 client IDs in your Google Cloud Credentials.
Then check whether it contains client_id with "client_type" : 3 or not. If not, you need to create a new one:
Open the Credentials page in the API Console.
Click Create credentials -> OAuth cliend ID. Then chose type Web application.
Wait 2-3 minutes, refresh Firebase Console & download your google-services.json again. It should contain client_id with "client_type" : 3 now.
Clean & rebuild your project to apply new API config.
The client_id with "client_type" : 3 is usually inside oauth_client tag, not services or other_platform_oauth_client.
If you fall to this case & cannot build the project, try copy your client_id to oauth_client tag and rebuild again.
"client": [
...
"oauth_client": [
...
{
"client_id": "YOUR WEB CLIENT ID",
"client_type": 3
}
]
]
for my case:
The lib was old so I go get the last lib at: https://firebase.google.com/docs/auth/android/firebaseui
put in dependency:
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
along with what currently there
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:26.7.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-auth-ktx'
and it is fixed
Try downloading your .json file again after changing the configuration in the Firebase console. Use this newer configuration file, not the old one.
Fixed after using this link to create my backend id to Google API.
https://developers.google.com/identity/sign-in/android/start-integrating#get_your_backend_servers_oauth_20_client_id
1- Open the Credentials page in the API Console.
2- The Web application type client ID is your backend server's OAuth 2.0 client ID.
After this, you can re-download your json file and android studio will automatically matching your string id.
I know it is late to answer but hope this will help someone in the future.
To access there is no need to hard code default_web_client_id in app.
To access default_web_client_id in Android App from google-services.json, we have to add SHA1 key under FireBase project Settings.
Go to Firebase Console > Open Project > Select App > Add Fingerprint.
After this copy generated google-services.json to project.
After this you will see the difference in json file as below:
Before :
"oauth_client": []
After :
"oauth_client": [
{
"client_id": "23........4-asdj...........................asda.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.abc.xyz",
"certificate_hash": "asjhdashhs"
}
},.....
This will solve your issue.
Generic solution for this is to apply google play services plugin at the end of build.gradle like this
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
buildFeatures {
dataBinding true
}
defaultConfig {
applicationId "xxxxxx"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
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.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// For Common Dimension
implementation 'com.intuit.sdp:sdp-android:1.0.5'
implementation 'com.intuit.ssp:ssp-android:1.0.5'
// Retrofit and Gson
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.6.1'
// Rx Java and Dagger
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'com.google.dagger:dagger:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
compileOnly 'javax.annotation:jsr250-api:1.0'
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
// Glide Image Loading
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'com.android.support:design:30.0.0'
implementation 'com.android.support:recyclerview-v7:30.0.0'
implementation 'com.android.support:cardview-v7:30.0.0'
implementation 'com.android.support:multidex:1.0.3'
/*Jsoup*/
implementation 'org.jsoup:jsoup:1.9.1'
/*Firebase*/
implementation 'com.google.firebase:firebase-core:17.5.0'
implementation 'com.google.firebase:firebase-config:19.2.0'
implementation 'com.google.firebase:firebase-messaging:20.2.4'
implementation 'com.google.firebase:firebase-database:19.3.1'
implementation 'com.google.firebase:firebase-auth:19.3.2'
implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-analytics:17.5.0'
/*location and google map*/
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.gms:play-services-auth:18.1.0'
/*Circle Image View*/
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.github.ittianyu:BottomNavigationViewEx:2.0.4'
implementation "com.android.support:design:30.0.0"
implementation 'com.facebook.android:facebook-android-sdk:5.15.3'
}
apply plugin: 'com.google.gms.google-services'
in my case I forgot to add
id 'com.google.gms.google-services'
to the plugin of build.gradle(:app)
I also had the same issue, make sure "google-services.json" is in your app directory. Then simply rebuild the project from "Build -> Rebuild Project"
Since the string resource "default_web_client_id" is auto-generated, it will be resolved once you rebuild the project
Use BOM to use the compatible version of firebase.
In your module (app-level) Gradle file:
dependencies {
// ...
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:29.3.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
// Declare the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics'
// Declare the dependencies for any other desired Firebase products
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
}
Source: https://firebase.google.com/docs/android/setup?authuser=0&hl=en#add-sdks
Update your project level build.gradle file with the following code:
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
}}
allprojects {
repositories {
google()
jcenter()
maven { url "https://maven.google.com"}
}}
task clean(type: Delete) {
delete rootProject.buildDir }
More Details:answerdone.com
For me the problem was because I was using minSdkVersion 15, updating to 16 has solved my problem.
I have searched for it whole day, I already have pasted that json file inside App and rebuild many times, but the android studio still marks it as error in ide.
Solution: just run the project, ignore the error, it will work, it is an IDE bug.
implementation platform('com.google.firebase:firebase-bom:29.0.0')
implementation 'com.firebaseui:firebase-ui-auth:4.3.2'
Put these lines into build.gradle(projectName)

Categories

Resources