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 {
    ...
}
KT_
  • 978
  • 11
  • 26
  • Your config is fine. Changing build variant is something that shouldn't be often. It requires recompiling of all files and takes some time. – Said May 29 '19 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. – KT_ May 29 '19 at 21:22
  • Hi, same problem, did you fix it? – tehmaestro Nov 27 '19 at 08: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. – KT_ Dec 02 '19 at 05:22
  • I'm stuck on this for a few hours, sometimes it works... I suggest 'Sync project with Gradle files' – Alexandre Gombert Jan 20 '20 at 14:40
  • I got stuck on this for a while. tried everything you tried but nothing worked. then i removed the project from android studio and re-imported it. everything worked. src: https://stackoverflow.com/a/45950059/5443056 – Braden Holt Apr 07 '20 at 20:49

1 Answers1

-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.

  • 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' } – saber tabatabaee yazdi Nov 11 '20 at 12:15