如何在变换构建变体风格时更改自动settings.gradle

时间:2016-11-06 07:11:38

标签: android android-studio gradle dependencies android-flavors

我有cms-app。这个程序有关于grater 200模块。如果加载所有模块,cpu 100%。和电脑很慢。我更改settings.gradle和加载所需的模块。

我想要更改settings.gradle每个构建变体。

样本gradle。

android {
defaultConfig {
    applicationId "org.myorg.cmsapp"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    manifestPlaceholders = [ MAPS_API_KEY: "123123123123123" ]
}
productFlavors {
    appA {
        applicationId "org.myorg.cmsapp"
        versionName "1.32"
        versionCode 22
        manifestPlaceholders = [ MAPS_API_KEY: "12312312" ]
    }
    appB {
        applicationId "ir.myweb.aaa"
        versionName "1.3"
        versionCode 3
        manifestPlaceholders = [ MAPS_API_KEY: "12312312" ]
    }
    appC {
        applicationId "com.mytw.bbb"
        versionName "1.12"
        versionCode 18
        manifestPlaceholders = [ MAPS_API_KEY: "12312312" ]
    } 
}
signingConfigs {
    appA {
        storeFile file('../keys/appA.jks')
        keyAlias "appA"
        storePassword "app123!!#A"
        keyPassword "app123!@#A"
    }
    appB {
        storeFile file('../keys/appB.jks')
        keyAlias "appB"
        storePassword "appB#!@#12312"
        keyPassword "ap!#!@pB"
    }
    appC {
        storeFile file('../keys/appC.jks')
        keyAlias "appC"
        storePassword "!@#appC\$"
        keyPassword "appC!@#appC\$"
    }
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        productFlavors.appA.signingConfig signingConfigs.appA
        productFlavors.appB.signingConfig signingConfigs.appB
        productFlavors.appC.signingConfig signingConfigs.appC 
    } 
} 
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    appACompile project(path: ':librarysplashscreenone')
    appACompile project(path: ':libraryhomepageeight', configuration: 'appARelease')
    appACompile project(path: ':librarycmspagestest', configuration: 'appARelease')

//    appBtCompile project(path: ':librarysplashscreentwo')
//    appBtCompile project(path: ':libraryhomepageeight', configuration: 'appBtRelease')
//    appBtCompile project(path: ':librarycmspagesdefault', configuration: 'appBtRelease')

//    appCCompile project(path: ':librarysplashscreenthree')
//    appCCompile project(path: ':libraryhomepagesix', configuration: 'appCRelease')
//    appCCompile project(path: ':librarycmspagesdefault', configuration: 'appCRelease')
}

我添加此任务,当更改构建变量将settings.gradle app替换为main settings.gradle。

task chackLoadedModule(type:Exec) {
println " change  ... "
workingDir '../all_module'

 commandLine 'cmd', '/c', 'generator.bat' , getCurrentFlavor()

  standardOutput = new ByteArrayOutputStream()

 ext.output = {
    return standardOutput.toString()
}
}

dependencies {
   preBuild.dependsOn chackLoadedModule
   .....

这个解决方案并不好。 我必须取消注释并评论依赖关系。当我更改构建变体时。 并且有时不工作。

1 个答案:

答案 0 :(得分:0)

我解决了依赖问题。但我有问题加载模块。

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')

String currentFlavors = getCurrentFlavor();

if (currentFlavors == "appA") {
    appACompile project(path: ':librarysplashscreenone')
    appACompile project(path: ':libraryhomepageeight', configuration: 'appARelease')
    appACompile project(path: ':librarycmspagestest', configuration: 'appARelease')
}

if (currentFlavors == "appB") {
    appBtCompile project(path: ':librarysplashscreentwo')
    appBtCompile project(path: ':libraryhomepageeight', configuration: 'appBtRelease')
    appBtCompile project(path: ':librarycmspagesdefault', configuration: 'appBtRelease')
}

if (currentFlavors == "appC") {
    appCCompile project(path: ':librarysplashscreenthree')
    appCCompile project(path: ':libraryhomepagesix', configuration: 'appCRelease')
    appCCompile project(path: ':librarycmspagesdefault', configuration: 'appCRelease')
}
}

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern;

if( tskReqStr.contains( "assemble" ) )
    pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
    pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

Matcher matcher = pattern.matcher( tskReqStr )

if( matcher.find() )
    return matcher.group(1).toLowerCase()
else
{
    println "NO MATCH FOUND"
    return "";
}
}