【问题标题】:How to configure firebase in Android's multi module application如何在 Android 的多模块应用程序中配置 firebase
【发布时间】:2021-02-05 22:18:36
【问题描述】:

我有一个带有多模块的 Android 应用程序。

  1. app - 使用 firebase 的 Analytics 和 crashlytics 的实际应用。
  2. data_source - 一个使用 firebase 的 crashlytics 和 firestore 的 android 模块。

当我在两个模块中配置 firebase 时,我需要将 google-services.json 文件粘贴到两个模块中。但是由于 firebase 项目首先配置了第一个模块的 ID。它不允许我在 data_source 模块中粘贴相同的 google-services.json 文件。

我得到的错误是 -> “没有为包名 'com.myproject.data_source' 找到匹配的客户端”

如何在多模块应用中使用 Firebase 服务。我总共有 4 个模块,并且所有模块都根据需要使用不同的 firebase 服务(Crashlytics 作为一个常见模块)。

【问题讨论】:

  • @chef417 这没有帮助。如果我按照这种方式进行操作,我将无法在所有模块中使用 crashlytics 或分析。我实际上找到了一个简单的方法。我将在此处添加它作为答案。

标签: android firebase


【解决方案1】:

这很简单。

下载 google-services.json 文件并粘贴到您在 firebase 中定义的模块(包名)中(在我的例子中是 com.myproject.app)。

现在可以在任何其他模块中使用 firebase。

  1. 确保您已在依赖项块的根 build.gradle 文件中添加以下行。

    classpath 'com.google.gms:google-services:4.3.3'
    
  2. 在 build.gradle 文件的其他模块依赖项块中添加所需的 firebase 服务。 在我的情况下,Firestore(添加任何你想要的)。

    implementation 'com.google.firebase:firebase-firestore:21.4.3'
    implementation 'com.google.firebase:firebase-firestore-ktx:21.4.3'
    
  3. 现在,当您想在其他模块中使用 Firebase 服务时

一些CommonObjectClass

    //You can find all the below details in the google-services.json file, NOTE: All details are fake and random created API keys, please use your in the downloaded file.
    val options = FirebaseOptions.Builder()
        .setProjectId("yourfirebaseprojectname")
        .setApplicationId("1:1083305160395:android:48c09b2789790789324g92734")
        .setApiKey("AIzaKJAHDK8ASD7FAODF9A9DSF0AF908ADSF")
        .build()

    Firebase.initialize(context, options, "yourfirebaseprojectname")

在我的情况下,您需要实例的类。用户数据存储

    //Analytics
    FirebaseAnalytics.getInstance(context)

    /Firestore
    val db = FirebaseFirestore.getInstance()

    val docRef = db.collection("Users").document()

...休息一下,像往常一样。只需配置初始化部分即可。

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多