Wesley
Wesley

Reputation: 583

Gradle: How do I define build type for specific flavors only?

I have been using gradle for creating different build variants for different companies for an Android app.

For example I have build flavors:

And then I have build types:

So this will create 6 build variants:

So the question is: Actually I don't need the development build type for company 2, I only need it for company 1.

Is there a way I can specify only company 1 have the development build type?

I have a lot of companies in my projects, some of the build type just don't make sense for those companies, and it confuses people who want to build the app.

Upvotes: 4

Views: 2011

Answers (2)

Wesley
Wesley

Reputation: 583

To answer my own question, I have found the documentation on the Gradle Plugin User Guide

Filtering Variants

When you add dimensions and flavors, you can end up with variants that don't make sense. For example you may define a flavor that uses your Web API and a flavor that uses hard-coded fake data, for faster testing. The second flavor is only useful for development, but not in release builds. You can remove this variant using the variantFilter closure, like this:

android {
    productFlavors {
        realData
        fakeData
    }

    variantFilter { variant ->
        def names = variant.flavors*.name

        if (names.contains("fakeData") && variant.buildType.name == "release") {
            variant.ignore = true
        }
    }
}

With the configuration above, your project will have only three variants:

  • realDataDebug

  • realDataRelease

  • fakeDataDebug

Upvotes: 14

Doug Stevenson
Doug Stevenson

Reputation: 317322

You can't stop the Android plugin from creating the matrix of all builds, but you can cause the build to fail if it's not valid. For example, if you don't want Flavor2 Debug builds to work, you can stop them like this:

afterEvaluate {
    tasks['prepareFlavor2DebugDependencies'].doFirst {
        throw new IllegalArgumentException("This project is not valid")
    }
}

Upvotes: -1

Related Questions