Android Studio 构建变体问题

KT_*_*KT_ 6 android-studio build.gradle android-build-type android-build-flavors build-variant

我很难让 Android Studio 构建正确的构建变体——有时甚至根本无法让我选择构建变体。

基本上,我的项目有两个不同的版本,一个是免费版本,另一个是“完整”版本。包 ID 是“com.mycompany.myproj”和“com.mycompany.myprojfree”。

一旦我指定了“myproj”和“myprojfree”风格以及“发布”和“调试”构建类型,Android Studio 会在列表中生成四个变体:myprojDebug、myprojfreeDebug、myprojfreeRelease 和 myprojRelease。

问题是,选择其中之一并不能可靠地选择用于构建、调试等的变体。例如,我将选择 myprojDebug,点击 Debug,然后 myprojfreeDebug 将构建(如在控制台中所见),而免费的版本将在连接的设备上打开。

此外,有时我什至无法在构建变体窗格中选择一个或多个构建变体。我可以点击它,但它不会改变。但有时如果我先把它改成别的东西,它会让我回去改掉不变的那个。

我看过一些帖子提到类似的问题,并遵循了所有的建议——清理、重建、删除 .idea、删除构建文件夹、使缓存/重启、删除 app.iml 等——都无济于事。

值得注意的是,直到昨天,当我从 Android Studio 3.1 更新到 3.4.1 时,所有这些都运行良好。

这是我的应用程序 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 {
    ...
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*ert -2

我很确定问题来自于文件和 Gradle 同步之间的不同步。

更改构建变体后,执行“使用 Gradle 文件文件/同步项目”。然后清理项目,重建并运行。