2013-12-16 2 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

-, - 해결 하시겠습니까? 개미를 사용하거나 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 
--- 
관련 문제