6

I am having difficulty getting Android Studio to build the right build variant — or even to let me select a build variant at all, sometimes.

Basically I have two different versions of my project a free and a "full" version. Package ids are "com.mycompany.myproj" and "com.mycompany.myprojfree".

Once I've specified "myproj" and "myprojfree" flavors and "release" and "debug" buildtypes, Android Studio produces four variants in the list: myprojDebug, myprojfreeDebug, myprojfreeRelease, and myprojRelease.

Problem is, selecting one of these doesn't reliably select the variant for building, debugging, etc. For instance, I'll select myprojDebug, hit Debug, and myprojfreeDebug will build (as can be seen in the console), and the free version will open on the attached device.

Moreover, sometimes I can't even select one or more of the build variants in the build variant pane. I can click on it, but it doesn't change. But sometimes if I change it to something else first it'll let me go back and change the non-changing one.

I've seen posts referring to similar-sounding problems and have followed all the suggestions — cleaning, rebuilding, deleting .idea, deleting the build folder, Invalidate Caches/Restart, deleting app.iml, etc. — all to no avail.

It may be worth noting that all of this worked fine until yesterday, when I updated from Android Studio 3.1 to 3.4.1.

Here's a simplified version of my app build.gradle:

apply plugin: 'com.android.application'

android {
    defaultConfig {
        versionCode ...
        multiDexEnabled true
        vectorDrawables {
            useSupportLibrary true
        }
        minSdkVersion 15
        targetSdkVersion 28
    }

    compileSdkVersion 28

    signingConfigs {
        myproj {
            keyAlias ...
            keyPassword ...
            storeFile file('...')
            storePassword ...
        }
        myprojfree {
            keyAlias ...
            keyPassword ...
            storeFile file('...')
            storePassword ...
        }
    }

    flavorDimensions "tier"

    productFlavors {
        myproj {
            signingConfig signingConfigs.myproj
            applicationId 'com.mycompany.myproj'
        }
        myprojfree {
            signingConfig signingConfigs.myprojfree
            applicationId 'com.mycompany.myprojfree'
        }
    }

    buildTypes {
        release {
            debuggable false
            buildConfigField "Boolean", "MY_DEBUG_MODE", "false"
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            gradle.projectsEvaluated {
                tasks.withType(JavaCompile) {
                    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
                }
            }
        }
        debug {
            debuggable true
            buildConfigField "Boolean", "MY_DEBUG_MODE", "true"
            gradle.projectsEvaluated {
                tasks.withType(JavaCompile) {
                    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
                }
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
    }

    configurations {
        implementation.exclude group: "org.apache.httpcomponents", module: "httpclient"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    ...
}
6
  • Your config is fine. Changing build variant is something that shouldn't be often. It requires recompiling of all files and takes some time. Commented May 29, 2019 at 21:18
  • That's fine, and what I would expect. This is only done when basically switching which (sub-)app is being compiled and debugged. That said, I'd also expect it to work. Commented May 29, 2019 at 21:22
  • Hi, same problem, did you fix it? Commented Nov 27, 2019 at 8:30
  • Honestly what I had taken to doing is just cleaning and building until things seem to turn out properly. Other than that I try to avoid it. Commented Dec 2, 2019 at 5:22
  • I'm stuck on this for a few hours, sometimes it works... I suggest 'Sync project with Gradle files' Commented Jan 20, 2020 at 14:40

1 Answer 1

-2

I'm pretty sure the problem came from a non-synchrony between files and Gradle sync.

So do `File/Sync Project with Gradle Files' after a change of Build Variant. Then clean project, rebuild and run.

Sign up to request clarification or add additional context in comments.

1 Comment

replace it from: dependencies { classpath 'com.android.tools.build:gradle:3.2.1' } to my Android Studio v3.0.1 in my case: dependencies { classpath 'com.android.tools.build:gradle:3.0.1' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.