gpt4 book ai didi

android - Android Studio/Gradle 中的 productFlavor 变量

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:46 24 4
gpt4 key购买 nike

我有一个具有以下产品风格的 android 应用程序:

productFlavors {
local {

}

development {


}

production {

}
}

然后在我的 build.grade 底部添加以下内容:

File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
android.applicationVariants.all { variant ->
def variantSuffix = variant.name.capitalize()
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
properties.put("apiSecret", WHAT_GOES_HERE)
properties.put("apiKey", WHAT_GOES_HERE)
PropertiesUtils.injectPropertyInFile(crashlyticsProperties, properties, "")
}
generateResourcesTask.dependsOn generatePropertiesTask
}

我正在尝试为 crashlytics 设置 api secret/key,但我需要能够根据我正在构建的产品风格来设置这些。

properties.put("apiSecret", WHAT_GOES_HERE)
properties.put("apiKey", WHAT_GOES_HERE)

如何设置/获取这些变量?

更新#1

我已将以下内容添加到我的 build.gradle

productFlavors {
local {
buildConfigField "String", "CRASHLYTICS_API_SECRET", "1234"
buildConfigField "String", "CRASHLYTICS_API_KEY", "1234"
}

development {
buildConfigField "String", "CRASHLYTICS_API_SECRET", "1234"
buildConfigField "String", "CRASHLYTICS_API_KEY", "1234"
}

production {
buildConfigField "String", "CRASHLYTICS_API_SECRET", "1234"
buildConfigField "String", "CRASHLYTICS_API_KEY", "1234"
}
}

然后在 build.gradle 文件的底部我有:

File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
android.applicationVariants.all { variant ->
def variantSuffix = variant.name.capitalize()
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
println "...copying apiSecret for ${variant.name}"
properties.put("apiSecret", BuildConfig.CRASHLYTICS_API_SECRET)
println "...copying apiKey for ${variant.name}"
properties.put("apiKey", BuildConfig.CRASHLYTICS_API_KEY)
PropertiesUtils.injectPropertyInFile(crashlyticsProperties, properties, "")
}
generateResourcesTask.dependsOn generatePropertiesTask
}

然而,这并没有编译并给我:

Error:(334, 1) Execution failed for task ':app:fabricGeneratePropertiesDevelopmentDebug'. Could not find property 'BuildConfig' on task ':app:fabricGeneratePropertiesDevelopmentDebug'.

最佳答案

对于这个错误:

Error:(334, 1) Execution failed for task ':app:fabricGeneratePropertiesDevelopmentDebug'. Could not find property 'BuildConfig' on task ':app:fabricGeneratePropertiesDevelopmentDebug'.

使用不同的结构,例如:

variant.mergedFlavor.buildConfigFields["CRASHLYTICS_API_SECRET"].value

当然,此解决方案需要定义这样的 BuildConfig 字段。如果您预计字段不存在,请确保先检查是否存在。

编辑:

如果您想要问题中描述的解决方案:

File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
android.applicationVariants.all { variant ->
def variantSuffix = variant.name.capitalize()
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
println "...copying apiSecret for ${variant.name}"
properties.put("apiSecret", variant.mergedFlavor.buildConfigFields.get("CRASHLYTICS_API_SECRET").value)
println "...copying apiKey for ${variant.name}"
properties.put("apiKey", variant.mergedFlavor.buildConfigFields.get("CRASHLYTICS_API_KEY").value)
PropertiesUtils.injectPropertyInFile(crashlyticsProperties, properties, "")
}
generateResourcesTask.dependsOn generatePropertiesTask
}

关于android - Android Studio/Gradle 中的 productFlavor 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49132150/

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