Gradle 具有 Android 风格的 applicationId

Gradle applicationId with flavors for Android

我想在 Google Play 商店上传我的应用程序,但在它上线之前我想测试它。应用程序中有一个测试模式,我希望生产 applicationId 像

com.company.product

但我也想有 2 种口味来测试

com.company.product.alpha
&&
com.company.product.beta

我的应用 build.gradle 应该如何?

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.company"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  
        ....

flavorDimensions "version"
    productFlavors {
        product {
            dimension "version"
            applicationIdSuffix ".product"
            versionCode 1
            versionName "1.0"
        }

        alpha {
            dimension "version"
            applicationIdSuffix ".product.alpha"
            versionCode 1
            versionName "1.0"
        }
     
        beta {
            dimension "version"
            applicationIdSuffix ".product.beta"
            versionCode 1
            versionName "1.0"
        }
    }


.....或者我想像这样?

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.company.product"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  
        ....

flavorDimensions "version"
    productFlavors {

        alpha {
            dimension "version"
            applicationIdSuffix ".alpha"
            versionCode 1
            versionName "1.0"
        }
     
        beta {
            dimension "version"
            applicationIdSuffix ".beta"
            versionCode 1
            versionName "1.0"
        }
    }


你可以这样做:

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
    applicationId "com.company.product"
    minSdkVersion 21
    targetSdkVersion 30
    flavorDimensions "application"
    versionCode 1
    versionName "1.0.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

productFlavors {
    alpha {
        dimension "application"
        applicationIdSuffix ".alpha"
        versionNameSuffix "-alpha"
        buildConfigField "String", "FLAVOR_TYPE", '"alpha"'
    }
    beta {
        dimension "application"
        applicationIdSuffix ".beta"
        versionNameSuffix "-beta"
        buildConfigField "String", "FLAVOR_TYPE", '"beta"'
    }
    production {
        dimension "application"
        applicationIdSuffix ""
        versionNameSuffix ""
        buildConfigField "String", "FLAVOR_TYPE", '"production"'
    }
}

为每种口味设置 applicationIdSuffixversionNameSuffix,并为生产一种留空。通过这种方式,您可以分发 com.company.product.alphacom.company.product.beta 的 alpha 和 beta 版本。