【问题标题】:Multiple google-services.json Per Product Flavour每个产品风格有多个 google-services.json
【发布时间】:2017-10-09 09:20:17
【问题描述】:

我认为这是对该主题提出的其他问题的变体。

我有两种产品口味。我在不同的环境中部署我的应用程序,每个环境都与不同的 Firebase 项目通信。因此,对于每种风味,我都需要能够针对特定环境(开发、测试、生产等)

有没有一种方法,我可以在不引入新产品风味的情况下,制作一种风味的构建变体,选择适当的google-services.json 文件?也许我以错误的方式处理这个问题......

【问题讨论】:

    标签: android firebase config variants


    【解决方案1】:

    我能够做到这一点的唯一方法是绕过使用 google-services.json 并动态创建 FirebaseApp 实例,例如

        if (<is dev>) {
            apiKey = <dev api key>;
            databaseUrl = <dev database url>;
        } else if (<is test> {
            apiKey = <>;
            databaseUrl = <>;
        } else // production {
            apiKey = <>;
            databaseUrl = <>;
        }
    
    
        FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                .setApiKey(apiKey)
                .setApplicationId(context.getString(R.string.google_app_id))
                .setDatabaseUrl(databaseUrl)
                .build();
    
        return FirebaseApp.initializeApp(context, firebaseOptions, "MyApp");
    

    【讨论】:

    • 太棒了。我试试看!
    • 请注意,您需要从使用这种方法的 gradle 文件中删除 apply plugin: 'com.google.gms.google-services' ..这就是处理 google-services.json
    • 是的。最后做了一个款待。感谢您的帮助。
    【解决方案2】:

    首先,将每个 buildType 的相应 google_services.json 放置在以下位置:

    app/src/debug/google_services.json
    app/src/main/google_services.json
    

    现在,让我们在 :app 的 build.gradle 中创建一些 gradle 任务,以自动将相应的 google_services.json 移动到 app/google_services.json

    task switchToDebug(type: Copy) {
        description = 'Switches to DEBUG google-services.json'
        from "src/debug"
        include "google-services.json"
        into "."
    }
    
    task switchToRelease(type: Copy) {
        description = 'Switches to RELEASE google-services.json'
        from "src/release"
        include "google-services.json"
        into "."
    }
    

    很棒 — 但是在构建应用程序之前必须手动运行这些任务很麻烦。我们希望在 :assembleDebug 或 :assembleRelease 运行之前的某个时间运行上述适当的复制任务。让我们看看 :assembleRelease 运行时会发生什么:

    Zaks-MBP:my_awesome_application zak$ ./gradlew assembleRelease
    Parallel execution is an incubating feature.
    .... (other tasks)
    :app:processReleaseGoogleServices
    ....
    :app:assembleRelease
    

    注意 :app:processReleaseGoogleServices 任务。此任务负责处理根 google_services.json 文件。我们希望处理正确的 google_services.json,因此我们必须事先立即运行我们的复制任务。 将此添加到您的 build.gradle。注意 afterEvaluate 封闭。

    afterEvaluate {
        processDebugGoogleServices.dependsOn switchToDebug
        processReleaseGoogleServices.dependsOn switchToRelease
    }
    

    现在,任何时候 :app:processReleaseGoogleServices 被调用,我们新定义的 :app:switchToRelease 都会被预先调用。调试 buildType 的逻辑相同。您可以运行 :app:assembleRelease 并且发布版本 google_services.json 将自动复制到您的应用模块的根文件夹。

    https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e

    【讨论】:

    • 请补充,致谢medium.com/google-cloud/…
    • 感谢您的回复,但我正在尝试配置多个 JSON 配置文件每个构建风格,例如开发、测试和生产(不创造新口味)。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 2017-06-24
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多