HarmonyOS 自动化打包脚本实现

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可以参考:

1、打包指导:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-command-line-building-app-0000001672412437-V5

2、自动提交发布包指导:https://developer.huawei.com/consumer/cn/doc/AppGallery-connect-References/agcapi-app-submit-with-file-0000001111845092

3、打包脚本demo:

#!/bin/bash
export JAVA_HOME=/Users/DATA/harmonyos_config/jdk-17.0.11.jdk/Contents/Home
export HDC_HOME=/Users/DATA/harmonyos_config/HarmonyOS-NEXT-DP2/base/toolchains
export HOS_SDK_HOME=/Users/DATA/harmonyos_config
export OHOS_NATIVE_HOME=/Users/DATA/harmonyos_config/HarmonyOS-NEXT-DP2/base/native
export NODE_HOME=/Users/DATA/harmonyos_config/node-v16.19.1-darwin-x64
set -x
path=$(dirname $0)
PROJECT_PATH=${path/\./$(pwd)}
  export OUT_PATH=$PROJECT_PATH/../../../../release/harmonyos_dict
  echo ${OUT_PATH}

  rm ${OUT_PATH}/*.app
  cd ${PROJECT_PATH}
  git reset --head
  git checkout dev
  git pull

  #环境已配置,下载并配置Node.js
  function init_Node() {
  #判断Node.js是否已安装
  if command -v node >/dev/null 2>&1; then
  return
  fi

  if [ ! -d "${NODE_HOME}" ]; then
  mkdir "${NODE_HOME}"
  fi

  cd ${NODE_HOME}
  wget --no-check-certificate -q "${NODE_DOWNLOAD_PATH}" -O node-linux.tar.xz #下载node
  tar -vxf node-linux.tar.xz
  NODE_DIR_PATH=xxx #node压缩包文件里面的目录
  cd ${NODE_DIR_PATH}
  mv -f ./* .[^.]* ../
  cd ..
  rm -rf NODE_DIR_PATH node-linux.tar.xz
  export NODE_HOME=${NODE_HOME}
  export PATH=$NODE_HOME/bin:$PATH
  node -v
  npm config set registry=https://repo.huaweicloud.com/repository/npm/
  npm config set @ohos:registry=https://repo.harmonyos.com/npm/

  npm config get @ohos:registry
  npm config set proxy=http://user:password@proxy.server.com:port #配置npm http代理,企业网络受限的情况下需要配置
  npm config set https-proxy=http://user:password@proxy.server.com:port #配置npm https代理,企业网络受限的情况下需要配置
  npm info express
  }

  #环境已配置,下载并配置JDK
  function init_JDK() {
  #判断jdk是否存在
  if command -v java >/dev/null 2>&1; then
  return
  fi

  if [ ! -d "${JAVA_HOME}" ]; then
  mkdir "${JAVA_HOME}"
  fi
  cd ${JAVA_HOME}
  wget --no-check-certificate -q "${JAVA_DOWNLOAD_PATH}" -O jdk-linux.tar.xz #下载jdk
  tar -vxf jdk-linux.tar.xz
  cd ${JDK_DIR_PATH}
  mv -f ./* .[^.]* ../
  cd ..
  rm -rf JDK_DIR_PATH jdk-linux.tar.xz
  export JAVA_HOME=${JAVA_HOME}
  export PATH=$JAVA_HOME/bin:$PATH
  java -version
  }

  #环境已配置,配置SDK(已获取离线SDK并解压完成)
  function init_SDK() {
  export $HDC_HOME
  export PATH=$HDC_HOME:$PATH
  export $HOS_NATIVE_HOME
  export HOS_SDK_HOME=${HOS_SDK_HOME}
  }
  # 环境已配置,安装ohpm, 若镜像中已存在ohpm,则无需重新安装
  function init_ohpm() {
  #判断ohpm是否存在
  if command -v ohpm >/dev/null 2>&1; then
  return
  fi
  # 初始化ohpm
  OHPM_HOME=${COMMANDLINE_TOOL_DIR}/command-line-tools/ohpm
  ${OHPM_HOME}/bin/init
  export PATH=${OHPM_HOME}/bin:$PATH
  ohpm -v
  # 配置ohpm仓库地址

  ohpm config set registry=https://ohpm.openharmony.cn/ohpm/
  }

  # 进入package目录安装依赖
  function ohpm_install {
  cd $1
  ohpm install
  }

  # 环境适配
  function init_HAP() {
  # 根据业务情况适配local.properties
  cd ${PROJECT_PATH}
  echo "hwsdk.dir=${HOS_SDK_HOME}" > ./local.properties
  # 根据业务情况安装ohpm三方库依赖
  ohpm_install "${PROJECT_PATH}"
  ohpm_install "${PROJECT_PATH}/native"
  ohpm_install "${PROJECT_PATH}/common/basic"
  ohpm_install "${PROJECT_PATH}/common/loginlib"
  ohpm_install "${PROJECT_PATH}/entry"
  # 如果构建过程报错 ERR_PNPM_OUTDATED_LOCKFILE,需要增加配置:lockfile=false, 根据node版本选择设置方式:
  # node.version<18
  npm config set lockfile=false # 如果执行此命令报错,建议直接在镜像的.npmrc文件中需要增加一行配置:lockfile=false
  # node.version>=18
  #cat ${HOME}/.npmrc | grep 'lockfile=false' || echo 'lockfile=false' >> ${HOME}/.npmrc
  # 根据业务情况,采用对应的构建命令,可以参考IDE构建日志中的命令
  }

  #构建所有模块的app
  function build_APP() {
  generate_APP "bundleName1" "productName1" "demo1_Release.p7b"
  generate_APP "bundleName2" "productName2" "demo2_Release.p7b"
  generate_APP "bundleName3" "productName3" "demo3_Release.p7b"
  generate_APP "bundleName4" "productName4" "demo4_Release.p7b"
  }

  #构建APP
  function generate_APP() {
  appBundleName=$1
  productName=$2
  profileFile=$3
  update_value_to_app_json ${appBundleName}

  # 清除工程下build目录
  cd ${PROJECT_PATH} && chmod +x hvigorw
  ./hvigorw clean --no-daemon

  # 打包,非签名包
  cd ${PROJECT_PATH} && ./hvigorw --mode project -p product=${productName} assembleApp --analyze=normal --parallel --incremental --daemon
  projectName=$(basename "${PROJECT_PATH}")
  #将非签名包构建成签名包
  cp ${PROJECT_PATH}/build/outputs/${productName}/${projectName}-${productName}-unsigned.app ${PROJECT_PATH}
  cp ${HOS_SDK_HOME}/HarmonyOS-NEXT-DP2/base/toolchains/lib/hap-sign-tool.jar ${PROJECT_PATH} && chmod +x hap-sign-tool.jar
  java -jar hap-sign-tool.jar sign-app -keyAlias "demo" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "demo.cer" -profileFile "${profileFile}" -inFile "${projectName}-${productName}-unsigned.app" -keystoreFile "demo.p12" -outFile "${productName}-signed.app" -keyPwd "*******" -keystorePwd "*******" -signCode "1"
  mv ./${productName}-signed.app ${OUT_PATH}/
  #构建完成后清除过程文件
  rm hap-sign-tool.jar
  rm ${projectName}-${productName}-unsigned.app
  }

  #更新${PROJECT_PATH}/AppScope/app.json5中的app.bundleName
  function update_value_to_app_json() {
  value=$1
  cd ${PROJECT_PATH}/AppScope
  if jq . "app.json5" >/dev/null 2>&1; then
  echo "app.json5 is valid."
  else
  echo "app.json5 is invalid."
  fi
  jq '.app.bundleName = "'${value}'"' ${PROJECT_PATH}/AppScope/app.json5
  }

  function main {
  #init_Node
  #init_JDK
  #init_SDK
  #init_ohpm
  init_HAP
  build_APP
  echo "build success!"
  }
分享
微博
QQ
微信
回复
22h前
相关问题
HarmonyOS 应用脚本打包
0浏览 • 1回复 待解决
HarmonyOS 自动化测试传参问题
31浏览 • 1回复 待解决
HarmonyOS应用的自动化测试工具
629浏览 • 1回复 待解决
HarmonyOS 是否有自动化测试的工具
22浏览 • 1回复 待解决
HarmonyOS 第三方应用自动化发布咨询
233浏览 • 1回复 待解决
HarmonyOS 能否提供脚本打包
110浏览 • 1回复 待解决
HarmonyOS 项目自动打包命令
50浏览 • 1回复 待解决