前置知识: JavaScriptTypeScript

概述与环境搭建

7 minBeginner2026/6/14

HarmonyOS 系统架构、FA 模型与 Stage 模型、DevEco Studio 安装配置、SDK 管理、模拟器配置与第一个 Hello World 应用。

1. HarmonyOS 概述

1.1 什么是 HarmonyOS

HarmonyOS(鸿蒙操作系统)是华为开发的全场景分布式操作系统,基于微内核架构设计,支持多设备协同、一次开发多端部署。其核心设计理念是”1+8+N”全场景战略:

概念说明
1手机作为核心入口
8平板、PC、手表、耳机、车机、智慧屏等
N泛 IoT 设备(冰箱、空调、摄像头等)

1.2 HarmonyOS 发展历程

版本时间特点
HarmonyOS 1.02019智慧屏首发,微内核架构
HarmonyOS 2.02020开源 OpenHarmony,手机适配
HarmonyOS 3.02022超级终端、原子化服务
HarmonyOS 4.02023方舟引擎、AI 大模型集成
HarmonyOS NEXT2024纯血鸿蒙,不再兼容安卓

1.3 核心特性

特性描述
分布式架构多设备虚拟化为一个超级终端
微内核设计内核只保留最基础功能,安全可靠
一次开发多端部署一套代码适配手机、平板、手表等多种设备
原子化服务免安装、即用即走的轻量服务
方舟编译器静态编译优化,提升运行效率

2. 系统架构

2.1 分层架构

┌─────────────────────────────────────────────┐
│              应用层 (Application)             │
│   系统应用  │  第三方应用  │  原子化服务       │
├─────────────────────────────────────────────┤
│              框架层 (Framework)               │
│  ArkUI  │  Ability  │  AI  │  分布式软总线    │
├─────────────────────────────────────────────┤
│              系统服务层 (System Service)       │
│  基础服务 │ 硬件服务 │ 数据管理 │ 安全隐私    │
├─────────────────────────────────────────────┤
│              内核层 (Kernel)                  │
│  Linux Kernel │ LiteOS │ 安全增强 │ HDF      │
└─────────────────────────────────────────────┘

2.2 内核层详解

内核适用设备特点
Linux Kernel手机、平板、PC功能完整、性能强大
LiteOS-A智慧屏、手表轻量级、支持进程
LiteOS-MIoT 小设备极轻量、MCU 级别

3. FA 模型与 Stage 模型

3.1 FA 模型(Feature Ability)

FA 模型是 HarmonyOS 早期版本的应用开发模型,基于 Ability 作为基本单元:

概念说明
FA (Feature Ability)提供 UI 界面的 Ability
PA (Particle Ability)无 UI 的后台服务 Ability
PageFA 中的单个页面
AbilitySlicePage 中的子切片(已废弃)

3.2 Stage 模型

Stage 模型是 HarmonyOS 3.0+ 推荐的应用开发模型,设计更加现代化:

概念说明
UIAbility提供 UI 界面的能力,替代 FA
ExtensionAbility无 UI 的扩展能力,替代 PA
WindowStage窗口管理,一个 UIAbility 可持有多个窗口
UIContent页面内容加载入口

3.3 两种模型对比

维度FA 模型Stage 模型
基本单元AbilityUIAbility / ExtensionAbility
生命周期较复杂简化清晰
窗口管理自动单窗口支持多窗口
后台任务PAExtensionAbility
推荐程度已不推荐官方推荐

3.4 Stage 模型应用结构

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          # 项目构建配置

4. DevEco Studio 安装配置

4.1 系统要求

项目最低要求推荐配置
系统Windows 10 64位 / macOS 10.15Windows 11 / macOS 13+
内存8 GB16 GB+
硬盘10 GBSSD 50 GB+
CPUx86_64,8 核x86_64,16 核+

4.2 下载与安装

  1. 访问华为开发者官网:https://developer.huawei.com
  2. 注册华为开发者账号
  3. 下载 DevEco Studio 最新版本
  4. 运行安装程序,选择安装路径
  5. 首次启动会自动下载 SDK 和工具链

4.3 SDK 管理

# SDK 路径配置
# File → Settings → SDK → HarmonyOS SDK Location

# SDK 版本管理
# File → Settings → SDK → HarmonyOS → 选择 API 版本
SDK 组件说明
HarmonyOS SDK系统核心 API
OpenHarmony SDK开源版本 SDK
Native SDKC/C++ 原生开发 SDK
Previewer SDK预览器渲染引擎

4.4 模拟器配置

# 创建模拟器
# 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

5. 第一个 Hello World 应用

5.1 创建项目

  1. 打开 DevEco Studio → File → New → Create Project
  2. 选择 Empty Ability 模板
  3. 填写项目信息:
字段
Project NameHelloHarmonyOS
Bundle Namecom.example.hello
Save Location选择项目保存路径
Compatible SDKAPI 12
Module LanguageArkTS
Device TypePhone / Tablet / Wearable

5.2 项目入口代码

// 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');
  }
}

5.3 页面代码

// 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)
  }
}

5.4 应用配置

// 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'],
          },
        ],
      },
    ],
  },
}

5.5 运行与调试

# 构建项目
# 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');

6. 常见开发工具

工具用途命令/入口
DevEco StudioIDE 开发环境形界面
hvigor构建工具hvigorw assembleHap
hdc设备调试工具hdc install entry.hap
SmartPerf性能分析工具DevEco → Profiler
Previewer实时预览DevEco → Previewer
InspectorUI 组件检查DevEco → ArkUI Inspector

7. 学习路线建议

概述与环境搭建 → ArkTS 与 ArkUI → UI 组件与动画 → 网络与数据持久化 → 多媒体与设备能力
     ↓                ↓               ↓                ↓                  ↓
  理解架构        掌握语法         构建界面         数据交互           完整应用
阶段目标建议时间
入门搭建环境、Hello World1-2 天
基础ArkTS 语法、ArkUI 组件1-2 周
进阶动画、网络、数据持久化2-3 周
实战完整应用开发2-4 周