OpenHarmony 的“一多开发”指的是 一次开发,多端部署(简称“一多”),即使用 一套代码工程,一次开发上架,按需部署到不同终端设备上
🎯 核心概念速览
产品形态定义
写在前面:
1.不同的形态其实本质差异体现在子系统和部件的差异,openharmony操作系统底层其实屏蔽了不同内核之间的差异,使其2in1(2in1和PC其实是相同的,只是换了称呼)设备类型也可以在TV上运行,在phone/default类型设备也可以运行。
2.vendor目录下的产品形态配置文件(比如vendor\hihope\dayu210\config.json)会根据相同合并、差异覆盖的方式更新productdefine\common\inherit\phone.json。
3.ArkTs即应用层主要体现在: “deviceTypes”: [
“phone”,
“tablet”
]
4.需要注意是的部分设备形态在低版本的API上是不支持的,其底层通过SysCap能力集合来区分不同的设备差异,当然也可以通过配置使其都支持。
5.不同设备类型通过bundleManager(BMS)安装子系统来进行校验,即HAP所设置的deviceTypes与实际设备类型不符合,是无法安装的。
不同形态编译命令:
方式一:
./build.sh -product-name 2in1_core_system -ccache --device-type=2in1
./build.sh -product-name dayu210 -ccache --device-type=2in1
仅作参考,实际并不希望随意更改设备类型。(不符合设计规范)
方式二:
hb命令在配置好自己的产品后,执行hb set命令会显示你的定制产品
.hb build -ccache --device-type=2in1
OpenHarmony采用分层解耦的产品定义架构,将产品配置分为三个层次:
层次 | 描述 | 配置位置 | 示例 |
---|---|---|---|
系统类型 | 基础系统能力 | base/ | mini/small/standard |
设备形态 | 产品类别模板 | inherit/ | phone/tablet/watch |
产品实例 | 具体产品配置 | products/ | system-arm64-default |
系统类型对比表
特性 | 轻量系统(mini) | 小型系统(small) | 标准系统(standard) |
---|---|---|---|
最小内存 | 128KB | 1MB | 128MB |
内核类型 | LiteOS-M | LiteOS-A/Linux | Linux |
处理器架构 | Cortex-M/RISC-V 32位 | Cortex-A | Cortex-A |
图形能力 | 无界面 | 基础GUI | 完整3D加速 |
应用框架 | 无 | 轻量级JS | 完整ArkUI |
分布式能力 | 基础通信 | 完整软总线 | 全场景协同 |
典型设备 | 传感器、手环 | 摄像头、路由器 | 手机、平板、车机 |
📁 源码目录架构
目录结构
productdefine/common/ [源码根目录]
├── base/ # 系统类型基础配置
│ ├── mini_system.json # 轻量系统最小集 [128KB+]
│ ├── small_system.json # 小型系统最小集 [1MB+]
│ └── standard_system.json # 标准系统最小集 [128MB+]
├── inherit/ # 设备形态模板库
│ ├── chipset_common.json # 芯片通用模板
│ ├── headless.json # 无头系统模板
│ ├── ipcamera.json # IPCamera模板
│ ├── phone.json # 手机模板 [通话+数据]
│ ├── tablet.json # 平板模板 [大屏+触控]
│ ├── wearable.json # 穿戴设备模板 [低功耗]
│ ├── tv.json # 电视模板 [多媒体]
│ ├── 2in1.json # 二合一设备模板
│ └── rich.json # 全功能模板
└── products/ # 具体产品配置
├── ohos-sdk.json # SDK产品配置
├── system_arm_default.json # 32位ARM配置
└── system_arm64_default.json # 64位ARM配置
🔍 产品形态本质区别分析
1. 系统架构差异
🏗️ 内核架构对比
🎯 系统能力矩阵
能力维度 | 轻量系统 | 小型系统 | 标准系统 |
---|---|---|---|
进程管理 | 任务调度 | 多进程 | 多用户多进程 |
内存管理 | 静态分配 | 动态分配 | 虚拟内存 |
文件系统 | 轻量级FS | 完整FS | 多文件系统 |
网络协议 | 基础TCP/IP | 完整协议栈 | 全协议支持 |
图形系统 | 无 | 基础GUI | 3D加速 |
安全机制 | 基础加密 | 完整安全框架 | 企业级安全 |
2. 设备形态特性对比
📱 手机形态 (phone.json)
核心特征:
- 通话能力:完整电话协议栈
- 移动网络:2G/3G/4G/5G/5G-A/6G支持
- 位置服务:GPS/北斗/基站定位
- 传感器:加速度计、陀螺仪、磁力计
- 相机:前后置摄像头支持
- 电池管理:精确电量管理
配置片段:
{
"component": "telephony",
"features": [
]
}
📱 平板形态 (tablet.json)
核心特征:
- 大屏适配:10-13英寸屏幕优化
- 触控优化:多点触控、手势识别
- 分屏多任务:多窗口并行运行
- 无通话模块:相比手机裁剪电话功能
- 增强图形:更高分辨率支持
差异化配置:
{
"inherit": ["productdefine/common/inherit/rich.json"],
"subsystems": [
{
"subsystem": "telephony",// 移除电话功能
"components": []
}
]
}
⌚ 穿戴形态 (wearable.json)
核心特征:
- 超低功耗:深度睡眠模式
- 健康监测:心率、血氧、步数
- 小屏适配:1-2英寸圆形/方形屏幕
- 蓝牙连接:BLE低功耗通信
- 简化UI:极简交互界面
功耗优化配置:
{
"component": "power_manager",
"features":
]
}
📺 电视形态 (tv.json)
核心特征:
- 大屏显示:55-85英寸4K/8K支持
- 遥控器:红外/蓝牙遥控
- 媒体播放:4K视频解码、HDR支持
- 网络连接:WiFi 6、有线以太网
- 无触控:纯遥控器操作
媒体能力配置:
{
"component": "multimedia",
"features": [
]
}
🔧 产品配置详解
配置继承机制
🧬 三层继承架构
⚙️ 配置优先级规则
- 高优先级: 产品实例配置
- 中优先级: 设备形态模板
- 低优先级: 系统类型基础
关键配置字段解析
📋 产品配置文件结构
{
"version": "3.0", // 配置格式版本
"product_name": "system-arm64-default", // 产品唯一标识
"device_company": "ohos", // 厂商标识
"target_cpu": "arm64", // 目标CPU架构 [arm/arm64/x86/riscv]
"board": "arm64", // 开发板型号
"type": "standard", // 系统类型 [mini/small/standard]
"enable_ramdisk": true, // 是否启用ramdisk
"build_selinux": true, // 是否启用SELinux
"inherit": ["rich.json"], // 继承的模板列表
"subsystems": [...] // 子系统配置
}
🎯 CPU架构支持矩阵
CPU架构 | 轻量系统 | 小型系统 | 标准系统 | 典型设备 |
---|---|---|---|---|
ARM Cortex-M | ✅ | ❌ | ❌ | STM32、ESP32 |
ARM Cortex-A | ❌ | ✅ | ✅ | 树莓派、RK3568 |
ARM64 | ❌ | ✅ | ✅ | 手机、服务器 |
RISC-V 32 | ✅ | ❌ | ❌ | 物联网芯片 |
RISC-V 64 | ❌ | ✅ | ✅ | 开发板 |
📊 系统能力对比分析
子系统能力矩阵
子系统类别 | 轻量系统 | 小型系统 | 标准系统 | 功能描述 |
---|---|---|---|---|
arkui | ❌ | 基础版 | 完整版 | UI框架 |
telephony | ❌ | 基础版 | 完整版 | 电话功能 |
multimedia | ❌ | 基础版 | 完整版 | 多媒体处理 |
location | ❌ | 基础版 | 完整版 | 位置服务 |
connectivity | 基础版 | 完整版 | 完整版 | 网络连接 |
security | 基础版 | 完整版 | 企业级 | 安全框架 |
distributed | ❌ | 基础版 | 完整版 | 分布式能力 |
内存占用对比
系统类型 | 最小内存 | 推荐内存 | 存储空间 | 启动时间 |
---|---|---|---|---|
轻量系统 | 128KB | 512KB | 1MB | <1s |
小型系统 | 1MB | 4MB | 16MB | 2-5s |
标准系统 | 128MB | 512MB | 2GB | 10-30s |
🛠️ 实战配置指南
创建新产品形态
步骤1: 选择基础系统类型
# 根据硬件资源选择
- 128KB-512KB内存 → mini_system.json
- 1MB-16MB内存 → small_system.json
- 128MB+内存 → standard_system.json
步骤2: 选择设备形态模板
# 根据产品类型选择
- 手机 → phone.json
- 平板 → tablet.json
- 手表 → wearable.json
- 电视 → tv.json
- 通用设备 → rich.json
步骤3: 创建产品配置文件
// products/my_product.json
{
"version": "3.0",
"product_name": "my-custom-device",
"device_company": "mycompany",
"target_cpu": "arm64",
"board": "myboard",
"type": "standard",
"inherit": [
"productdefine/common/inherit/rich.json",
"productdefine/common/inherit/phone.json"
],
"subsystems": [
{
"subsystem": "custom",
"components": [
{
"component": "my_custom_feature",
"features": ["custom_feature_enable=true"]
}
]
}
]
}
配置验证流程
✅ 验证步骤
- 语法检查: JSON格式验证
- 依赖检查: 子系统依赖关系
- 内存估算: 内存占用计算
- 功能测试: 核心功能验证
🔍 验证工具
# 使用build.sh验证
./build.sh --product-name my_product --check-only
# 查看最终部件列表
cat out/preloader/my_product/parts.json
🔍 配置分析
系统类型详细配置对比
以下是三种系统类型的详细组件对比:
📊 轻量系统 (mini_system.json)
{
"subsystems": [
{
"subsystem": "hiviewdfx",
"components": [
"hilog_lite",
"hievent_lite",
"hidumper_lite"
]
},
{
"subsystem": "startup",
"components": [
"bootstrap_lite",
"appspawn_lite"
]
},
{
"subsystem": "communication",
"components": [
"dsoftbus",
"ipc"
]
},
{
"subsystem": "security",
"components": [
"device_auth",
"huks_lite"
]
},
{
"subsystem": "commonlibrary",
"components": [
"utils_lite"
]
},
{
"subsystem": "updater",
"components": [
"sys_installer_lite"
]
},
{
"subsystem": "thirdparty",
"components": [
"libcoap",
"lwip",
"mbedtls",
"cJSON",
"zlib",
"libboundscheck",
"cmsis",
"nvram"
]
}
]
}
📊 小型系统 (small_system.json)
{
"subsystems": [
{
"subsystem": "hdf",
"components": [
"hdf_core",
"hdf_driver_framework",
"hdf_device_desc",
"hdf_config_manager",
"hdf_platform"
]
},
{
"subsystem": "kernel",
"components": [
"liteos_a"
]
},
{
"subsystem": "hiviewdfx",
"components": [
"hilog_lite",
"hievent_lite",
"hidumper_lite"
]
},
{
"subsystem": "startup",
"components": [
"bootstrap_lite",
"appspawn_lite",
"init_lite"
]
},
{
"subsystem": "communication",
"components": [
"dsoftbus",
"ipc",
"samgr_lite"
]
},
{
"subsystem": "security",
"components": [
"device_auth",
"huks_lite",
"access_token_lite"
]
},
{
"subsystem": "commonlibrary",
"components": [
"c_utils",
"utils_lite"
]
},
{
"subsystem": "updater",
"components": [
"sys_installer_lite"
]
},
{
"subsystem": "thirdparty",
"components": [
"libcoap",
"lwip",
"mbedtls",
"cJSON",
"zlib",
"libboundscheck",
"cmsis",
"nvram",
"musl",
"libffi"
]
}
]
}
📊 标准系统 (standard_system.json)
{
"subsystems": [
{
"subsystem": "hdf",
"components": [
"hdf_core",
"hdf_driver_framework",
"hdf_device_desc",
"hdf_config_manager",
"hdf_platform",
"hdf_usb"
]
},
{
"subsystem": "communication",
"components": [
"ipc",
"dsoftbus",
"net_manager"
]
},
{
"subsystem": "systemabilitymgr",
"components": [
"samgr",
"safwk"
]
},
{
"subsystem": "hiviewdfx",
"components": [
"hilog",
"hitrace",
"hidumper",
"hievent",
"hisysevent",
"hiview",
"faultloggerd"
]
},
{
"subsystem": "security",
"components": [
"access_token",
"huks",
"deviceauth",
"selinux_adapter",
"dataclassification",
"iam"
]
},
{
"subsystem": "commonlibrary",
"components": [
"c_utils",
"memory_utils"
]
},
{
"subsystem": "updater",
"components": [
"update_service",
"update_engine"
]
},
{
"subsystem": "developtools",
"components": [
"hdc",
"hidumper",
"hitrace"
]
},
{
"subsystem": "thirdparty",
"components": [
"mbedtls",
"openssl",
"zlib",
"jsoncpp",
"libxml2",
"libcurl",
"cares",
"nghttp2",
"libffi",
"libunwind",
"libcxx",
"libcxxabi",
"compiler_rt",
"libc",
"musl",
"e2fsprogs",
"f2fs-tools",
"exfatprogs",
"dosfstools"
]
}
]
}
设备形态配置详解
🖥️ 无头系统 (headless.json)
专为无显示界面的设备设计,如服务器、网关:
{
"version": "3.0",
"subsystems": [
{
"subsystem": "build",
"components": [
"build_framework"
]
},
{
"subsystem": "startup",
"components": [
"startup_l2",
"init",
"appspawn"
]
},
{
"subsystem": "hiviewdfx",
"components": [
"hilog",
"hitrace",
"hidumper",
"hievent",
"hisysevent",
"hiview",
"faultloggerd"
]
},
{
"subsystem": "distributedhardware",
"components": [
"device_manager"
]
},
{
"subsystem": "security",
"components": [
"device_auth",
"huks",
"access_token",
"selinux_adapter",
"dataclassification",
"iam"
]
},
{
"subsystem": "account",
"components": [
"os_account"
]
},
{
"subsystem": "communication",
"components": [
"ipc",
"dsoftbus",
"net_manager",
"bluetooth"
]
},
{
"subsystem": "ability",
"components": [
"ability_runtime",
"ability_base",
"abilityms",
"ability_manager",
"abilityms_client"
]
},
{
"subsystem": "arkui",
"components": [
"napi"
]
}
]
}
📹 IPCamera (ipcamera.json)
专为网络摄像头设计,集成多媒体能力:
{
"version": "3.0",
"subsystems": [
{
"subsystem": "arkui",
"components": [
"ace_engine",
"napi"
]
},
{
"subsystem": "account",
"components": [
"os_account"
]
},
{
"subsystem": "distributeddatamgr",
"components": [
"preferences",
"kv_store",
"relational_store",
"data_storage",
"distributeddata"
]
},
{
"subsystem": "filemanagement",
"components": [
"user_file_service",
"file_api",
"storage_manager",
"filemanagement_libn"
]
},
{
"subsystem": "security",
"components": [
"appverify",
"deviceauth",
"huks",
"access_token",
"selinux_adapter",
"dataclassification"
]
},
{
"subsystem": "hdf",
"components": [
"hdf_core",
"hdf_driver_framework",
"hdf_device_desc",
"hdf_config_manager",
"hdf_platform",
"hdf_usb",
"hdf_input",
"hdf_display",
"hdf_sensor",
"hdf_audio",
"hdf_camera"
]
},
{
"subsystem": "multimedia",
"components": [
"camera_framework",
"camera_standard",
"media_standard",
"audio_standard",
"image_standard"
]
},
{
"subsystem": "thirdparty",
"components": [
"curl",
"mbedtls",
"openssl",
"zlib",
"jsoncpp",
"libxml2",
"libyuv",
"libjpeg_turbo",
"libpng",
"libwebp",
"ffmpeg",
"alsa_lib"
]
}
]
}
配置字段深度解析
🔧 关键配置字段说明
字段名 | 数据类型 | 必填 | 说明 | 示例值 |
---|---|---|---|---|
version | string | ✅ | 配置格式版本 | “3.0” |
product_name | string | ✅ | 产品唯一标识符 | “my_custom_device” |
device_company | string | ✅ | 厂商代码 | “hisilicon” |
target_cpu | string | ✅ | CPU架构 | “arm64/arm/x86/riscv” |
board | string | ✅ | 开发板型号 | “hi3516dv300” |
type | string | ✅ | 系统类型 | “standard/small/mini” |
enable_ramdisk | boolean | ❌ | 启用ramdisk加速 | true/false |
build_selinux | boolean | ❌ | 启用SELinux安全 | true/false |
inherit | array | ❌ | 继承的模板列表 | [“rich.json”, “phone.json”] |
subsystems | array | ✅ | 子系统配置数组 | […] |
🎯 子系统配置结构
{
"subsystems": [
{
"subsystem": "arkui", // 子系统名称
"components": [ // 组件列表
{
"component": "ace_engine", // 组件名称
"features": [ // 特性配置 (可选)
]
}
]
}
]
}
高级配置技巧
🔄 动态配置继承
{
"inherit": [
"productdefine/common/base/standard_system.json",
"productdefine/common/inherit/rich.json",
"productdefine/common/inherit/phone.json"
],
"subsystems": [
{
"subsystem": "telephony",
"components": [
{
"component": "cellular_call",
"features": [
"5g_support=true",
"volte_enable=true"
]
}
]
}
]
}
⚙️ 条件编译配置
{
"subsystems": [
{
"subsystem": "graphic",
"components": [
{
"component": "graphic_2d",
"features": [
"gpu_acceleration=false",
"gpu_acceleration=true",
]
}
]
}
]
}
最佳实践案例
📱 创建自定义手机产品
场景: 基于标准系统创建一款中低端4G手机
- 基础配置 (products/my_phone.json):
{
"version": "3.0",
"product_name": "my-4g-phone",
"device_company": "mycorp",
"target_cpu": "arm",
"board": "mt6765",
"type": "standard",
"enable_ramdisk": true,
"build_selinux": true,
"inherit": [
"productdefine/common/base/standard_system.json",
"productdefine/common/inherit/phone.json"
],
"subsystems": [
{
"subsystem": "telephony",
"components": [
{
"component": "cellular_call",
"features": [
]
}
]
},
{
"subsystem": "multimedia",
"components": [
{
"component": "camera_standard",
"features": [
]
}
]
}
]
}
- 内存优化配置:
{
"subsystems": [
{
"subsystem": "graphic",
"components": [
{
"component": "graphic_2d",
"features": [
"gpu_acceleration=true",
"memory_optimization=aggressive"
]
}
]
}
]
}
⌚ 创建智能手表产品
场景: 基于小型系统创建低功耗智能手表
{
"version": "3.0",
"product_name": "my-smartwatch",
"device_company": "mycorp",
"target_cpu": "arm",
"board": "nrf52840",
"type": "small",
"enable_ramdisk": false,
"build_selinux": false,
"inherit": [
"productdefine/common/base/small_system.json",
"productdefine/common/inherit/wearable.json"
],
"subsystems": [
{
"subsystem": "sensors",
"components": [
"accelerometer",
"gyroscope",
"heart_rate_sensor"
]
},
{
"subsystem": "powermgr",
"components": [
{
"component": "power_manager",
"features": [
"low_power_mode=extreme",
"always_on_display=true"
]
}
]
}
]
}
🚨 常见错误及解决方案
错误类型 | 错误信息 | 根因分析 | 解决方案 |
---|---|---|---|
JSON格式错误 | Expecting ‘,’ delimiter | 缺少逗号或括号 | 使用JSON验证工具 |
依赖缺失 | Component ‘xxx’ not found | 未包含所需子系统 | 检查inherit配置 |
内存溢出 | Out of memory | 组件过多或配置过大 | 裁剪不必要的组件 |
架构不匹配 | CPU arch incompatible | target_cpu配置错误 | 检查CPU架构设置 |
权限错误 | Permission denied | SELinux配置问题 | 调整安全策略 |
配置模板速查表
📋 快速配置模板
设备类型 | 基础系统 | 推荐模板 | 关键特性 |
---|---|---|---|
智能手机 | standard | phone.json + rich.json | 通话、4G/5G、相机 |
平板电脑 | standard | tablet.json + rich.json | 大屏、无通话 |
智能手表 | small | wearable.json | 低功耗、健康监测 |
智能电视 | standard | tv.json + rich.json | 4K视频、遥控器 |
IPCamera | standard | ipcamera.json | 视频采集、网络传输 |
IoT网关 | small | headless.json | 无界面、网络功能 |
开发板 | standard | rich.json | 全功能、开发调试 |
📚 附录:源码文件清单
📁 基础系统类型文件
productdefine/common/base/
├── mini_system.json # 轻量系统 - 128KB内存
├── small_system.json # 小型系统 - 1MB内存
└── standard_system.json # 标准系统 - 128MB内存
📁 设备形态模板文件
productdefine/common/inherit/
├── rich.json # 全功能模板
├── phone.json # 手机专用模板
├── tablet.json # 平板专用模板
├── wearable.json # 穿戴设备模板
├── tv.json # 智能电视模板
├── 2in1.json # 二合一设备模板
├── headless.json # 无头系统模板
├── ipcamera.json # 网络摄像头模板
└── chipset_common.json # 芯片通用模板
📁 产品配置示例
productdefine/common/products/
├── ohos-sdk.json # SDK产品配置
├── system_arm_default.json # 32位ARM系统
├── system_arm64_default.json # 64位ARM系统
├── system_riscv32.json # 32位RISC-V系统
└── system_riscv64.json # 64位RISC-V系统
📊 组件统计对比表
系统类型 | 子系统数量 | 组件总数 | 第三方库 | 典型内存占用 |
---|---|---|---|---|
mini_system | 7 | 25+ | 8个 | 128KB-512KB |
small_system | 9 | 35+ | 11个 | 1MB-16MB |
standard_system | 10 | 50+ | 20个 | 128MB-512MB |
rich.json | 23 | 80+ | 40+ | 512MB+ |
phone.json | 25+ | 100+ | 45+ | 1GB+ |
🎯 实际应用建议
选择系统类型的决策树
硬件资源评估:
├── 内存 < 1MB → mini_system.json
├── 内存 1-128MB → small_system.json
└── 内存 >= 128MB → standard_system.json
功能需求评估:
├── 需要完整UI → standard + rich.json
├── 需要通话功能 → standard + phone.json
├── 需要穿戴功能 → small + wearable.json
└── 需要摄像头 → standard + ipcamera.json
内存优化检查清单
- 移除不必要的子系统
- 禁用GPU加速 (gpu_acceleration=false)
- 减少第三方库依赖
- 使用轻量级组件 (xxx_lite)
- 启用内存压缩 (memory_compression=true)
- 调整缓存大小 (cache_size=small)
编译验证流程
# 完整编译
./build.sh --product-name my_product --ccahe