一、环境配置:基础工具链搭建
-
开发工具安装
- DevEco Studio 5.0+:安装时勾选 HarmonyOS SDK(API 12+)、ArkTS 编译器及本地模拟器。
- Node.js 18.17+:需支持 ES2020+ 语法,验证命令:
node -v
和npm -v
。 - React Native 0.72.5:仅此版本官方适配 HarmonyOS,避免高版本线程冲突。
-
环境变量设置
- 添加鸿蒙工具链路径(如
export PATH="/Applications/DevEco-Studio.app/.../toolchains:$PATH"
)。 - 配置调试端口:
export HDC_SERVER_PORT=7035
。
- 添加鸿蒙工具链路径(如
-
依赖库集成
- 安装鸿蒙桥接库:
npm install @react-native-oh/react-native-harmony
。 - 修改
metro.config.js
启用鸿蒙支持:const { createHarmonyMetroConfig } = require('@react-native-oh/react-native-harmony/metro.config'); module.exports = mergeConfig(getDefaultConfig(__dirname), createHarmonyMetroConfig()); ```[5](@ref)。
- 安装鸿蒙桥接库:
二、项目初始化与工程结构设计
- 创建项目
npx react-native@0.72.5 init FoodApp --template react-native-template-harmonyos ```[3,4](@ref)。
- 目录结构调整
- 替换默认 Android/iOS 目录,创建鸿蒙专用层级:
├── common_har # 公共工具 ├── harmony_hsp # 原生封装层(分布式/AI能力) └── rn_components # RN组件层 ```[4,8](@ref)。
- 替换默认 Android/iOS 目录,创建鸿蒙专用层级:
- 入口配置
- 在
EntryAbility.ets
中预加载 JS Bundle 加速启动:preloadJSBundle(this.context, 'index.bundle'); ```[5,6](@ref)。
- 在
三、核心功能适配与性能优化
-
组件与布局适配
- 组件映射:使用
<HarmonyListView>
替代<FlatList>
,启用recycleEnabled=true
提升列表渲染性能。 - 样式规范:鸿蒙 Flex 布局与 Android 存在差异,优先使用绝对单位(如
px
)。
- 组件映射:使用
-
原生能力桥接
- 分布式数据同步(跨设备菜谱同步):
import distributedKVStore from '@ohos.distributedKVStore'; kvManager.put('favorite_recipe', JSON.stringify(recipeData)); ```[4,8](@ref)。
- AI语音集成(菜谱语音搜索):
HarmonyAI.startSpeechRecognition({ language: 'zh_CN' }); ```[5,6](@ref)。
- 分布式数据同步(跨设备菜谱同步):
-
性能关键点
- 包体积优化:通过
react-native-harmony-cli
进行 Tree-Shaking,移除未使用库。 - 内存管理:调用鸿蒙原生接口释放图片缓存:
HarmonyMemory.releaseBuffer(imageHandle); ```[6](@ref)。
- 包体积优化:通过
四、调试与测试准备
-
真机调试流程
- 启用设备调试模式:
hdc shell setprop persist.debug.hdc.enable 1
。 - 端口转发:
hdc forward tcp:8081 tcp:8081
。
- 启用设备调试模式:
-
性能监控工具
- 集成
@react-native-harmony/host/PerformanceMonitor
,监控 FPS、内存、CPU 指标。
- 集成
五、上架前合规检查
- 权限声明
- 在
module.json5
中声明分布式权限:"requestPermissions": [{ "name": "ohos.permission.DISTRIBUTED_DATASYNC" }] ```[3,4](@ref)。
- 在
- 隐私合规
- 用户饮食数据需脱敏处理,避免直接传输原始数据。