17

I got a project configured with multiple variants and flavors:

buildTypes {
    debug {
    }
    release {
    }
}
flavorDimensions "default"
productFlavors {
    mock {
    }
    alpha {
    }
    beta {
    }
    prod {
    }
}

Whenever I open the project from another one (so starting Android Studio), it selects the mockDebug variant by default. Often I end up build this one first, then realizing I'm on the wrong variant.

Is there a way to tell Android Studio to defaults to one variant, let's say betaDebug?

Technicals: Android Studio 3.1.4, Gradle wrapper 4.4, Android Gradle 3.1.4.

shkschneider
  • 17,833
  • 13
  • 59
  • 112

3 Answers3

20

With Android Studio 3.5+ you can set default falvors:

android {
  flavorDimensions "stage", "target"
  productFlavors {
    develop {
      getIsDefault().set(true) // that does the magic
      dimension "stage"
      ...

When using KTS it lookes like this:

android {
  flavorDimensions("stage", "target")
  productFlavors {
    create("develop") {
      isDefault = true
      dimension("stage")
      ...
G00fY
  • 4,545
  • 1
  • 22
  • 26
  • 2
    How did you find out? Looked in source? Just curious how people do it first time =) – Maksim Turaev Mar 27 '20 at 06:02
  • 1
    @MaksimTuraev Most probably from [this issue](https://issuetracker.google.com/issues/36988145), referenced in [this SO answer](https://stackoverflow.com/a/37912981/1233652). – Alex Lipov Sep 23 '20 at 13:52
  • 1
    @MaksimTuraev I actually check the sources (https://stackoverflow.com/a/44122888/3660290) every now and then. But the easiest way is to switch to KTS, since the type-safe accessors offer autocompletion. – G00fY Mar 11 '21 at 17:42
  • 1
    For the Groovy file "getIsDefault().set(true)" can be replaced by "isDefault true". – jlguenego Jul 29 '21 at 07:59
5

Change the order in which you define them in productFlavors. The IDE always loads the first flavor it finds there as the default.

TibiG
  • 819
  • 1
  • 6
  • 18
  • Silly of me. Thanks. – shkschneider Aug 29 '18 at 07:51
  • 35
    This doesn't seem to be the case for me - my `Build Variants` list is just alphanumerically sorted. Changing the order in the `productFlavors` block doesn't affect it. This is for Android Studio 3.1.3, at least. – eliasbagley Dec 05 '18 at 17:22
0

What actually worked for me is enabling "Android Studio Preferences -> Experimental -> Only sync the active variant". It keeps the selected build variant when reopening AS or when re-syncing, basically solving the original problem.

AS/AGP v4.1.