移植 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 原生窗口和输入事件处理,此时已经实现,不过我们下一篇文章再介绍,先放一张图上来看看:

相关推荐
鸿蒙开发3 小时前
共享 Schema 与 SchemaFactory
前端·后端·harmonyos
二流小码农6 小时前
HarmonyOS开发助手,0代码让元服务从开发到上架
android·ios·harmonyos
产品人卫朋7 小时前
MTL如何把市场机会转化为有效销售线索?
人工智能·华为·产品经理·ipd流程·流程架构·mtl流程
贾伟康8 小时前
【共创稿事节】第一张服务卡片:2×2 今日菜谱的克制设计
microsoft·harmonyos·arkts·鸿蒙
2501_944525768 小时前
开源鸿蒙平台 KMP_CMP 三方库「Atrium」适配全流程
microsoft·开源·harmonyos
less_121388 小时前
鸿蒙 WPS Open SDK 二开复盘:从注册、打开到回传的完整能力链
服务器·sdk·harmonyos·wps·鸿蒙开发
2601_949575869 小时前
开源鸿蒙平台 CMP 三方库「voyager」适配全流程
华为·开源·harmonyos
2601_9495758610 小时前
开源鸿蒙平台 CMP 三方库「compose-multiplatform-2048」适配全流程
华为·开源·harmonyos
网络豆13 小时前
开源鸿蒙平台 CMP 三方库「kmp-compose-tab-container」适配全流程
华为·开源·harmonyos
2601_9495758613 小时前
开源鸿蒙平台 KMP_CMP 三方库「PreCompose」适配全流程
华为·开源·harmonyos