在多模块项目中使用 Firebase

Pro*_*oWi 5 android gradle firebase android-gradle-plugin firebase-authentication

我有一个多模块多口味的android项目,模块如下:

  1. core 模块,这是一个 android 库,其中包含常见的东西,包括库依赖项。
  2. authentication模块,顾名思义,一个包含一堆UI活动的模块,负责对用户进行身份验证。它也是具有启动器活动的模块。
  3. user模块,这是另一个android库模块。它负责处理用户配置文件 UI、用户数据以及 Firebase 数据库。但它还必须处理 Firebase 身份验证才能获取 Uid,因为它在数据库中用作密钥。
  4. 目前不相关的其他模块

该模块core对 Firebase 没有任何作用,只是将其包含为其他模块的依赖项。所以我在我的项目中 有这个build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.0'
    classpath 'com.google.gms:google-services:4.1.0'
}
Run Code Online (Sandbox Code Playgroud)

我的核心 也有这个build.gradle

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

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        otherstuff
    }
    otherStuff
}

dependencies {
    otherStuff
    api 'com.google.android.gms:play-services-base:16.1.0'
    api 'com.google.firebase:firebase-core:16.0.6'
    api 'com.google.firebase:firebase-auth:16.1.0'
    api 'com.google.firebase:firebase-database:16.0.6'
    api 'com.google.firebase:firebase-storage:16.0.5'
    api 'com.hbb20:ccp:2.2.3'
    api 'com.google.android.libraries.places:places:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

user模块除了导入core库之外什么都不做,并立即使用身份验证。所以我的用户中 有以下内容build.gradle

dependencies {
    otherStuff
    implementation project(':core')
}
Run Code Online (Sandbox Code Playgroud)

然后我继续在我的课程中使用它,如下所示:

FirebaseAuth auth = FirebaseAuth.getInstance();

boolean isLoggedIn()
{
    assert(auth != null);
    return (auth.getCurrentUser() != null);
}

void uploadUserImpl()
{
    assert(isLoggedIn());
    db.getReference("users").child(auth.getCurrentUser().getUid()).setValue(this);
}

etc
Run Code Online (Sandbox Code Playgroud)

authentication模块是定义应用程序名称、启动器活动等的模块。因此,至少在我看来,它也是应该包含该google-services.json文件的模块。所以它在authentication文件夹下。

它包括coreuser库,因此在我的身份验证中, build.gradle我有以下内容:

dependencies {
    otherStuff
    implementation project(':core')
    implementation project(':user')
}
Run Code Online (Sandbox Code Playgroud)

它还继续使用 Firebase 身份验证来登录和注册用户。

现在有问题:

尝试构建项目时,出现以下错误:

File google-services.json is missing. The Google Services Plugin cannot function without it. 
 Searched Location: 
/work/mewais/CompanyName/ProjectName/core/src/FlavorName/debug/google-services.json
/work/mewais/CompanyName/ProjectName/core/google-services.json
Run Code Online (Sandbox Code Playgroud)

如果我尝试复制google-services.jsoncore过,我得到以下错误:

No matching client found for package name 'com.companyname.projectname.core'
Run Code Online (Sandbox Code Playgroud)

google-services.json文件将应用程序名称定义为:

"package_name": "com.companyname.projectname.appname"
Run Code Online (Sandbox Code Playgroud)

这显然需要应用程序名称而不是core.

那么如何在此设置中包含 Firebase?我想保留内部定义的 firebase 依赖项,core因为多个模块将使用它。同时,authentication是实际定义的那个appname,也是我开始使用 Firebase 的启动器活动的那个。我也希望user和任何其他模块能够在此之后使用 Firebase。向 Firebase 注册所有模块对我来说没有意义(甚至不确定是否可能,因为 Firebase 期望应用程序而不是库?!)。那么有没有办法解决这个问题?

Dew*_*eed 4

我设法将 firebase 依赖项包装到库模块中,而不会污染主模块。这是我所做的:

google-play-services插件要求包含它的模块具有 applicationId 及其 JSON 文件(否则它将无法编译)。

如果您确定不使用google-play-services插件提供的特殊功能,可以将其删除。

https://developers.google.com/android/guides/google-services-plugin

然后您可以按照本文的说明进行操作:

https://medium.com/@samstern_58566/how-to-use-firebase-on-android-without-the-google-services-plugin-93ecc7dc6c4

更新:另一个链接

https://firebase.google.com/docs/projects/multiprojects#support_multiple_environments_in_your_android_application

由于此提供程序只是读取已知名称的资源,因此另一个选择是将字符串资源直接添加到您的应用程序中,而不是使用 Google Services gradle 插件。