Skip to content

0x039-鸿蒙应用开发笔记7-脚本打包

之前记录怎么打包[0x029-鸿蒙应用开发-如何打release包] 但是开发和打包切换的时候需要频繁切换Sign Configs非常难受。 这里研究下如何通过命令打包:

安装hvigorw

安装hvigorw命令行 参考:hvigorw-Command Line Tools - 华为HarmonyOS开发者

命令举例

常用命令

  • clean
  • assembleApp

例如:

bash
hvigorw assembleApp
# 根据文档,有用缺省参数
hvigorw assembleApp -p buildMode=release product=default
hvigorw assembleApp
# 根据文档,有用缺省参数
hvigorw assembleApp -p buildMode=release product=default

可以从product=default入手,增加一个Product,

|400

  • 命令行中的product= 驱动的是 applyToProducts字段的变量,必须取到
  • 图里的Product对应products数组

因此,也在signingConfigs增加了生产环境的证书配置,修改build-profile.json5配置文件,如下

json
{
  "app": {
    "signingConfigs": [
      {
        "name": "default",
        "type": "HarmonyOS",
        "material": {
          "storePassword": "xxx",
          "certpath": "xxxx.cer",
          "keyAlias": "xx",
          "keyPassword": "xx",
          "profile": "xxxx.p7b",
          "signAlg": "xx",
          "storeFile": "xxxkey.p12"
        }
      },     
      {
        "name": "release",
        "type": "HarmonyOS",
        "material": {
          "storePassword": "xxx",
          "certpath": "xxxx1.cer",
          "keyAlias": "xx",
          "keyPassword": "xx",
          "profile": "xxxx1.p7b",
          "signAlg": "xx",
          "storeFile": "xxxkey1.p12"
        }
      }
    ],
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
        "compatibleSdkVersion": "5.0.0(12)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "useNormalizedOHMUrl": true
          }
        }
      },
      {
        "name": "Testing",
        "signingConfig": "release",
        "compatibleSdkVersion": "5.0.0(12)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "useNormalizedOHMUrl": true
          }
        }
      }
    ],
    "buildModeSet": [
      {
        "name": "debug",
      },
      {
        "name": "release"
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default",
            "Testing"
          ]
        }
      ]
    }
  ]
}
{
  "app": {
    "signingConfigs": [
      {
        "name": "default",
        "type": "HarmonyOS",
        "material": {
          "storePassword": "xxx",
          "certpath": "xxxx.cer",
          "keyAlias": "xx",
          "keyPassword": "xx",
          "profile": "xxxx.p7b",
          "signAlg": "xx",
          "storeFile": "xxxkey.p12"
        }
      },     
      {
        "name": "release",
        "type": "HarmonyOS",
        "material": {
          "storePassword": "xxx",
          "certpath": "xxxx1.cer",
          "keyAlias": "xx",
          "keyPassword": "xx",
          "profile": "xxxx1.p7b",
          "signAlg": "xx",
          "storeFile": "xxxkey1.p12"
        }
      }
    ],
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
        "compatibleSdkVersion": "5.0.0(12)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "useNormalizedOHMUrl": true
          }
        }
      },
      {
        "name": "Testing",
        "signingConfig": "release",
        "compatibleSdkVersion": "5.0.0(12)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "useNormalizedOHMUrl": true
          }
        }
      }
    ],
    "buildModeSet": [
      {
        "name": "debug",
      },
      {
        "name": "release"
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default",
            "Testing"
          ]
        }
      ]
    }
  ]
}

配置完成后,按如下命令可以进行打包:

bash
hvigorw assembleApp -p product=Testing
# hvigorw assembleApp -p product=Testing && open ./build/outputs/Testing
hvigorw assembleApp -p product=Testing
# hvigorw assembleApp -p product=Testing && open ./build/outputs/Testing

或者直接选Product也行,不需要再来回修改证书路径。