[经验分享] 打包hap的技巧(减小体积、命令行打包),以及如何创建API10的项目 原创

westinyang 显示全部楼层 发表于 2023-10-26 15:07:20

关于作者:I'm westinyang

打包hap的技巧

视频教程:https://www.bilibili.com/video/BV18u41177yx

减小打包体积

DevEcoStudio顶部菜单中的 构建 - 编译 中有两个选项,分别是 编译Hap编译App

  • 前者生成的hap,实际上会包含用于调试的 SourceMap 文件,所以打包出来的体积会大一些
  • 而后者生成的hap,是不包含的,打包体积能小很多,一般在分发应用的时候,建议使用这种方式
  • 在实际测中,编译App生成的hap,打包的体积能比前者小30%左右,甚至在有些项目中,打包的体积可以减少50%左右

命令行打包

如果项目已经配置过了签名,后续不想用DevEcoStudio修改打包,例如一些简单的自动化场景,你想通过脚本自动修改项目的某处代码或配置并打包,你也可以通过命令行的方式来打包hap,在项目根目录中打开命令窗口,输入下面的命令,这等同于刚才在DevEcoStudio中的操作

  • hvigorw.bat assembleHap
  • hvigorw.bat assembleApp

另外,你也可以通过 hvigorw.bat -h 查看更多命令的使用方式

Usage: hvigor [taskNames...] <options...>

Options:
  -v, --version                     Show version of Hvigor.
  -e, --error                       Set log level to error.
  -w, --warn                        Set log level to warn.
  -i, --info                        Set log level to info.
  -d, --debug                       Set log level to debug.
  --stacktrace                      Print out the stacktrace for all exceptions.
  -p, --prop <value>                Define extra properties. (default: [])
  -m, --mode <string>               Specifies the mode in which the command is currently executed.
  -s, --sync                        Sync the information in plugin for other platform.
  --enable-build-script-type-check  Enable the build script hvigorfile.ts type check.
  --no-parallel                     Disable parallel building mode.
  --no-incremental                  Disable incremental building mode.
  --no-daemon                       Disable building with daemon process.
  --stop-daemon                     Shut down current project's daemon process.
  --stop-daemon-all                 Shut down all projects' daemon process.
  --status-daemon                   Show all daemon process's status of the project.
  -h, --help                        display help for command

Commands:
  version                           Show version of Hvigor.
  tasks                             Show all available tasks of specific modules.
  taskTree                          Show all available taskTree of specific modules.

Examples:
  hvigor assembleApp  Do assembleApp task

copyright 2023

如何创建API10的项目

视频教程:https://www.bilibili.com/video/BV1vu411J7oz

准确的说应该是:如何使用 DevEcoStudio 4.0 Beta2 创建 API10 的项目,因为等Release版的Studio发布后,应该就不需要这么做了

  • 首先,你需要安装 DevEcoStudio 4.0 Beta2,下载地址
  • 打开DevEcoStudio,选择新建项目,下一步,这里的SDK只能选择API9,我们不管他继续创建
  • 由于API10的项目配置有所变化,所以我们需要手动修改配置
  • 先打开模块下面的 build-profile.json5,删除 runtimeOS配置
  • 再打开根目录的 build-profile.json5,把 compileSdkVersioncompatibleSdkVersion这两行移动到 products里面,并把这两个值都改为 10
  • 最后,在 products内加上 runtimeOS配置,点击 Sync Now 同步项目即可

持续关注

无用

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部