简体   繁体   English

在多模块项目中使用 Firebase

[英]Using Firebase in a multi-module project

I have a multi-module multi-flavor android project, the modules are as follows:我有一个多模块多口味的android项目,模块如下:

  1. core module, which is an android library that holds common stuff, including library dependencies. core模块,这是一个 android 库,其中包含常见的东西,包括库依赖项。
  2. authentication module, which is as the name suggests, a module that contains a bunch of UI activities and is responsible for authenticating users. authentication模块,顾名思义,一个包含一堆UI活动的模块,负责对用户进行身份验证。 It's also the module that has the launcher activity.它也是具有启动器活动的模块。
  3. user module, which is another android library module. user模块,这是另一个android库模块。 It is responsible for handling user profile UI, user data, along with Firebase database.它负责处理用户配置文件 UI、用户数据以及 Firebase 数据库。 But it also has to deal with Firebase authentication to get the Uid as it's used as key in the database.但它还必须处理 Firebase 身份验证才能获取 Uid,因为它在数据库中用作密钥。
  4. Other modules that are irrelevant right now目前不相关的其他模块

The module core does nothing with respect to Firebase, except just include it as a dependency for other modules.模块core对 Firebase 没有任何作用,只是将其作为其他模块的依赖项包含在内。 So I have this in my project build.gradle :所以我在我的项目build.gradle有这个:

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

and I also have this in my core build.gradle :我的核心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'
}

The user module does nothing except import the core library, and use authentication right away. user模块除了导入core库之外什么都不做,并立即使用身份验证。 So I have the following in my user build.gradle :所以我的用户build.gradle有以下build.gradle

dependencies {
    otherStuff
    implementation project(':core')
}

and then I proceed to use it in my classes like so:然后我继续在我的课程中使用它,如下所示:

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

The authentication module is the one that defines the app name, the launcher activity, etc. So, in my mind at least, it's also the one that should have the google-services.json file. authentication模块是定义应用程序名称、启动器活动等的模块。因此,至少在我看来,它也是应该具有google-services.json文件的模块。 So it has it under the authentication folder.所以它在authentication文件夹下。

It includes both core and user libraries, so in my authentication build.gradle I have the following:它包括core库和user库,因此在我的身份验证build.gradle我有以下内容:

dependencies {
    otherStuff
    implementation project(':core')
    implementation project(':user')
}

It also proceeds to use Firebase authentication for logging in and signing up users.它还继续使用 Firebase 身份验证来登录和注册用户。

Now with the problem:现在有问题:

trying to build the project, I get the following error:尝试构建项目时,出现以下错误:

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

and if I try to copy the google-services.json under core too, I get the following error:如果我也尝试复制core下的google-services.json ,我会收到以下错误:

No matching client found for package name 'com.companyname.projectname.core'

the google-services.json file has the app name defined inside it as: google-services.json文件将应用程序名称定义为:

"package_name": "com.companyname.projectname.appname"

which obviously expects the app name and not core .这显然需要应用程序名称而不是core

So how to include Firebase in this setting?那么如何在此设置中包含 Firebase? I want to keep the firebase dependencies defined inside core because multiple modules will use it.我想保留core内部定义的 firebase 依赖项,因为多个模块将使用它。 At the same time, authentication is the one that actually defines the appname and is also the one that has the launcher activity in which I start to use Firebase.同时, authentication是一个实际上定义了appname ,也是有在我开始使用火力地堡发射活动之一。 I also expect user and any other modules to be able to use Firebase after that.我还希望user和任何其他模块在此之后能够使用 Firebase。 It doesn't make sense to me to register all modules with Firebase (not even sure if it's possible since Firebase expects an app not a library?!).向 Firebase 注册所有模块对我来说没有意义(甚至不确定是否可能,因为 Firebase 期望应用程序而不是库?!)。 So is there anyway to fix this issue?那么有没有办法解决这个问题?

I managed to wrap firebase dependencies into a library module without polluting the main module.我设法在不污染主模块的情况下将 firebase 依赖项包装到一个库模块中。 Here is what I did:这是我所做的:

google-play-services plugin requires the module that includes it to have applicationId and its JSON file(or it won't compile). google-play-services插件要求包含它的模块具有 applicationId 及其 JSON 文件(否则将无法编译)。

If you're sure you don't use special functions that google-play-services plugin provides, you can delete them.如果您确定不使用google-play-services插件提供的特殊功能,您可以删除它们。

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

Then you can follow this article's instructions:然后您可以按照本文的说明进行操作:

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

Update: Another Link更新:另一个链接

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

Because this provider is just reading resources with known names, another option is to add the string resources directly to your app instead of using the Google Services gradle plugin.由于此提供程序只是读取具有已知名称的资源,因此另一种选择是将字符串资源直接添加到您的应用程序中,而不是使用 Google 服务 gradle 插件。

you may put a google-services.json file in flavor accoding to use:-您可以将 google-services.json 文件放入风味编码中以使用:-

check this link检查此链接

android {
 // set build flavor here to get the right gcm configuration.
    //def myFlavor = "flavor1"
    def myFlavor = "flavor2"

    if (myFlavor.equals("flavor1")) {
        println "--> flavor1 copy!"
        copy {
            from 'src/flavor1/'
            include '*.json'
            into '.'
        }
    } else {
        println "--> flavor2 copy!"
        copy {
            from 'src/flavor2/'
            include '*.json'
            into '.'
        }
    }

    // other stuff
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 gradle DSL 在多模块项目中声明风味依赖项 - Declaring flavor dependencies in multi-module project using gradle DSL Android多模块项目因TypeNotPresentException失败 - Android multi-module project fails with TypeNotPresentException 在多模块项目中集成 Crashlytics - Integrating Crashlytics in a multi-module project 使用 Jacoco 测量多模块和动态功能模块 Android 项目中的测试覆盖率 - Measuring tests coverage in multi-module and dynamic feature module Android project using Jacoco 在多模块应用程序中使用 koin - Using koin in Multi-module application 使用单个命令从多模块项目运行所有 UI/单元测试 - Run all UI/Unit tests from a multi-module project using a single command 有没有办法使用导航组件在Android多模块项目中创建隐式深层链接 - Is there a way to create implicit deeplink in Android Multi-module project using Navigation Component RobolectricTestRunner在多模块Maven项目中找不到清单文件 - RobolectricTestRunner cannot find manifest file in multi-module maven project 在多模块android studio项目中集成.aar文件 - Integrate .aar file in multi-module android studio project Intellij IDEA 13.1.13中具有Android + Gradle的多模块项目 - Multi-module project in Intellij IDEA 13.1.13 with Android+Gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM