HarmonyOS(鸿蒙操作系统)是华为开发的全场景分布式操作系统,基于微内核架构设计,支持多设备协同、一次开发多端部署。其核心设计理念是”1+8+N”全场景战略:
| 概念 | 说明 |
|---|
| 1 | 手机作为核心入口 |
| 8 | 平板、PC、手表、耳机、车机、智慧屏等 |
| N | 泛 IoT 设备(冰箱、空调、摄像头等) |
| 版本 | 时间 | 特点 |
|---|
| HarmonyOS 1.0 | 2019 | 智慧屏首发,微内核架构 |
| HarmonyOS 2.0 | 2020 | 开源 OpenHarmony,手机适配 |
| HarmonyOS 3.0 | 2022 | 超级终端、原子化服务 |
| HarmonyOS 4.0 | 2023 | 方舟引擎、AI 大模型集成 |
| HarmonyOS NEXT | 2024 | 纯血鸿蒙,不再兼容安卓 |
| 特性 | 描述 |
|---|
| 分布式架构 | 多设备虚拟化为一个超级终端 |
| 微内核设计 | 内核只保留最基础功能,安全可靠 |
| 一次开发多端部署 | 一套代码适配手机、平板、手表等多种设备 |
| 原子化服务 | 免安装、即用即走的轻量服务 |
| 方舟编译器 | 静态编译优化,提升运行效率 |
┌─────────────────────────────────────────────┐
│ 应用层 (Application) │
│ 系统应用 │ 第三方应用 │ 原子化服务 │
├─────────────────────────────────────────────┤
│ 框架层 (Framework) │
│ ArkUI │ Ability │ AI │ 分布式软总线 │
├─────────────────────────────────────────────┤
│ 系统服务层 (System Service) │
│ 基础服务 │ 硬件服务 │ 数据管理 │ 安全隐私 │
├─────────────────────────────────────────────┤
│ 内核层 (Kernel) │
│ Linux Kernel │ LiteOS │ 安全增强 │ HDF │
└─────────────────────────────────────────────┘
| 内核类型 | 适用设备 | 特点 |
|---|
| Linux Kernel | 手机、平板、PC | 功能完整、性能强大 |
| LiteOS-A | 智慧屏、手表 | 轻量级、支持进程 |
| LiteOS-M | IoT 小设备 | 极轻量、MCU 级别 |
FA 模型是 HarmonyOS 早期版本的应用开发模型,基于 Ability 作为基本单元:
| 概念 | 说明 |
|---|
| FA (Feature Ability) | 提供 UI 界面的 Ability |
| PA (Particle Ability) | 无 UI 的后台服务 Ability |
| Page | FA 中的单个页面 |
| AbilitySlice | Page 中的子切片(已废弃) |
Stage 模型是 HarmonyOS 3.0+ 推荐的应用开发模型,设计更加现代化:
| 概念 | 说明 |
|---|
| UIAbility | 提供 UI 界面的能力,替代 FA |
| ExtensionAbility | 无 UI 的扩展能力,替代 PA |
| WindowStage | 窗口管理,一个 UIAbility 可持有多个窗口 |
| UIContent | 页面内容加载入口 |
| 维度 | FA 模型 | Stage 模型 |
|---|
| 基本单元 | Ability | UIAbility / ExtensionAbility |
| 生命周期 | 较复杂 | 简化清晰 |
| 窗口管理 | 自动单窗口 | 支持多窗口 |
| 后台任务 | PA | ExtensionAbility |
| 推荐程度 | 已不推荐 | 官方推荐 |
MyApplication/
├── AppScope/ # 应用全局配置
│ ├── app.json5 # 应用配置(版本号、图标等)
│ └── resources/ # 全局资源
├── entry/ # 主模块
│ ├── src/main/
│ │ ├── ets/ # ArkTS 源码
│ │ │ ├── entryability/ # UIAbility
│ │ │ │ └── EntryAbility.ets
│ │ │ └── pages/ # 页面
│ │ │ └── Index.ets
│ │ ├── resources/ # 模块资源
│ │ └── module.json5 # 模块配置
│ ├── build-profile.json5 # 构建配置
│ └── oh-package.json5 # 包依赖
└── build-profile.json5 # 项目构建配置
| 项目 | 最低要求 | 推荐配置 |
|---|
| 系统 | Windows 10 64位 / macOS 10.15 | Windows 11 / macOS 13+ |
| 内存 | 8 GB | 16 GB+ |
| 硬盘 | 10 GB | SSD 50 GB+ |
| CPU | x86_64,8 核 | x86_64,16 核+ |
- 访问华为开发者官网:https://developer.huawei.com
- 注册华为开发者账号
- 下载 DevEco Studio 最新版本
- 运行安装程序,选择安装路径
- 首次启动会自动下载 SDK 和工具链
# SDK 路径配置
# File → Settings → SDK → HarmonyOS SDK Location
# SDK 版本管理
# File → Settings → SDK → HarmonyOS → 选择 API 版本
| SDK 组件 | 说明 |
|---|
| HarmonyOS SDK | 系统核心 API |
| OpenHarmony SDK | 开源版本 SDK |
| Native SDK | C/C++ 原生开发 SDK |
| Previewer SDK | 预览器渲染引擎 |
# 创建模拟器
# Tools → Device Manager → New Emulator
# 推荐配置
# Phone: API 12, 8GB RAM, 1080x2340
# Tablet: API 12, 8GB RAM, 2560x1600
# Wearable: API 12, 1GB RAM, 466x466
| 模拟器类型 | 适用场景 | 最低内存 |
|---|
| Phone | 手机应用开发 | 4 GB |
| Tablet | 平板应用开发 | 4 GB |
| Wearable | 手表应用开发 | 2 GB |
| TV | 智慧屏应用开发 | 4 GB |
| Car | 车机应用开发 | 4 GB |
- 打开 DevEco Studio → File → New → Create Project
- 选择 Empty Ability 模板
- 填写项目信息:
| 字段 | 值 |
|---|
| Project Name | HelloHarmonyOS |
| Bundle Name | com.example.hello |
| Save Location | 选择项目保存路径 |
| Compatible SDK | API 12 |
| Module Language | ArkTS |
| Device Type | Phone / Tablet / Wearable |
// entry/src/main/ets/entryability/EntryAbility.ets
import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
console.info('[EntryAbility] onCreate');
}
onDestroy(): void {
console.info('[EntryAbility] onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
console.info('[EntryAbility] onWindowStageCreate');
// 加载主页面
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
console.error(`Failed to load content: ${JSON.stringify(err)}`);
return;
}
console.info('Succeeded in loading content');
});
}
onWindowStageDestroy(): void {
console.info('[EntryAbility] onWindowStageDestroy');
}
onForeground(): void {
console.info('[EntryAbility] onForeground');
}
onBackground(): void {
console.info('[EntryAbility] onBackground');
}
}
// entry/src/main/ets/pages/Index.ets
@Entry
@Component
struct Index {
@State message: string = 'Hello HarmonyOS!';
build() {
Column() {
// 标题文本
Text(this.message)
.fontSize(32)
.fontWeight(FontWeight.Bold)
.fontColor('#1a73e8')
.margin({ top: 100 })
// 副标题
Text('欢迎使用鸿蒙开发')
.fontSize(18)
.fontColor('#666666')
.margin({ top: 16 })
// 按钮
Button('点击问候')
.width('60%')
.height(48)
.fontSize(18)
.backgroundColor('#1a73e8')
.borderRadius(24)
.margin({ top: 40 })
.onClick(() => {
this.message = '你好,鸿蒙!';
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Center)
}
}
// entry/src/main/module.json5
{
module: {
name: 'entry',
type: 'entry',
description: '$string:module_desc',
mainElement: 'EntryAbility',
deviceTypes: ['phone', 'tablet', '2in1'],
deliveryWithInstall: true,
installationFree: false,
pages: '$profile:main_pages',
abilities: [
{
name: 'EntryAbility',
srcEntry: './ets/entryability/EntryAbility.ets',
description: '$string:EntryAbility_desc',
icon: '$media:layered_image',
label: '$string:EntryAbility_label',
startWindowIcon: '$media:startIcon',
startWindowBackground: '$color:start_window_background',
exported: true,
skills: [
{
entities: ['entity.system.home'],
actions: ['action.system.home'],
},
],
},
],
},
}
# 构建项目
# Build → Build Hap(s)/APP(s) → Build Hap(s)
# 运行到模拟器
# Run → Run 'entry'
# 运行到真机
# 1. 开启开发者模式
# 2. USB 连接设备
# 3. Run → Run 'entry'
# 日志查看
# HiLog 工具窗口
console.info('[MyTag] This is an info log');
console.error('[MyTag] This is an error log');
console.warn('[MyTag] This is a warning log');
| 工具 | 用途 | 命令/入口 |
|---|
| DevEco Studio | IDE 开发环境 | 图形界面 |
| hvigor | 构建工具 | hvigorw assembleHap |
| hdc | 设备调试工具 | hdc install entry.hap |
| SmartPerf | 性能分析工具 | DevEco → Profiler |
| Previewer | 实时预览 | DevEco → Previewer |
| Inspector | UI 组件检查 | DevEco → ArkUI Inspector |
概述与环境搭建 → ArkTS 与 ArkUI → UI 组件与动画 → 网络与数据持久化 → 多媒体与设备能力
↓ ↓ ↓ ↓ ↓
理解架构 掌握语法 构建界面 数据交互 完整应用
| 阶段 | 目标 | 建议时间 |
|---|
| 入门 | 搭建环境、Hello World | 1-2 天 |
| 基础 | ArkTS 语法、ArkUI 组件 | 1-2 周 |
| 进阶 | 动画、网络、数据持久化 | 2-3 周 |
| 实战 | 完整应用开发 | 2-4 周 |