在库中实施 Firebase

Implement Firebase inside of a Library

我想在我想在许多应用程序中用作 SDK 的库中实现 Friebase 通知系统。

Firebase 现在要求提供 App ID,但我是在库中实现它,因此没有 App ID。

我如何才能实现向使用我的库的应用程序发送通知的目标?

提前致谢。

一个选择是让您的库的用户创建一个 Firebase 项目,然后将生成的 google-services.json 文件传递​​到他们的应用程序中,然后您的库就可以依赖它。

是的,您实际上可以这样做,在您的库中 build.gradle 将其放入 defaultConfig 字段中

buildConfigField("String", "FIREBASE_APP_KEY", "\"${firebaseAppKey}\"")

然后在你项目的 gradle.properties

firebaseAppKey = <yourFirebaseAppSecret>;

对于 each project/app 你必须在你的 gradle.properties 上定义这个变量。

您必须为每个项目创建一个 Firebase 应用程序,但您的库现在可以拥有 Firebase SDK。

当你想访问这个环境变量值时使用BuildConfig.FIREBASE_APP_KEY (例如实例化 firebase)。

我知道这是一个有公认答案的老问题,但所有答案都有一个很大的缺点 - 除了将您的库添加到他们的应用程序之外,它们还要求您的图书馆的用户做一些工作。如果您的库是从 Maven 存储库下载的,那么有一种方法可以做到这一点 根本不会打扰您的库的用户

注意:此方法是 hack,不受 Firebase 支持。当询问 Firebase 支持时,我得到以下回复:

Firebase SDKs are not intended for library projects. The features available on Firebase were integrated in an application level and not on a per module or per library basis so, the use case for having this integrated on a library project is not possible or not supported.

尽管如此,我找到了一种方法,也许有人会发现它很有用,所以这里是:

这是使用实时数据库的示例,但它应该适用于所有 Firebase SDK。

在你项目的主 build.gradle 添加 mavenCentral 仓库:

allprojects {
    repositories {
        ...
        mavenCentral()
    }
}

在您的库项目的 build.gradle 中,添加 Google Play 服务(作为依赖项,而不是插件):

compile 'com.google.android.gms:play-services-gcm:11.0.4'

添加相关的 Firebase SDK(与 Google Play 服务版本相同):

compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.google.firebase:firebase-database:11.0.4'

将您的 SDK 注册为 Firebase 上的项目,下载它 google-services.json 并使用任何文本编辑器打开它。

在您图书馆的 strings.xml 中添加以下行,并使用来自 google-services.json

的数据填充这些行
<string name="gcm_defaultSenderId">project_number</string>
<string name="google_api_key">current_key</string>
<string name="google_app_id">mobilesdk_app_id</string>
<string name="google_crash_reporting_api_key">current_key</string>
<string name="google_storage_bucket">storage_bucket</string>
<string name="firebase_database_url">firebase_url</string>
<string name="default_web_client_id">client_id</string>
<string name="project_id">project_id</string>

就是这样。您可以在您的 libaray 中使用 Firebase 实时数据库,然后构建它并将其发布到 Maven(发布到 Maven 是必不可少的,否则您的库的用户将不得不手动添加依赖项)。从应用程序内部激活时,将使用您的数据库。

请注意,如果您的图书馆的用户将使用 Google Play 服务或 Firebase,此方法可能会导致异常和意外行为,因此使用风险自负!

这些都有些老套或工作量太大,这里有一个很好的简单示例(虽然具有讽刺意味,因为它很长 post -- 但值得)。

可以在您的库项目中使用 FireBase 代码,当然,消费应用程序需要注册应用程序并获取应用程序 ID / google-services.json 文件。

但是您的图书馆没有,也不应该关心这个,这是消费应用程序的工作,而不是您的图书馆。

这是一个在库项目中使用 firebase-messaging 模块的简短示例。

YourLibrary 模块的 build.gradle:

// Other typical library set up 
apply plugin: 'com.android.library'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName '1.0'

        // Don’t for get your library’s proguard file!
        consumerProguardFiles 'proguard-rules.pro'
    }
}

ext {
    currentFirebaseVersion = "11.8.0"
}

dependencies {
    /* 
    Here we depend on the firebase messaging dependency (via compileOnly),
    allowing us to use the FireBase API within our library module.

    I exclude that org.json module because it may cause build warnings, this 
    step isn’t totally necessary.

    NOTE: You should use `compileOnly` here so the dependency is
    not added to the build output You will be allowed to use the 
    dependency in your library. If the consuming app wants to use firebase
    they’ll need to depend on it (using `implementation`).
    */
    compileOnly("com.google.firebase:firebase-messaging:$currentFirebaseVersion") {
        exclude group: 'org.json', module: 'json'
    }
}

// Other typical library set up. But nothing else relating Firebase.

这就是您在图书馆项目中需要做的全部工作。 不要在此处应用 gms 插件,并且不要将google-services 类路径添加到库build.gradle.

下面是设置消费应用程序的方法:

MyClientApp 的顶级 build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google() // You know the drill...
    }
    // Any other set up you might have...

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        /*
        Here in your client app’s top-level build.gradle you add the
        google-services to the app’s classpath.
        */
        classpath 'com.google.gms:google-services:3.2.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
// Other basic stuff...
allprojects {
    apply plugin: 'maven'
    apply plugin: 'maven-publish'

    repositories {
        jcenter()
        google()
    }
}

现在我们需要设置消费应用程序模块build.gradle,很简单。我们几乎只需要应用插件,并依赖于我们创建的包含所有 FireBase 代码的库模块。

MyClientApp的模块级别build.gradle:

buildscript {
    repositories {
        google()
        mavenLocal()
    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.your.application.that.can.use.firebase"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName '1.0'
    }
    //other typical set up
}

ext {
    currentFirebaseVersion = "11.8.0"
}

dependencies {
    implementation('com.your.library:YourLibrary:1.0@aar') {
        transitive = true
        // Use the consuming application's FireBase module, so exclude it
        // from the dependency. (not totally necessary if you use compileOnly
        // when declaring the dependency in the library project).
        exclude group: 'com.google.firebase'
        // Exclude the "plain java" json module to fix build warnings.
        exclude group: 'org.json', module: 'json'
    }

    implementation("com.google.firebase:firebase-messaging:$currentFirebaseVersion") {
        // Exclude the "plain java" json module to fix build warnings.
        exclude group: 'org.json', module: 'json'
    }
}

// Needs to be at the bottom of file.
apply plugin: 'com.google.gms.google-services'

一些注意事项:

  • 必须在底部应用google-services插件(仅在客户端模块build.gradle)。
  • 依赖于包含 FireBase 代码的库模块,但排除它的 FireBase 模块版本,支持你自己的依赖版本。
  • 应用程序取决于它自己的 FireBase 版本。
  • classpath 'com.google.gms:google-services:3.1.1’ 仅进入客户端应用程序的顶级 build.gradle
  • 当然您需要注册客户端应用程序并将 google-services.json 放入客户端应用程序的项目中。
  • 在您的应用清单中定义必要的 Firebase Service(或使用清单合并并从您的库项目中合并它们)
  • google_play_services_version 元数据标记添加到客户端应用程序的清单中。
  • 库 can/should 在声明 FireBase 依赖项时使用 compileOnly

现在您将能够在您的应用程序中使用您在库中定义的使用 FireBase 的代码。或者您可以让您的库模块完成所有 FireBase 工作!

当然,这通常用于内部库,因为像 Firebase 这样的框架并非设计用于在库模块中实现,但有时您需要这样做,所以这是一个简单的 non-hacky/sane问题的解决方案。它可以用于通过 maven 分发的项目——我的库使用它,并且它从未引起任何问题。

更新:

在声明库模块的 Firebase 依赖项时,您应该使用 compileOnly。通过这样做,依赖项将不会添加到构建输出中。但是你将被允许在你的库中使用依赖项。如果消费应用想要使用 firebase,他们将需要手动依赖它(使用 implementation)。这将有助于减少应用程序中不需要的 dependencies/bloat 以及声明这样的依赖项的“正确”方式。 注意:您可能需要执行运行时检查以确保库在您的模块中使用它的代码之前可用。

Firebase Multiple Project

请参考link它有所有信息