2013-12-16 7 views
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) 
    } 
} 

}

+1

你到底想幹什麼?很難想象一個超過100種構建類型是最好的方法的用例。 –

+0

謝謝你的幫助。我需要構建大量的包(大約40個),每個包都需要修改清單文件來構建。所以我不得不寫很多構建類型。 – xiangmao

+0

- , - 解決它?使用ant或mvn可以做到這一點。 – qinmiao

回答

0

我知道這個問題是舊的,但我有類似今天要做的事情。 從yml文件讀取值以生成味道。 而且由於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 
--- 
相關問題