【问题标题】:How to use the SDK Firebase (Android) in Nativescript如何在 Nativescript 中使用 SDK Firebase (Android)
【发布时间】:2018-02-23 14:13:54
【问题描述】:

我正在尝试在 Android 上本地使用 sdk Firebase,但是当我尝试使用以下方式初始化 Firebase 时:

com.google.firebase.FirebaseApp.initializeApp(utils.ad.getApplicationContext();

我收到此错误

System.err:错误:java.lang.IllegalStateException:默认 FirebaseApp 未在此过程中初始化 *******。请务必先调用 FirebaseApp.initializeApp(Context)。

在我的 app.gradle 中:

dependencies {
   compile 'com.google.firebase:firebase-core:11.8.0'
   compile 'com.google.firebase:firebase-messaging:11.8.0'
   compile "com.google.android.gms:play-services-base:11.8.0"
}

可以帮助我吗?

【问题讨论】:

    标签: android firebase sdk angular2-nativescript


    【解决方案1】:

    将规则添加到 platforms/android/build.gradle 文件,以包含 google-services 插件和 Google 的 Maven 存储库:

    buildscript {
        // ...
        dependencies {
            // ...
            classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
        }
    }
    
    allprojects {
        repositories {
            // ...
            maven {
                url "https://maven.google.com" // Google's Maven repository
            }
        }
    }
    

    将 firebase 依赖项插入 App_Resources/Android/app.gradle:

    dependencies {
      compile 'com.google.firebase:firebase-core:11.8.0'
      //compile 'com.google.firebase:firebase-messaging:11.8.0'
      //compile "com.google.android.gms:play-services-base:11.8.0"
      // ...
    }
    

    并在App_Resources/Android/app.gradle末尾插入:

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

    最后,如果你遇到错误:

    > File google-services.json is missing. The Google Services Plugin cannot function without it.
    

    手动复制google-services.json文件:

    cp app/App_Resources/Android/google-services.json platforms/android/app/google-services.json
    

    【讨论】:

    • 感谢您的回复。我有这个错误:> 未找到 ID 为“com.google.gms.google-services”的插件。
    • 事实是我不想在 platofroms / android / build.gradle 中添加依赖项,但我只想使用 app.gradle 文件来使用 firebase。这是可能的?我读了指南:)
    • 我不知道是否可以,我个人结束了将platforms/android/build.gradle保存为platforms目录之外的build.gradle.template,并在必要时重新安装。
    • 所以没有动态的东西?
    • 你考虑过nativescript-plugin-firebase吗?
    【解决方案2】:

    同时,非常感谢您的回复@attdona :)

    我通过或多或少地做你所做的来解决。

    唯一的区别是:

    1. 为了使用动态 build.grade,我创建了一个 hook 来替换 用我的原件来编译。
    2. 总是使用 hook 我复制 文件 google-services.jsonApp_Resources/Android/平台/安卓/应用

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2017-10-12
      • 2020-05-13
      • 2020-11-14
      相关资源
      最近更新 更多