gpt4 book ai didi

android - 如何从基于Android Studio的Gradle中的文件动态添加构建类型?

转载 作者:行者123 更新时间:2023-12-03 03:35:48 25 4
gpt4 key购买 nike

我想通过在构建时从文件中读取属性来动态添加构建类型。因为有超过100种构建类型,所以我不写build.gradle文件。

谁能帮我 ?

builde.gradle文件:

buildTypes{
release {
signingConfig signingConfigs.myConfig
runProguard true
zipAlign true
proguardFiles files('proguard.cfg', getDefaultProguardFile('proguard-android.txt'))
}

samsung {
initWith release
versionNameSuffix "samsung"
packageNameSuffix ".samsung"
}

google {
initWith release
versionNameSuffix "google"
packageNameSuffix ".google"
}
...
}

并修改每个构建的 list :
android.applicationVariants.all { variant -> 
def propertyList = variant.productFlavors*.name
propertyList.add(variant.buildType.name)

// read property from file
def variantProperties = new Properties()
propertyList.reverse().each { property ->
def propFile = "channel.properties"
try {
file(propFile).withReader { reader ->
def tmpProps = new Properties()
tmpProps.load(reader)
variantProperties.putAll(tmpProps)
}
} catch(e) {
println "[${variant.name}] Failed to load ${propFile}"
}
}
println "[${variant.name}] manifest properties: ${variantProperties}"
// default channel value is SinaDown
def defaultValue = "16"
def channel = variant.buildType.name
def value = variantProperties.getProperty("${channel}", defaultValue)
println "[Channel]: ${channel} [Value]: ${value}"

// modify AndroidManifest.xml
variant.processManifest.doLast {
copy {
from("${buildDir}/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/manifests/$variant.name")

// get the channel value
def channelValue = variantProperties.getProperty("${channel}", defaultValue)
println "[Channel]: ${channel} [Value]: ${channelValue}"

filter {
String line ->
line.replaceAll("<meta-data android:name=\"CHANNEL\" android:value=\".*\"/>",
"<meta-data android:name=\"CHANNEL\" android:value=\"${channelValue}\"/>")
}

// set the path to the modified Manifest:
def manifestPath = "${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml"
variant.processResources.manifestFile = file(manifestPath)
}
}

}

最佳答案

我知道这个问题很旧,但是今天我不得不做类似的事情。
从yml文件中读取值以生成 flavor 。
而且由于buildTypes和productFlavors都为NamedDomainObjectContainer,所以它也应该起作用。

查看Inject Build Variables into the Manifest,然后将代码修改为所需的代码。

我的bluid.gradle文件类似于:

 apply plugin: "pl.softmate.gradle-properties-yaml-plugin"
def configs = project.ext.properties.configs // configs from the .yml
android {
//...
configs.each { config ->
productFlavors.create(config.name, {
applicationId config.applicationId
// ...
})
}

和我的yml文件:
---
configs:
- name: brand1
applicationId: com.app.brand1
- name: brand2
applicationId: com.app.brand2
---

关于android - 如何从基于Android Studio的Gradle中的文件动态添加构建类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607083/

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