2

We have an app that has 3 different build types (alpha, beta, release) and different flavours (uk, usa...) based on a country deployed).

Instead of manually compiling each combination of buildType and flavour, is there a Gradle task to generate them all at once (uk-alpha, uk-beta, uk-release, usa-alpha, usa-beta, usa-release, ...)?

Edit: Gradle file (without libs)

repositories {

    flatDir {
        dirs 'libs'
    }
}

final VERSION_MAJOR = 0
final VERSION_MINOR = 7
final VERSION_PATCH = 1

android {
    defaultConfig {
        applicationId "com.app"
        multiDexEnabled true
        versionName "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
        versionCode VERSION_MAJOR * 10000000 + VERSION_MINOR * 100000 + VERSION_PATCH * 1000
    }

    signingConfigs {
        release {
            keyAlias 'xxx'
            keyPassword 'xxx'
            storeFile file('xxx')
            storePassword 'xxx'
        }
    }

    dexOptions {
        preDexLibraries = true
        javaMaxHeapSize "2g"
    }

    buildTypes {
        release {
            minifyEnabled true
            debuggable false
            versionNameSuffix ".0-release_location-on_live-server"
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        beta { 
            versionNameSuffix ".1-beta_location-off_live-server"
            minifyEnabled true
            debuggable true
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        alpha { 
            versionNameSuffix ".2-alpha_location_off_test-server"
            minifyEnabled false
            debuggable true
        }

        debug { 
            versionNameSuffix ".3-debug_location_off_test-server"
            minifyEnabled false
            debuggable true
        }
    }
    productFlavors {
        uk {
            minSdkVersion 16
            targetSdkVersion 24
        }

        usa {
           minSdkVersion 16
           targetSdkVersion 24
       }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/rxjava.properties'
        exclude 'META-INF/rxandroid.properties'
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError true
        disable 'InvalidPackage'
    }
    applicationVariants.all { variant ->
//        if(variant.buildType.name.equals("release"))
//            variant.versionCode += 0
//        if(variant.buildType.name.equals("beta"))
//            variant.versionCode += 1
//        if(variant.buildType.name.equals("debug"))
//            variant.versionCode += 2

        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

dependencies {
    ... libs ...
}
vanomart
  • 1,739
  • 1
  • 18
  • 39
  • I thing the `assemble` task will do this. – Karakuri Nov 06 '16 at 20:05
  • @JBirdVegas I tried Gradle build but that gives me this error: http://stackoverflow.com/questions/32987530/android-studio-keystore-was-tampered-with-or-password-was-incorrect – vanomart Nov 07 '16 at 06:58
  • @Karakuri I've tried running "gradle assemble" from Gradle view in Android Studio but that doesn't output any apk. The only way how I can generate .apk is to go throught Build -> Generate signed apk and then output apk version I want – vanomart Nov 07 '16 at 06:58
  • Post your `build.gradle` file along with the actual error. Get the error from the command line when executing the full build task. Sounds like your flavors are not setup right – JBirdVegas Nov 07 '16 at 12:21
  • ok guys, thank you for your help. I just noticed that files that I generate using wizard in android studio are generated in module root folder, but apks generated using gradle build are in app/build/output/apk. I still didn't manage to figure out the error with the keystore, but that's not the point of this topic. – vanomart Nov 07 '16 at 20:45

1 Answers1

0

you can use command line

./gardlew build

Or using gradle menu in android studio app -> Tasks -> build -> build

it will generate all apks in app/build/outputs/apk

Moaz Rashad
  • 1,035
  • 1
  • 10
  • 16