ChatGPT解决这个技术问题 Extra ChatGPT

What Product Flavor does Android Studio build by default in build.gradle?

We have an Android project that uses the new Gradle build system, and we use Android Studio as a development tool. When there are several product flavors specified in build.gradle, we notice that Android Studio builds the first one specified alphabetically. Is there a way to tell Android Studio to build and test only a specific product flavor during development?


X
Xavier Ducrohet

On the lower left of the Studio window there's a docked view called "Build Variants".

Open it and choose whichever variant you want to work on. This will impact which variant is built but also the enabled source folders, which will drive completion in code and resources editors.


Cool. Is there a way to customize which one it prefers upon initial import?
Variants are a combination of flavor(s) and build types. You can only deploy variants. You can never build an apk that's only a flavor.
If you interested, you can use command line to build specified variant: ./gradlew assemble<...>, e.g. assembleGooglePlayRelease. And you can build and install on connected device or emulator using: ./gradlew install<...>
But with which product flavor, does Android studio build the application by default?
I would be interested if there is a way to define the build flavor, which is selected by default when checking out the project. currently it selects the second flavor and second buildtype, but would like to define it my way
A
Anton Tananaev

Currently there appears to be no way to select default flavor. There is a feature request in Android Issue Tracker for it:

https://code.google.com/p/android/issues/detail?id=64917

You can star/vote the request, so it gets higher priority on Android development tools backlog.


this feature will be in Android Studio 3.5. NO JOKE - this took 5 years to implement !! (look at the ticket creation date)
C
Community

Yes, there is a way:

android {
    productFlavors {
        foo {
            isDefault true
        }
    }
}

And otherwise variants with the debug build type are favoured.

It was added in Android Studio 3.5, see feature request:

"Included in Android Gradle Plugin 3.5.0-alpha08 and Android Studio 3.5 Canary 8 [3.5.0.7]. The heuristic for projects using older AGP and projects without explicit settings has also been updated to favour variants with the build type debug, as described in the commit message."


isDefault is declared as final and there’s no setter available. However we can use isDefault.set(true)
also: isDefault = true or isDefault true
maybe this doesn't work with gradle.kts scripts or smth - does nothing in our project
R
Roman Tikhonov

What actually worked for me is enabling "Android Studio Preferences -> Experimental -> Only sync the active variant". After you select the desired build variant once, AS will keep the selected build variant when reopening or when re-syncing the project, basically solving the original problem.

AS/AGP v4.1.


a
aprilmintacpineda

On latest of android studio, on menu bar, go to Build > Select build Variant, if it's greyed out (disabled), open the project directory and then open the app section then go to that button again. After you've clicked on it, it will open up a window on the sidebar called Build Variants, from there you can select which build variant you want to build, then click on debug and it will build the variant you selected.


T
Tshunglee

You can specify the command run In Section Before launch Gradle-aware Make

You can list all gradle tasks by gradlew tasks

https://i.stack.imgur.com/MLJYx.png


I tried this and while it certainly does result in only one flavor being built, Android Studio will still only launch the selected variant. Worse, if I select a specific Activity not in the selected variant , then I get a confusing error message saying "the activity must be exported or contain an intent-filter". The error goes away once I change the build bariant selection.