How do I restrict Gradle native binary build based on combination of flavour, platform and build type?

Given a build.gradle containing:

model {
      buildTypes {
        debug
        release
    }
      flavors {
        unitTesting
        export
    }
      platforms {
        "osx-x86_64" {
            operatingSystem "osx"
            architecture "x86_64"
        }
          "windows-x86_64" {
            operatingSystem "windows"
            architecture "x86_64"
        }
      }
}
  libraries {
    libaryIamWantingToBuild {}
}

How do I define which libraries I want to build, based on a combination of platform, build type and flavour?

For example, how do I define to Gradle that I want to build the following combination of libraries only?:

osx-x86_64, debug, export osx-x86_64, debug, unitTesting osx-x86_64, release, export

windows-x86_64, debug, export windows-x86_64, release, export The reason being is that I wish to create debug & release libraries for other projects to link to, but the unit testing library is only used within the context of this project in order to produce gcov statistics.

When you configure a set of variant dimensions (platforms/buildTypes/flavors) you’re configuring the entire possible set of binary variants that can be built.

There are a couple of ways of restricting the set that will be built:

  • Allow Gradle to choose what should be built by building the top-level executable/libraries required, with Gradle building the required library dependencies.
  • Target a component at a restricted subset of variant dimensions: http ://www.gradle.org/docs/nightly/userguide/nativeBinaries.html#N15EFE
  • Only calling the tasks for the binaries you want to build (or creating a task that depends on these binaries)
  • Setting the ‘buildable’ flag to false for any binary you don’t want built
The third option looks something like this. It’s not pretty but it works:
binaries.all {
    if (platform.operatingSystem.macOsX) {
        if (buildType == buildTypes.release) {
            if (flavor.name != “export”) {
                 buildable = false
            }
        }
    }
} task buildAll {

dependsOn binaries.matching {

it.buildable

} }