24

Quick history:

  • I'm using AndroidStudio 3.0
  • my project has 100+ modules (multiple applications and libraries).
  • all of them have same flavorDimensions and flavors.

Now question: how to change flavors for all modules in bulk in AndroidStudio 3.0+ without changing each application separately?

MatrixDev
  • 1,432
  • 15
  • 20
  • I have just 7 modules and I felt the need to ask this question, I can't imagine having this problem for 100+ modules. What solution do you use at the moment? – arekolek May 21 '19 at 11:19
  • 1
    @arekolek I've wrote gradle plugin which disabled all flavors except one configured in gradle.properties. At least this way it only takes to change one word in properties and perform a gradle sync and thats all. I'd still like for better UX from AS side though – MatrixDev May 22 '19 at 17:46
  • Could you share your plugin ? – Gavin Liu Feb 24 '20 at 06:51
  • 1
    This hasn't really been possible consistently as the project file structures change and nobody wants to close and reopen Android Studio on the regular. So I build an [Android Studio Plugin](https://plugins.jetbrains.com/plugin/14450-build-variant-quick-selector) – gtcompscientist Jun 19 '20 at 14:19

5 Answers5

10

Here is what I recently ended up doing:

  • close Android Studio

  • open a terminal

  • cd to the base directory of your project

  • replace all occurrences of <option name="SELECTED_BUILD_VARIANT" value="debug" /> with <option name="SELECTED_BUILD_VARIANT" value="release" /> or vice versa in all iml files. Here is a one liner to change all modules to release:

      find . -name \*.iml | xargs perl -pi -e 's/<option\s+name="SELECTED_BUILD_VARIANT"\s+value="[^"]+"/<option name="SELECTED_BUILD_VARIANT" value="release"/'
    
  • to change back to debug run:

      find . -name \*.iml | xargs perl -pi -e 's/<option\s+name="SELECTED_BUILD_VARIANT"\s+value="[^"]+"/<option name="SELECTED_BUILD_VARIANT" value="debug"/'
    
  • Open Android Studio again and do gradle sync

  • The build variant of all modules should be replaced now

Of course this approach makes assumptions about the formatting of an xml file which makes it a bit fragile. So far it seems to work well though.

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
Marten
  • 3,802
  • 1
  • 17
  • 26
10

There are two settings in Android Studio to enable switching all variants at once when selecting the app variant.

The settings are "Only sync the active variant" & "Do not build Gradle task list during Gradle sync". Both of them need to be disabled, then I restarted Android Studio once and Gradle sync.

Android Studio settings

With the settings in the screenshot disabled, I went from switching and waiting for ~10 seconds per dynamic-feature module (10 in my project) to one single switch in <5 seconds.

Note: This is tested in Android Studio 4.0.1

Sebas LG
  • 1,715
  • 1
  • 18
  • 31
  • are you sure it switches variants in bulk? as far as I know it just avoids running Gradle tasks when you change configuration. – MatrixDev Jul 03 '20 at 08:24
  • @MatrixDev Yes, I've updated the info because when I switch these options AS might start working as expected only until next restart. The updated config I described works consistently after AS restart. – Sebas LG Jul 23 '20 at 21:29
  • It's not woking on Android Studio 4.1.3 – Hanako Sep 06 '21 at 18:12
  • saved my day, all I had to do is that disabling both options. Thanks! – emrcftci Apr 19 '22 at 08:42
7

I built the Build Variant Matrix Selector which, with all respect, I believe is faster to use than the ".. Quick Selector" mentioned above here. No duplicate drop downs, no hassle. Just select the variants from radio button in the matrix and go.

enter image description here

Nilzor
  • 18,082
  • 22
  • 100
  • 167
  • A little back story here: I built the plugin because I first tried the Build Variant Quick Selector but wasn't happy with it. But it served as a great inspiration and even helped me kick start my code being open source, so big thanks to Pandora! – Nilzor Mar 20 '21 at 07:51
  • nice plugin, saves time immensely – Peterstev Uremgba Jun 25 '21 at 12:16
  • Great plugin, recently updated to work with most recent android studio - electric eel – Karol Feb 20 '23 at 10:21
  • This actually does work very well. Since they removed the ability to disable gradle auto-syncing (WHY?!?!?) Studio is a nightmare to work with. This plugin makes it so much faster to switch variants! – CR. Apr 13 '23 at 22:55
  • 2
    Doesn't work on Giraffe | 2022.3.1 Beta 2 - https://github.com/Nilzor/build-variant-matrix/issues/10 – medavox May 12 '23 at 11:05
4

You need a plugin called Build Variant Quick-Selector

  1. Install Build Variant Quick-Selector (Settings->Plugins->Marketplace)

  2. Build-> Switch All Build Variants...

Irwin Nawrocki
  • 337
  • 4
  • 7
0

Just extending Marten's answer. It's a bit tedious to use command line and not see what you're doing. Just use vscode or any other text editor of your choice.

  • First close Android Studio (because it can revert the changes back to original)
  • Search for *.iml files that has the text below. Just make sure to include any folders which are ignored from git. vscode seems to ignore searching folders and files that are defined in .gittignore
<option name="SELECTED_BUILD_VARIANT" value="debug" />

then replace it debug/release whatever you want.

  • Open Android Studio and see changes are applied

enter image description here