0

我知道每个配置都有多种风味。但我有一个很大的配置列表(大约 15 个),我不想在构建配置中定义 15 个风格。我正在寻找一些基于循环 gradle 构建任务的方法,改变风味的配置或任何不需要手动定义所有风味的类似方法。

有什么建议么 ?

更新 :

repositories {
  mavenCentral()
}

apply plugin: 'com.android.application'


android {
  Properties props = new Properties()
  props.load(new FileInputStream(file('build.properties')))
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion

  defaultConfig {
    applicationId "com.example.app"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    resConfigs "en"

    versionCode props['VERSION_CODE'].toInteger()
    versionName props['VERSION_NAME']
    buildConfigField('boolean', 'DEBUG_MODE', "false")
    buildConfigField('String', 'DEBUG_VERSION', props['EMPTY_STRING'])

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled true
      shrinkResources true
      proguardFiles 'proguard-rules.pro'
    }
  }

  sourceSets {
    sd_debug {
      assets {
        srcDirs('src/sd_debug/assets')
      }
      java {
        srcDirs('src/sd_debug/java')
      }
      res {
        srcDirs('src/sd_debug/res')
      }
    }

    sp_debug {
      assets {
        srcDirs('src/sd_debug/assets')
      }
      java {
        srcDirs('src/sd_debug/java')
      }
      res {
        srcDirs('src/sd_debug/res')
      }
    }

    sd_debug_devpackage {
      assets {
        srcDirs('src/sd_debug/assets')
      }
      java {
        srcDirs('src/sd_debug/java')
      }
      res {
        srcDirs('src/sd_debug/res')
      }
    }

    sp_prod {
      assets {
        srcDirs('src/main/assets')
      }
      java {
        srcDirs('src/sp_prod/java')
      }
      res {
        srcDirs('src/sp_prod/res')
      }
    }
  }

  productFlavors {
    // develop flavor
    sd_debug {
      buildConfigField 'String', 'DEBUG_VERSION', props['DEBUG_VERSION']
      buildConfigField 'boolean', 'DEBUG_MODE', "true"
      buildConfigField 'Integer', 'HOST_ID', "90"
    }
    sp_debug {
      buildConfigField 'String', 'DEBUG_VERSION', props['DEBUG_VERSION']
      buildConfigField 'boolean', 'DEBUG_MODE', "true"
      buildConfigField 'Integer', 'HOST_ID', "1"
    }
    sd_debug_devpackage {
      buildConfigField 'String', 'DEBUG_VERSION', props['DEBUG_VERSION']
      buildConfigField 'boolean', 'DEBUG_MODE', "true"
      buildConfigField 'Integer', 'HOST_ID', "91"
    }

    sp_prod {
      buildConfigField 'boolean', 'DEBUG_MODE', "false"
      buildConfigField 'Integer', 'HOST_ID', "1"
    }
  }

  packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude '.readme'
  }

  dexOptions {
    jumboMode = true
  }
}


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

  compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
  compile "com.android.support:design:$rootProject.supportLibraryVersion"
  compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"

  compile "com.google.android.gms:play-services-base:$rootProject.playServiceVersion"
  compile "com.google.android.gms:play-services-gcm:$rootProject.playServiceVersion"
  compile "com.google.android.gms:play-services-analytics:$rootProject.playServiceVersion"
  compile "com.squareup.okhttp3:okhttp:$rootProject.okhttpVersion"
  compile "com.google.code.gson:gson:$rootProject.gsonVersion"
  compile "com.j256.ormlite:ormlite-core:$rootProject.ormliteVersion"
  compile "com.j256.ormlite:ormlite-android:$rootProject.ormliteVersion"
  compile "com.jakewharton:butterknife:$rootProject.butterknifeVersion"
  compile "me.dm7.barcodescanner:zbar:$rootProject.zbarVersion"
  compile "de.greenrobot:eventbus:$rootProject.eventbusVersion"
  //  compile "io.reactivex:rxjava:$rootProject.rxjavaVersion"
  //  compile "io.reactivex:rxandroid:$rootProject.rxjavaVersion"
  compile "com.google.dagger:dagger:$rootProject.daggerVersion"
  annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"


  // Dependencies for local unit tests
  testCompile "junit:junit:$rootProject.ext.junitVersion"
  testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
  testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
  testCompile "org.powermock:powermock-module-junit4:$rootProject.ext.powerMockito"
  testCompile "org.powermock:powermock-api-mockito:$rootProject.ext.powerMockito"

  // Android Testing Support Library's runner and rules
  androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
  androidTestCompile "com.android.support.test:rules:$rootProject.ext.rulesVersion"

  // Espresso UI Testing dependencies.
  androidTestCompile "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
  androidTestCompile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
  androidTestCompile "com.android.support:design:$rootProject.supportLibraryVersion"
  androidTestCompile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"

  androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
  androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
  androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
}

每种风味都必须使用 api 密钥构建,我有 15 个 api 密钥。

4

1 回答 1

1

这会解决你的问题吗?

[
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12',
    '13',
    '14',
    '15'
].each { hostId ->
    sourceSets {
        "sp_prod_$hostId" {
            assets {
                srcDirs('src/main/assets')
            }
            java {
                srcDirs('src/sp_prod/java')
            }
            res {
                srcDirs('src/sp_prod/res')
            }
        }
    }

    productFlavors {
        "sp_prod_$hostId" {
            buildConfigField 'boolean', 'DEBUG_MODE', "false"
            buildConfigField 'Integer', 'HOST_ID', hostId
        }
    }
}
于 2016-12-13T19:57:53.310 回答