Search code examples
androidgradleandroid-build-flavors

Build multiple apk with a flavor and multiple config


I know it's possible to have multiple flavor with configuration for each of them. but i have a big list of configuration (about 15) and i don't want to define 15 flavor in build configuration. I'm looking for some approach base on looping on gradle build task with changing configuration of flavor or any similar approach that don't need to define exactly all flavor by hand.

any suggestions ?

Updated :

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"
}

each flavor must be build with an api key, and i have 15 api keys.


Solution

  • Would this solve your problem?

    [
        '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
            }
        }
    }