OpenHarmony应用编译 - 如何在源码中编译复杂应用(3.2-Release)
创始人
2025-07-08 16:11:31
0

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

概述

文档环境

  • 开发环境:Windows 11
  • 编译环境:Ubuntu 22.04
  • 开发板型号:DAYU 200(RK3568)
  • 系统版本:OpenHarmony-3.2-Release
  • 涉及仓库:applications_launcher

功能简介

  • 在OpenHarmony系统中预安装应用的hap包会随系统编译打包到镜像中,目前有两种编译预安装应用hap包的方式,一种为随系统编译时,编译应用源码生成hap包的方式,另一种是将已生成的hap包放入系统源码中,再进行打包的方式。后者需要开发者使用DevEco Studio或其它途径,把应用源码编译构建为hap包,再将hap放入系统源码中。
  • 本文档以系统应用Launcher为例,带大家了解如何通过系统源码编译应用的方式来打包预安装应用。

3.2-Release系统编译Launcher

 删除或注释系统中默认的Launcher应用hap包编译方式。

文件位置:applications/standard/hap/BUILD.gn 

group("hap") {
  deps = [
    	...
      # "//applications/standard/hap:launcher_hap", // 直接删除或注释,不参与编译
      # "//applications/standard/hap:launcher_settings_hap",  // 直接删除或注释,不参与编译
    	...
  ]
 }

在applications/standard/launcher/product/phone目录中增加BUILD.gn文件。

内容如下:

import("//build/ohos.gni")

ohos_hap("launcher_hap") {
  hap_profile = "src/main/module.json"
  deps = [
    ":launcher_js_assets",
    ":launcher_resources",
  ]
  certificate_profile = "../../signature/launcher.p7b"
  hap_name = "Launcher_OS"
  part_name = "prebuilt_hap"
  subsystem_name = "applications"
  js_build_mode = "release"
  module_install_dir = "app/com.ohos.launcher"
}

ohos_js_assets("launcher_js_assets") {
  ets2abc = true
  source_dir = "src/main/ets"
}

ohos_resources("launcher_resources") {
  sources = [ "src/main/resources", "../../common/src/main/resources" ]
  deps = [ ":launcher_app_profile" ]
  hap_profile = "src/main/module.json"
}

ohos_app_scope("launcher_app_profile") {
  app_profile = "../../AppScope/app.json"
  sources = [ "../../AppScope/resources" ]
}

说明:

  • 此处把产物名称hap_name定义为Launcher_OS是为了区分原系统源码中默认的hap包名称,实际可以填写为Launcher。
  • 因为launcher_hap中使用了其它har(common)中的资源,所以launcher_resources中的sources需要添加依赖的resources资源目录,在步骤3中launcher_settings_hap
    中的配置也是如此。

在applications/standard/launcher/feature/settings目录中增加BUILD.gn文件。

内容如下:

import("//build/ohos.gni")

ohos_hap("launcher_settings_hap") {
  hap_profile = "src/main/module.json"
  deps = [
    ":launcher_settings_js_assets",
    ":launcher_settings_resources",
  ]
  certificate_profile = "../../signature/launcher.p7b"
  hap_name = "Launcher_Settings_OS"
  part_name = "prebuilt_hap"
  subsystem_name = "applications"
  js_build_mode = "release"
  module_install_dir = "app/com.ohos.launcher"
}

ohos_js_assets("launcher_settings_js_assets") {
  ets2abc = true
  source_dir = "src/main/ets"
}

ohos_resources("launcher_settings_resources") {
  sources = [ "src/main/resources", "../../common/src/main/resources" ]
  deps = [ ":launcher_settings_app_profile" ]
  hap_profile = "src/main/module.json"
}

ohos_app_scope("launcher_settings_app_profile") {
  app_profile = "../../AppScope/app.json"
  sources = [ "../../AppScope/resources" ]
}

说明:

  • 此处把产物名称hap_name定义为Launcher_Settings_OS是为了区分原系统源码中默认的hap包名称,实际可以填写为Launcher_Settings

因为系统编译只可读取json后缀的配置文件,所以需要把应用源码中的app.json5和module.json5文件复制,并改名为app.json和module.json文件。

复制applications/standard/launcher/AppScope/app.json5文件为app.json。

复制applications/standard/launcher/product/phone/src/main/module.json5文件为module.json。

复制applications/standard/launcher/feature/settings/src/main/module.json5文件为module.json。

在applications/standard/hap/ohos.build文件module_list中增加launcher_hap模块和launcher_settings_hap模块编译。

{
  "subsystem": "applications",
  "parts": {
    "prebuilt_hap": {
	  	...
      "module_list": [
       	...
        "//applications/standard/launcher/product/phone:launcher_hap",
        "//applications/standard/launcher/feature/settings:launcher_settings_hap",
        ...
      ]
    }
  }
}

修改预编译配置build/prebuilts_download_config.json文件,把launcher依赖增加到npm_install_path列表中。

{
  "prebuilts_download_dir": "../OpenHarmony_canary_prebuilts",
  "npm_install_path": [
    "developtools/ace_ets2bundle/compiler",
    "developtools/ace_js2bundle/ace-loader",
    "third_party/jsframework",
    "arkcompiler/ets_frontend/ts2panda",
    "arkcompiler/ets_frontend/legacy_bin/api8",
    "interface/sdk-js/build-tools",
    "applications/standard/launcher/common",
    "applications/standard/launcher/feature/appcenter",
    "applications/standard/launcher/feature/bigfolder",
    "applications/standard/launcher/feature/form",
    "applications/standard/launcher/feature/gesturenavigation",
    "applications/standard/launcher/feature/numbadge",
    "applications/standard/launcher/feature/pagedesktop",
    "applications/standard/launcher/feature/recents",
    "applications/standard/launcher/feature/smartdock",
    "applications/standard/launcher/feature/settings",
    "applications/standard/launcher/product/phone"
  ],
  ...
}

执行预编译指令。

bash build/prebuilts_download.sh --skip-ssl

可以在应用源码中看到应用内的依赖已经被正确的加载。

执行源码编译指令。如果以下2个目录产物正确,则说明应用源码编译方式修改成功。

在out/rk3568/obj/applications/standard/launcher目录中,会生成2个hap的编译产物。

在out/rk3568/packages/phone/system/app/com.ohos.launcher目录中,是实际系统环境中的hap包产物。

烧录系统验证功能。

Launcher正常启动:

系统应用目录文件正确:

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...
Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...