2
빌드 할 때 파일에서 속성을 읽어서 빌드 유형을 동적으로 추가하려고합니다. 100 개 이상의 빌드 유형이 있으므로 build.gradle 파일에는 쓰지 않습니다.Android Studio의 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"
}
...
}
및 매니페스트 각 빌드를 수정 :
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)
}
}
}
? 100 가지가 넘는 빌드 유형이 가장 좋은 방법 인 유스 케이스를 상상하기는 어렵습니다. –
도와 주셔서 감사합니다. 패키지 (약 40 개)를 많이 만들어야하며 빌드 할 매니페스트 파일을 각각 수정해야합니다. 그래서 많은 빌드 유형을 작성해야했습니다. – xiangmao
-, - 해결 하시겠습니까? 개미를 사용하거나 mvn이 할 수 있습니다. – qinmiao