简体   繁体   English

Android Studio 构建变体问题

[英]Android Studio build variant problems

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.我很难让 Android Studio 构建正确的构建变体——有时甚至根本无法让我选择构建变体。

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".包 ID 是“com.mycompany.myproj”和“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.一旦我指定了“myproj”和“myprojfree”风格以及“发布”和“调试”构建类型,Android Studio 会在列表中生成四个变体:myprojDebug、myprojfreeDebug、myprojfreeRelease 和 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.问题是,选择其中之一并不能可靠地选择用于构建、调试等的变体。例如,我将选择 myprojDebug,点击 Debug,然后 myprojfreeDebug 将构建(如在控制台中所见),而免费的版本将在连接的设备上打开。

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

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.值得注意的是,直到昨天,当我从 Android Studio 3.1 更新到 3.4.1 时,所有这些都运行良好。

Here's a simplified version of my app build.gradle:这是我的应用程序 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 {
    ...
}

I'm pretty sure the problem came from a non-synchrony between files and Gradle sync.我很确定问题来自文件和 Gradle 同步之间的不同步。

So do `File/Sync Project with Gradle Files' after a change of Build Variant.更改 Build Variant 后,请执行“File/Sync Project with Gradle Files”。 Then clean project, rebuild and run.然后清理项目,重建并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM