【问题标题】:versionNameSuffix for product flavors产品风味的版本名称后缀
【发布时间】:2016-12-26 20:30:54
【问题描述】:

有没有办法为各种产品风格设置不同的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

【问题讨论】:

    标签: android gradle android-gradle-plugin android-productflavors


    【解决方案1】:

    productFlavors 现在支持 versionNameSuffix

    来自安卓开发者文档

    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}"
    
    }
    

    【讨论】:

      【解决方案2】:

      没有直接的方法可以这样做。目前,com.android.application gradle-plugin 不支持任何 DSL 让productFlavors 拥有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/),代码已推送到 GitHub (https://github.com/krrishnaaaa/product-flavours/blob/master/app/build.gradle)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-06
        • 2015-10-06
        • 1970-01-01
        相关资源
        最近更新 更多