2

我有 cms-app 。这个应用程序有大约 200 个模块。如果加载所有模块,cpu 100%。而且电脑很慢。我更改 settings.gradle 并加载所需的模块。

我想更改 settings.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 应用程序更改为主 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
   .....

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

4

1 回答 1

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 "";
}
}
于 2016-11-16T10:22:41.873 回答