#2023盲盒+码# 打包hap的技巧(减小体积、命令行打包),以及如何创建API10的项目 原创 精华
westinyang
 发布于 2023-8-21 09:11
 浏览
 0收藏
【本文正在参加 2023「盲盒」+码有奖征文活动】 https://ost.51cto.com/posts/25284
@toc
关于作者:I’m westinyang
打包hap的技巧
减小打包体积
DevEcoStudio顶部菜单中的 构建 - 编译 中有两个选项,分别是编译Hap和编译App

- 前者生成的hap,实际上会包含用于调试的 
SourceMap文件,所以打包出来的体积会大一些 - 而后者生成的hap,是不包含的,打包体积能小很多,一般在分发应用的时候,建议使用这种方式
 - 在实际测中,编译App生成的hap,打包的体积能比前者小30%左右,甚至在有些项目中,打包的体积可以减少50%左右
 
命令行打包
如果项目已经配置过了签名,后续不想用DevEcoStudio修改打包,例如一些简单的自动化场景,你想通过脚本自动修改项目的某处代码或配置并打包,你也可以通过命令行的方式来打包hap,在项目根目录中打开命令窗口,输入下面的命令,这等同于刚才在DevEcoStudio中的操作
hvigorw.bat assembleHaphvigorw.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的项目
准确的说应该是:如何使用 DevEcoStudio 4.0 Beta2 创建 API10 的项目,因为等Release版的Studio发布后,应该就不需要这么做了
- 首先,你需要安装 DevEcoStudio 4.0 Beta2,下载地址
 - 打开DevEcoStudio,选择新建项目,下一步,这里的SDK只能选择API9,我们不管他继续创建
 - 由于API10的项目配置有所变化,所以我们需要手动修改配置
 - 先打开模块下面的
build-profile.json5,删除runtimeOS配置 - 再打开根目录的
build-profile.json5,把compileSdkVersion和compatibleSdkVersion这两行移动到products里面,并把这两个值都改为10 - 最后,在
products内加上runtimeOS配置,点击 Sync Now 同步项目即可 

持续关注
- 关于作者:I’m westinyang
 - 哔哩哔哩:个人主页
 
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
    已于2024-1-17 23:51:57修改
 
        赞
        4
 
        收藏 
      
 回复
  相关推荐
 




















不错不错,对着视频学习清晰又明了!
bucuobucuo
SourceMap文件居然能占到百分之五十
原来可以手动修改API10
是的,目前beta2只能这样修改
比如cocos2d游戏编译的包,就能相差50%左右的体积