Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Company1
  • Company2

And then I have build types:

  • Production
  • Preview
  • Development

So this will create 6 build variants:

  • Company1Production
  • Company1Preview
  • Company1Development
  • Company2Production
  • Company2Preview
  • Company2Development

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.

like image 842
Kelly Avatar asked Oct 14 '25 17:10

Kelly


1 Answers

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

like image 142
Kelly Avatar answered Oct 17 '25 10:10

Kelly