Android-Gradle Plugin - product flavors, project properties and dependencies

Hello everyone,

this question is comprised of two parts:

  1. I am building different product flavors of an Android App in Gradle (Android Studio).

Hence I defined the following product flavors:

android {
      project.ext.set("customer", "")
    project.ext.set("server", "")
      //Configuration happens here - code removed for readability
      buildTypes {
          debug {
            server = "test"
        }
          release {
            server = "release"
        }
    }
      //Available product flavors
    productFlavors {
        customerA{
            customer = "a"
        }
        customerB{
            customer = "b"
        }
        customerC{
            customer = "c"
        }
    }
}

However, later on, when I access the defined project property “customer” (whose value is set in the product flavor i am currently building) it always has the value “c” even though i build customerA (in which case customer should be “a” rather than “c”). For instance I execute the following task later on:

task preBuild.doLast {
    println "Building customer: " + customer
}

and it always prints:

Building customer: c

Regardless of which product flavor i am currently building(I build them separately at the moment). So i am guessing there is some overwriting happening? Possibly related to the configuration VS execution phase? Not sure how/why though, so any help is be greatly appreciated.

  1. I need to execute my own code (i.e. task), after my android project has been compiled by gradle (but not yet packaged into an apk file). So I used:
gradlew tasks --all

To get a list of available tasks. I am using product flavors, so almost all tasks have the name of a flavor somewhere in between like:

assembleFlavorA

or

installFlavorB

etc…

What I can do right now, is execute my own task before compiling starts by hooking into the preBuild task:

preBuild <<{
    //Do some stuff
}

The above gets called for every product flavor which is exactly what I want. However when I try the same with the assemble task or the build task:

assemble <<{
    //Get's never executed
}
  build <<{
    //Get's never executed
}

The above code is never executed, regardless of which product flavor I am building. Looking at the dependency list for releaseFlavorA:

myapp:assembleFlavorARelease - Assembles the Release build for flavor FlavorA [library:bundleRelease]
    myapp:checkFlavorAReleaseManifest
    myapp:compileFlavorAReleaseAidl
    myapp:compileFlavorAReleaseJava
    myapp:compileFlavorAReleaseNdk
    myapp:compileFlavorAReleaseRenderscript
    myapp:dexFlavorARelease
    myapp:generateFlavorAReleaseAssets
    myapp:generateFlavorAReleaseBuildConfig
    myapp:generateFlavorAReleaseResValues
    myapp:generateFlavorAReleaseResources
    myapp:generateFlavorAReleaseSources
    myapp:lintVitalFlavorARelease - Runs lint on just the fatal issues in the FlavorARelease build
    myapp:mergeFlavorAReleaseAssets
    myapp:mergeFlavorAReleaseResources
    myapp:packageFlavorARelease
    myapp:preFlavorADebugBuild
    myapp:preFlavorAReleaseBuild
    myapp:preAltdorfDebugBuild
    myapp:preAltdorfReleaseBuild
    myapp:preBerlinDebugBuild
    myapp:preBerlinReleaseBuild
    myapp:preBuild
    myapp:prepareFlavorAReleaseDependencies
    myapp:prepareComAndroidSupportAppcompatV71910Library - Prepare com.android.support:appcompat-v7:19.1.0
    myapp:prepareTrunkGradleLibraryUnspecifiedLibrary - Prepare trunk-gradle:library:unspecified
    myapp:processFlavorAReleaseJavaRes
    myapp:processFlavorAReleaseManifest
    myapp:processFlavorAReleaseResources
    myapp:validateReleaseSigning
    myapp:zipalignFlavorARelease

I only see preBuild but neither assemble nor build, which is odd, since it’s shown when running

gradlew tasks

But most methods in above list are flavor specific, and I don’t want to have the same task 20 times, because I have 20 different flavors… So how can I execute my necessary tasks, once the compiling is done, but the APK hasn’t been packaged yet for all flavors? Something like:

//I know there is no task called "postCompile" - so anything post compiling and pre-packaging would be fine
postCompile << {
    //Do something that needs to be done for all flavors
}

Thank you very much in advance and please let me know if I need to clarify something.