Changing build variants in bulk in AndroidStudio 3

2020-08-22 05:53发布

问题:

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?

回答1:

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"/'
    
  • 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.



回答2:

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.

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