移植 AWTK 到 纯血鸿蒙 (HarmonyOS NEXT) 系统 (4) - 平台适配

在移植 AWTK 到 HarmonyOS NEXT 系统之前,我们需要先完成平台适配,比如文件、多线程(线程和同步)、时间、动态库和资源管理。

1. 文件

HarmonyOS NEXT 支持标准的 POSIX 文件操作接口,我们可以直接使用下面的代码:

复制代码
src/platforms/pc/fs_os.c

2. 多线程

HarmonyOS NEXT 支持标准的 POSIX 线程操作接口,我们可以直接使用下面的代码:

复制代码
src/platforms/pc/thread_with_pthread.c

3. 时间

HarmonyOS NEXT 支持标准的时间函数,我们可以直接使用下面的代码:

复制代码
src/platforms/pc/platform_time.c

4. 动态库

HarmonyOS NEXT 支持标准的动态库操作接口,我们可以直接使用下面的代码:

复制代码
src/platforms/pc/dl.c

5. 系统信息

AWTK 需要一些系统信息,比如 屏幕大小、屏幕密度、可写文件目录、临时目录、当前语言、字体缩放比例等。我们可以使用下面的代码从 UIAbilityContext 获取,然后传入到 native 层:

js 复制代码
    let context = getContext(this) as common.UIAbilityContext;
    let filesDir: string = context.filesDir;
    let tempDir: string = context.tempDir;
    let resourceManager = context.resourceManager;
    let resourceDir = context.resourceDir;
    let screenDensity: number = context.config.screenDensity || 640;
    let fontScale:number = context.config.fontSizeScale || 1;
    let language:string = context.config.language || "zh_CN";
    awtk_napi.init(resourceManager, filesDir, tempDir, screenDensity, fontScale, language);

6. 资源管理

资源管理比较麻烦一点,我们参考 AWTK-IOS 将资源打包成一个文件 assets.zip,放到应用程序的包里。在 HarmonyOS NEXT 系统中,我们可以把 assets.zip 放到 rawfile 目录下:

sh 复制代码
entry/src/main/resources/rawfile/assets.zip

然后通过资源管理器读取:

cpp 复制代码
    filename = "assets.zip";
    RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
    if (rawFile != nullptr) {
    }   
    long len = OH_ResourceManager_GetRawFileSize(rawFile);
    assetsData = std::make_unique<uint8_t[]>(len);

    int res = OH_ResourceManager_ReadRawFile(rawFile, assetsData.get(), len);

    OH_ResourceManager_CloseRawFile(rawFile);
    OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);

    assetsZipContent = assetsData.get();
    assetsZipSize = len;
    awtk_app_->Init(filesDir.c_str(), tempDir.c_str(), screenDensity, fontScale, language.c_str(), assetsZipContent, assetsZipSize);

7. 编译运行

AWTK 原生窗口和输入事件处理,此时已经实现,不过我们下一篇文章再介绍,先放一张图上来看看:

相关推荐
u***j32412 分钟前
HarmonyOS分布式能力核心技术深度解析
分布式·华为·harmonyos
7***n7517 分钟前
HarmonyOS分布式数据管理
分布式·华为·harmonyos
S***y3961 小时前
HarmonyOS在智能家居中的情景模式
华为·智能家居·harmonyos
IT充电站1 小时前
HarmonyOS 帧动画 animator
harmonyos
IT充电站1 小时前
HarmonyOS 位置服务全攻略:精准定位、地理编码与后台持续定位实现
harmonyos
IT充电站1 小时前
HarmonyOS 组件导航(Navigation)
harmonyos
6***37941 小时前
HarmonyOS在智能家居中的场景联动
华为·智能家居·harmonyos
u***u6852 小时前
HarmonyOS在智能手表中的开发
华为·harmonyos·智能手表
0***143 小时前
HarmonyOS系统安全机制
华为·harmonyos
m***D2864 小时前
HarmonyOS在智能家居中的家庭网络
华为·智能家居·harmonyos