在库中实现Firebase

Sek*_*kai 24 android firebase google-cloud-messaging firebase-cloud-messaging

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

Firebase现在要求提供应用程序ID,但我正在库中实现它,因此没有应用程序ID.

我怎样才能实现能够向使用我的图书馆的应用程序发送通知的目标?

提前致谢.

Joa*_*Ley 10

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

buildConfigField("String", "FIREBASE_APP_KEY", "\"${firebaseAppKey}\"")
Run Code Online (Sandbox Code Playgroud)

然后在你的项目里面 gradle.properties

firebaseAppKey = <yourFirebaseAppSecret>;

对于每个项目/应用程序,您必须在您的上定义此变量gradle.properties.

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

当您想要访问此环境变量值时使用BuildConfig.FIREBASE_APP_KEY (例如,实例化firebase).


Sak*_*boy 9

这些都是有点hacky或太多的工作,这是一个很好的简单例子(虽然具有讽刺意味,因为它是一个很长的帖子 - 但值得).

可以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.
Run Code Online (Sandbox Code Playgroud)

这就是您在图书馆项目中所需要做的一切.请勿在此处应用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()
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要设置消费应用程序模块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'
Run Code Online (Sandbox Code Playgroud)

有些事情需要注意:

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

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

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

更新:

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


Sir*_*lot 6

我知道这是一个老问题,答案是可以接受的,但是所有答案都有很大的缺点-除了将您的库添加到他们的应用程序之外,它们还需要您库的用户来做。如果要从Maven存储库下载库,则可以通过一种方法完全不干扰库的用户

注意:此方法是黑客行为,Firebase不支持。当被问及Firebase支持时,我得到以下答复:

Firebase SDK不适用于库项目。Firebase上可用的功能已集成到应用程序级别,而不是基于每个模块或每个库,因此无法或不支持将其集成到库项目中的用例。

不过,我已经找到了一种方法,并且有人会觉得它很有用,所以这里是:

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

在项目的主要build.gradle添加mavenCentral存储库中:

allprojects {
    repositories {
        ...
        mavenCentral()
    }
}
Run Code Online (Sandbox Code Playgroud)

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

compile 'com.google.android.gms:play-services-gcm:11.0.4'
Run Code Online (Sandbox Code Playgroud)

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

compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.google.firebase:firebase-database:11.0.4'
Run Code Online (Sandbox Code Playgroud)

将您的SDK注册为Firebas上的项目,下载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>
Run Code Online (Sandbox Code Playgroud)

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

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


Art*_*son 5

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