gpt4 book ai didi

android - 有没有办法使用 Firebase App Distribution 为每个 Android 构建变体配置单独的 serviceCredentialsFile?

转载 作者:行者123 更新时间:2023-12-02 15:39:38 26 4
gpt4 key购买 nike

我们有多个 Firebase 项目,它们通过构建类型和风格共享相同的代码库。我们的目标是通过 Gradle 使用应用程序分发并使用服务帐户凭据进行身份验证。

docs ,表明可以使用 firebaseAppDistribution block 来配置参数,服务凭证文件路径就是其中之一。由于每个变体都是一个 Firebase 项目,并且每个项目都有自己的服务凭证,因此据我所知,我们需要在 Gradle 配置中指向单独的服务凭证文件路径。

我尝试根据变体使用 gradle 任务更新文件路径,但无法使其工作。当前的构建文件如下所示:

...

apply plugin: 'com.google.firebase.appdistribution'

class StringExtension {
String value

StringExtension(String value) {
this.value = value
}

public void setValue(String value) {
this.value = value
}

public String getValue() {
return value
}
}

android {

...

productFlavors.whenObjectAdded {
flavor -> flavor.extensions.create("service_key_prefix", StringExtension, '')
}

productFlavors {

flavor1 {
...
service_key_prefix.value = "flavor1"
}

flavor2 {
...
service_key_prefix.value = "flavor2"
}
}

buildTypes {

debug {
firebaseAppDistribution {
releaseNotesFile = file("internal_release_notes.txt").path
groupsFile = file("group_aliases_debug_fb.txt").path
}
}

release {
firebaseAppDistribution {
releaseNotesFile = file("release_notes.txt").path
groupsFile = file("group_aliases_prod_fb.txt").path
}
}
}
}

...

android.applicationVariants.all { variant ->

task("firebaseCredentials${variant.name.capitalize()}", overwrite: true) {
variant.productFlavors.each { flavor ->

doLast {
firebaseAppDistribution {

def serviceKeyFile = file(
"../${flavor.service_key_prefix.value}-${variant.buildType.name}-service-key.json")
if (serviceKeyFile != null) {
serviceCredentialsFile = serviceKeyFile.path
}
}
}
}
}
}

android.applicationVariants.all { variant ->
def distTask = tasks.named("appDistributionUpload${variant.name.capitalize()}")
def configTask = tasks.named("firebaseCredentials${variant.name.capitalize()}")
distTask.configure {
dependsOn(configTask)
}
}

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

任务似乎运行正确,但我猜文件路径未更新,因为当我运行 appDistributionUpload 时它仍然给出以下错误:

Could not find credentials. To authenticate, you have a few options:

  1. Set the serviceCredentialsFile property in your gradle plugin

  2. Set a refresh token with the FIREBASE_TOKEN environment variable

  3. Log in with the Firebase CLI

  4. Set service credentials with the GOOGLE_APPLICATION_CREDENTIALS environment variable

关于如何实现这样的分发配置有什么想法吗?

最佳答案

联系支持团队后,我了解到此配置尚未开箱即用,并且目前作为功能请求存在。

以下是 Firebase 支持团队提供的解决方法,该解决方法会在每个上传任务开始时修改内存中的相关环境变量:

android {

applicationVariants.all { variant ->

final uploadTaskName = "appDistributionUpload${variant.name.capitalize()}"
final uploadTask = project.tasks.findByName(uploadTaskName)

if (uploadTask != null) {
uploadTask.doFirst {
resetCredentialsCache()
final value = "$projectDir/src/${variant.name}/app-distribution-key.json"
setEnvInMemory('GOOGLE_APPLICATION_CREDENTIALS', value)
}
}
}
}

private static void setEnvInMemory(String name, String val) {
// There is no way to dynamically set environment params, but we can update it in memory
final env = System.getenv()
final field = env.getClass().getDeclaredField("m")
field.setAccessible(true)
field.get(env).put(name, val)
}

private static void resetCredentialsCache() {
// Google caches credentials provided by environment param, we are going to reset it
final providerClass = Class.forName(
'com.google.firebase.appdistribution.buildtools.reloc.com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider')
final providerCtor = providerClass.getDeclaredConstructor()
providerCtor.setAccessible(true)
final provider = providerCtor.newInstance()

final credsClass = Class.forName(
'com.google.firebase.appdistribution.buildtools.reloc.com.google.api.client.googleapis.auth.oauth2.GoogleCredential')
final field = credsClass.getDeclaredField('defaultCredentialProvider')
field.setAccessible(true)
field.set(null, provider)
}

编辑:上述解决方案仅适用于应用分发插件版本 1.3.1。

我没有进一步联系支持人员,而是在 Jenkins CI 上使用了 GOOGLE_APPLICATION_CREDENTIALS 环境变量,如所述 here

关于android - 有没有办法使用 Firebase App Distribution 为每个 Android 构建变体配置单独的 serviceCredentialsFile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59560760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com