Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first
Asked Answered
L

8

10

I am trying to add data to firestore. and also i want to do this using dagger. but i keep getting this error. can you help me please......

** Default FirebaseApp is not initialized in this process com.example.vvvv. Make sure to call FirebaseApp.initializeApp(Context) first. **

FirestoreREpository

class FirestoreRepository @Inject constructor(private val usersRef: 
CollectionReference) {

fun saveSearchResults(userEntities: List<UserEntity>) {
    usersRef.add(userEntities).addOnCompleteListener { task ->
        when (task.isSuccessful) {
            true ->  Log.d(ContentValues.TAG, "added:success")
            false ->  Log.d(ContentValues.TAG, "added:unsuccess")
        }
    }
  }
 }

ViewModel

@HiltViewModel
class UserListViewModel @Inject constructor(private val repository: RoomRepository, private 
val firestoreRepository: FirestoreRepository) :
ViewModel() {

private val _users = MutableLiveData<List<User>>()

val users: LiveData<List<User>>
    get() = _users

var userData: MutableLiveData<List<User>> = MutableLiveData()

fun userSearch(term: String) {
    viewModelScope.launch {
        loadFromCache(term)
        val getPropertiesDeferred = 
  GithubApi.retrofitService.searchUser(term)
        try {
            val userEntities: MutableList<UserEntity> = mutableListOf()
            val result = getPropertiesDeferred.body()
            _users.value = result?.users
            result?.users?.forEach {
                userEntities.add(
                    UserEntity(
                        term = term,
                        login = it.login,
                        avatar_url = it.avatar_url
                    )
                )
            }
            clearSearchResults(term)
            updateSearchResults(userEntities, term)
            firestoreRepository.saveSearchResults(userEntities) //save 
data with firebase
        } catch (e: Exception) {
            Log.e("userListErr", e.message.toString())
        }
    }
}

private fun updateSearchResults(userEntities: List<UserEntity>, term: 
String) {
    viewModelScope.launch(Dispatchers.IO) {
        repository.insertSearchResults(userEntities)
        loadFromCache(term)
    }
}

private fun loadFromCache(term: String) {
    viewModelScope.launch(Dispatchers.IO) {
        val list = repository.getSearchResults(term)
        userData.postValue(list)
    }
}

private fun clearSearchResults(term: String) {
    viewModelScope.launch(Dispatchers.IO) {
        repository.deleteSearchResults(term)
    }
   }
}

AppModule

@Module
@InstallIn(SingletonComponent::class)
object APPModule {

@Singleton
@Provides
fun getAppDB(context: Application): AppDatabase {
    return AppDatabase.getAppDB(context)
}

@Singleton
@Provides
fun getDao(appDB: AppDatabase): AppDao {
    return appDB.getDAO()
}

@Provides
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()

@Provides
fun provideUsersRef(db: FirebaseFirestore) = db.collection("users")
}
Linder answered 13/4, 2022 at 13:58 Comment(4)
Show us the content of your AppModule, where you create an object of FirebaseFirestore.Predella
I haven't created the app module Firebase :/ where should i call this after creatingLinder
@AlexMamo hi alex. I updated my code again after reviewing your projects. dagger is a difficult topic for me to understand. can you please help. where is my mistake –Linder
It looks much better now. Good to hear that you solved it.Predella
L
-1

I used multi-module in this project. id com.google.gms.google-services to app directory and solved.

Linder answered 13/4, 2022 at 20:13 Comment(2)
Please elaborate your answer. what steps you tookNashner
Your short answer and unobvious is misleading, it should be removed unless it's more clarified !Varanasi
D
6

The error occurred when I upgraded to the latest version. There is definitely a better solution, but for now I fixed the error this way. I downgraded "com.google.gms:google-services".

dependencies {
    classpath 'com.android.tools.build:gradle:7.3.1'
    classpath 'com.google.gms:google-services:4.4.0'
}

To:

dependencies {
    classpath 'com.android.tools.build:gradle:7.3.1'
    classpath 'com.google.gms:google-services:4.3.15'
}
Dungeon answered 4/10, 2023 at 12:42 Comment(2)
As of May 2024, the same problem persists, but with version 4.4.1. Downgrading the version instantly resolved the issueFraase
And as of July 2024, the same problem persists with version 4.4.2.Inartificial
O
4
  1. In build.gradle(project) in dependencies add the following:

      classpath 'com.google.gms:google-services:4.3.5'
    
  2. In build.gradle(module) add the following:

    plugins {
    id 'com.google.gms.google-services'
     }
    
  3. in your last line of code add the following:

    apply plugin: 'com.google.gms.google-services'
    
Outclass answered 2/10, 2022 at 17:50 Comment(0)
A
4

you can just use version 4.3.15 . I have this problem and I solved it by changing the version to "com.google.gms:google-services:4.3.15"

Abrahan answered 26/1 at 10:23 Comment(0)
P
1

Worked for me After downgrade google version classpath 'com.google.gms:google-services:4.3.15'

Premonish answered 8/11, 2023 at 10:55 Comment(0)
A
1

It seems that classpath 'com.google.gms:google-services:4.4.1' has an issue. I downgraded version 4.4.1 to 4.3.10 and it's working without error. You can downgrade or upgrade to another version and try, I think it will work. Thanks

Assiduity answered 28/2 at 7:17 Comment(0)
J
0

I meet this problem, because google-service sdk version not work with gradle version.

Maybe you can try with this direction.

The original project build.gradle config is ok.

buildscript {
   dependencies {
          classpath "com.android.tools.build:gradle:3.2.1"
          classpath 'com.google.gms:google-services:4.1.0'

Finally, it's solve by find compatible SDK version.

buildscript {
   dependencies {
          classpath "com.android.tools.build:gradle:3.3.3"
          classpath 'com.google.gms:google-services:4.2.0'
Jorin answered 2/2, 2023 at 2:29 Comment(0)
L
0

I also solved it by downgrading to version 4.3.15

       classpath 'com.google.gms:google-services:4.3.15'
Luminary answered 11/5 at 18:59 Comment(0)
L
-1

I used multi-module in this project. id com.google.gms.google-services to app directory and solved.

Linder answered 13/4, 2022 at 20:13 Comment(2)
Please elaborate your answer. what steps you tookNashner
Your short answer and unobvious is misleading, it should be removed unless it's more clarified !Varanasi

© 2022 - 2024 — McMap. All rights reserved.