产品口味的 versionNameSuffix

versionNameSuffix for product flavors

有没有什么方法可以为不同的产品风格设置不同的 versionNameSuffix,并且可以为构建类型设置不同的

以下代码适用于我:

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            verstionNameSuffix "-prod"
        }
}

但是当我想为这样的产品口味设置 versionNameSuffix 时:

productFlavors {
        production {
            versionNameSuffix "-prod"
        }
        development {
            versionNameSuffix "-dev"
        }
}

我收到这个错误:

Error:(48, 0) Could not find method versionNameSuffix() for arguments [-dev] on ProductFlavor_Decorated{name=development, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={BASE_URL=com.android.builder.internal.ClassFieldImpl@1630a8db}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.ProductFlavor.

有没有其他方法可以设置产品口味的versionNameSuffix

没有直接的方法。目前,com.android.application gradle-plugin 不支持 productFlavors 的任何 DSL 以具有 versionNameSuffix.

如果你想这样做,你必须创建两个方法。

import java.util.regex.Matcher
import java.util.regex.Pattern

def getCurrentVersionSuffix() {
    def currentFlavor = getCurrentFlavor()
    if (currentFlavor.equals("prod")) {
        return "-prod"
    } else if (currentFlavor.equals("uat")) {
        return "-uat"
    } else if (currentFlavor.equals("dev")) {
        return "-dev"
    }
}

def getCurrentFlavor() {
    String taskRequestName = getGradle().getStartParameter().getTaskRequests().toString()

    Pattern pattern;

    if (taskRequestName.contains("assemble"))
        pattern = Pattern.compile("assemble(\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher(taskRequestName)

    if (matcher.find()) {
        return matcher.group(1).toLowerCase()
    } else {
        return "";
    }
}

并更新 buildTypes DSL

buildTypes {
    debug {
        versionNameSuffix getCurrentVersionSuffix()
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        versionNameSuffix getCurrentVersionSuffix()
    }
}

这是目前实现的唯一途径。可能以后他们会提供一些DSL什么的。

我已经写了一篇关于这个的博客 (http://www.pcsalt.com/android/product-flavors-android/) and the code is pushed on GitHub (https://github.com/krrishnaaaa/product-flavours/blob/master/app/build.gradle)。

productFlavors 现在支持 versionNameSuffix

来自 android 开发者文档

https://developer.android.com/studio/build/build-variants#flavor-dimensions

android {
  ...
  buildTypes {
    debug {...}
    release {...}
  }

  // Specifies the flavor dimensions you want to use. The order in which you
  // list each dimension determines its priority, from highest to lowest,
  // when Gradle merges variant sources and configurations. You must assign
  // each product flavor you configure to one of the flavor dimensions.
  flavorDimensions "api", "mode"

  productFlavors {
    demo {
      // Assigns this product flavor to the "mode" flavor dimension.
      dimension "mode"
      ...
    }

    full {
      dimension "mode"
      ...
    }

    // Configurations in the "api" product flavors override those in "mode"
    // flavors and the defaultConfig block. Gradle determines the priority
    // between flavor dimensions based on the order in which they appear next
    // to the flavorDimensions property above--the first dimension has a higher
    // priority than the second, and so on.
    minApi24 {
      dimension "api"
      minSdkVersion 24
      // To ensure the target device receives the version of the app with
      // the highest compatible API level, assign version codes in increasing
      // value with API level. To learn more about assigning version codes to
      // support app updates and uploading to Google Play, read Multiple APK Support
      versionCode 30000 + android.defaultConfig.versionCode
      versionNameSuffix "-minApi24"
      ...
    }

    minApi23 {
      dimension "api"
      minSdkVersion 23
      versionCode 20000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi23"
      ...
    }

    minApi21 {
      dimension "api"
      minSdkVersion 21
      versionCode 10000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi21"
      ...
    }
  }
}
...

您还可以像这样对 productFlavors 进行操作:

android.productFlavors.all { flavor ->

    flavor.versionNameSuffix = "-${flavor.name}"

}