在多模块项目中使用Firebase

时间:2019-02-01 05:52:11

标签: android firebase gradle android-gradle firebase-authentication

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

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

模块core对Firebase无效,只是将其作为其他模块的依赖项包含在内。因此,我在我的项目 build.gradle中有了它:

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

,而且我的核心 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'
}

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

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

然后我继续在类中使用它,

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

authentication模块是定义应用程序名称,启动器活动等的模块。因此,至少在我看来,它也是应具有google-services.json文件的模块。因此它位于authentication文件夹下。

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

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

它还将继续使用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

如果我也尝试在google-services.json下复制core,则会出现以下错误:

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

google-services.json文件中的应用名称定义为:

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

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

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

2 个答案:

答案 0 :(得分:1)

我设法将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

答案 1 :(得分:0)

您可以将google-services.json文件进行风味编码以使用:-

  

选中此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
    }