当前位置: 首页 > 知识库问答 >
问题:

Android Gradle 1.0计算版本代码在多种风格的设置中

章涵蓄
2023-03-14

为不同产品口味计算版本代码的代码在Android Gradle1.0系统中不再工作。我以前成功地使用了下面的示例代码。

android {

  buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
      classpath 'com.android.tools.build:gradle:1.0.0'
    }
  }

  // This actual the app version code. Our given range is [0, 99999]
  defaultConfig.versionCode = 123

  // 2 dimensions of flavors. API is more important than ABI.
  flavorGroups "api", "abi"

  productFlavors {
    gingerbread {
      flavorGroup "api"
      minSdkVersion 10
      versionCode = 1
    }
    icecreamSandwich {
      flavorGroup "api"
      minSdkVersion 14
      // this must be higher than the gingerbread version to ensure update of the
      // app when the device gets a system update from GB to ICS
      versionCode = 2
    }
    x86 {
      flavorGroup "abi"
      ndk.abiFilter "x86"
      // this is the flavor part of the version code.
      // It must be higher than the arm one for devices supporting
      // both, as x86 is preferred.
      versionCode = 3
    }
    arm {
      flavorGroup "abi"
      ndk.abiFilter "armeabi-v7a"
      versionCode = 1
    }
    mips {
      flavorGroup "abi"
      // It must be higher than the arm one for devices supporting
      // both, as mips is preferred.
      ndk.abiFilter "mips"
      versionCode = 2
    }
    fat {
      flavorGroup "abi"
      // fat binary, lowest version code to be
      // the last option
      versionCode = 0
    }
  }

  // make per-variant version code
  applicationVariants.all { variant ->
    // get the version code of each flavor
    def apiVersion = variant.productFlavors.get(0).versionCode
    def abiVersion = variant.productFlavors.get(1).versionCode

    // set the composite code
     variant.mergedFlavor.versionCode = apiVersion * 1000000 + abiVersion * 100000 + defaultConfig.versionCode
  }
}

共有1个答案

令狐建修
2023-03-14

摘自Google用户指南

多味变种

在某些情况下,人们可能想要基于不止一个标准创建相同应用程序的几个版本。例如,Google Play中的多APK支持支持4种不同的过滤器。在每个过滤器上创建不同的APK,需要能够使用不止一个维度的产品口味。

flavorDimensions "abi", "version"

productFlavors {
    freeapp {
        flavorDimension "version"
        ...
    }

    x86 {
        flavorDimension "abi"
        ...
    }
} }
   // 2 dimensions of flavors. API is more important than ABI.
flavorDimensions "api", "abi"

productFlavors {
    gingerbread {
        flavorDimension "api"
        minSdkVersion 10
        versionCode = 1
    }
    icecreamSandwich {
        flavorDimension "api"
        minSdkVersion 14
        // this must be higher than the gingerbread version to ensure update of the
        // app when the device gets a system update from GB to ICS
        versionCode = 2
    }
    x86 {
        flavorDimension "abi"
        ndk.abiFilter "x86"
        // this is the flavor part of the version code.
        // It must be higher than the arm one for devices supporting
        // both, as x86 is preferred.
        versionCode = 3
    }
    arm {
        flavorDimension "abi"
        ndk.abiFilter "armeabi-v7a"
        versionCode = 1
    }
    mips {
        flavorDimension "abi"
        // It must be higher than the arm one for devices supporting
        // both, as mips is preferred.
        ndk.abiFilter "mips"
        versionCode = 2
    }
    fat {
        flavorDimension "abi"
        // fat binary, lowest version code to be
        // the last option
        versionCode = 0
    }
}

// make per-variant version code
applicationVariants.all { variant ->
    // get the version code of each flavor
    def apiVersion = variant.productFlavors.get(0).versionCode
    def abiVersion = variant.productFlavors.get(1).versionCode

    // set the composite code
    variant.mergedFlavor.versionCode = apiVersion * 1000000 + abiVersion * 100000 + defaultConfig.versionCode
}
    applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def apk = output.outputFile;
        def newName =  "${output.name}-${variant.mergedFlavor.versionCode}"
        if (variant.buildType.versionNameSuffix) {
            newName += "-${variant.buildType.versionNameSuffix}"
        }
        if (output.zipAlign) {
            output.zipAlign.outputFile = new File((File) apk.parentFile, newName + '-aligned.apk');
        }
        output.packageApplication.outputFile = new File((File) apk.parentFile, newName + ".apk")
    }
}

参见下面的构建结果:

    gingerbreadArmDebug-1100123.apk
    gingerbreadArmDebug-1100123-aligned.apk
    gingerbreadFatDebug-1000123.apk
    gingerbreadFatDebug-1000123-aligned.apk
    gingerbreadMipsDebug-1200123.apk
    gingerbreadMipsDebug-1200123-aligned.apk
    gingerbreadX86Debug-1300123.apk
    gingerbreadX86Debug-1300123-aligned.apk
    icecreamSandwichArmDebug-2100123.apk
    icecreamSandwichArmDebug-2100123-aligned.apk
    icecreamSandwichFatDebug-2000123.apk
    icecreamSandwichFatDebug-2000123-aligned.apk
    icecreamSandwichMipsDebug-2200123.apk
    icecreamSandwichMipsDebug-2200123-aligned.apk
    icecreamSandwichX86Debug-2300123.apk
    icecreamSandwichX86Debug-2300123-aligned.apk
    gingerbreadArmRelease-1100123.apk
    gingerbreadFatRelease-1000123.apk
    gingerbreadMipsRelease-1200123.apk
    gingerbreadX86Release-1300123.apk
    icecreamSandwichArmRelease-2100123.apk
    icecreamSandwichFatRelease-2000123.apk
    icecreamSandwichMipsRelease-2200123.apk
    icecreamSandwichX86Release-2300123.apk

其中一个的信息,由apktool提取:

version: 2.0.0-RC3
apkFileName: gingerbreadArmDebug-1100123.apk
isFrameworkApk: false
usesFramework:
  ids:
  - 1
sdkInfo:
  minSdkVersion: '10'
  targetSdkVersion: '21'
packageInfo:
  forced-package-id: '127'
versionInfo:
  versionCode: '1100123'
  versionName: '1.0'
compressionType: false

更新2:

在GitHub上发布了我的测试项目

 类似资料:
  • 我们的代码必须尽可能的清晰和易读。 这实际上是一种编程艺术 —— 以一种正确并且人们易读的方式编码来完成一个复杂的任务。一个良好的代码风格大大有助于实现这一点。 语法 下面是一个备忘单,其中列出了一些建议的规则(详情请参阅下文): <!-- ```js function pow(x, n) { let result = 1; for (let i = 0; i < n; i++) { result

  • 代码风格 必须 严格遵循 PSR-2 规范。

  • 空白 每行不能超出99个字符。 缩进只用空格,不用TAB。 行和文件末尾不要有空白。 空格 二元运算符左右加空格,包括属性里的等号: #[deprecated = "Use `bar` instead."] fn foo(a: usize, b: usize) -> usize { a + b } 在分号和逗号后面加空格: fn foo(a: Bar); MyStruct { foo: 3

  • 通过设置风格名称来更改3D地球的颜色风格。Gio.js提供一些预设的色彩风格。 例如:想将默认风格更改为“magic”风格: controller.setStyle("magic"); 试试codepen在线编辑: See the Pen Gio setStyle() by syt123450 (@syt123450) on CodePen. 预设风格 这里您可以预览我们提供的预设配色模板,您可以

  • 我们已经覆盖了配置项目时的大多数一次性任务:选择一个许可证,安排初始的网站等等。但是开始一个新项目时最重要的方面是会变化的。选择一个邮件列表地址很容易;而保证列表的对话不走题且有效率则完全是另一回事情。如果项目经过多年的关闭,内部的开发,重新开始时开发过程将会改变,你需要准备开发者来应对这种变化。 第一步是最难的,因为未来管理的先例和预期都没有设置。正式的规则还没有带来项目的稳定性,而是由开发过程

  • 您可以在创建或编辑文件时设置用于控制代码格式的首选参数。 手动设置代码格式 打开支持的文件。 选择“编辑”>“代码”>“应用源格式”。 或者,从“常用工具栏”>“格式化源代码”中选择“应用源格式”。 手动设置文件中选定代码的格式 打开代码。 选择代码的任意部分。 选择“编辑”>“代码”>“将源格式应用于选定内容”。 或者,从“常用工具栏”>“格式化源代码”中选择“将源格式应用于选定内容”。 注意: