如何为每种口味生成单独的签名 APK?

How to generate a separate signed APK for each flavor?

我正在使用 Android Studio 3 更新 Android 应用程序。以前的版本是使用 Android Studio 2 创建的。

当我使用 Android Studio 2 构建最后一个版本时,已经创建了两个单独的 APK,一个用于我配置的每种产品风格。现在,在 Android Studio 3 中,"Generate Signed APK" 对话框仅提供 "combined" 风格,它会创建一个包含两种风格的 APK:

应用Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.MyApp"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 42
        versionName "2.0.1"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "freemium", "free"
    productFlavors {
        MyApp {
            dimension "freemium"
        }
        MyAppFree {
            dimension "free"
            applicationId 'com.example.MyApp.Free'
        }
    }
}

如何解决这个问题?

好的,问题是由于两种口味使用不同的口味维度造成的。 Gradle 为每个维度组合创建一个 assemble 任务:MyAppMyAppFree 在我的例子中

我添加尺寸只是为了在从 Android Studio 2 更新到 3 时消除警告 "All flavors must now belong to a named flavor dimension"。因为我实际上没有使用维度解决方案是简单地为两种口味使用一个维度:

flavorDimensions "default"
    productFlavors {
        MyApp {
            dimension "default"
        }
        MyAppFree {
            dimension "default"
            applicationId 'com.example.MyApp.Free'
        }
    }