【问题标题】:Android Studio build variant problemsAndroid Studio 构建变体问题
【发布时间】:2019-10-15 12:53:02
【问题描述】:

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

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

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

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

此外,有时我什至无法在构建变体窗格中选择一个或多个构建变体。我可以点击它,但它不会改变。但有时如果我先将其更改为 else,它会让我返回并更改不变的。

我看到过提到类似问题的帖子,并遵循了所有建议 - 清理、重建、删除 .idea、删除构建文件夹、使缓存/重新启动无效、删除 app.iml 等 - 全部为否有用。

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

这是我的 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 {
    ...
}

【问题讨论】:

  • 你的配置没问题。更改构建变体是不应该经常发生的事情。它需要重新编译所有文件并且需要一些时间。
  • 没关系,这也是我所期望的。这仅在基本上切换正在编译和调试的(子)应用程序时才完成。也就是说,我也希望它能够工作。
  • 你好,同样的问题,你解决了吗?
  • 老实说,我所做的只是清洁和建造,直到事情看起来正常。除此之外,我尽量避免。
  • 我在这个问题上卡了几个小时,有时它可以工作......我建议'使用 Gradle 文件同步项目'

标签: android-studio build.gradle android-build-flavors android-build-type build-variant


【解决方案1】:

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

在更改 Build Variant 后,“使用 Gradle 文件的文件/同步项目”也是如此。 然后清理项目,重建并运行。

【讨论】:

  • 将它从:dependencies { classpath 'com.android.tools.build:gradle:3.2.1' } 替换为我的 Android Studio v3.0.1 在我的情况下:dependencies { classpath 'com.android. tools.build:gradle:3.0.1' }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多