HarmonyOS 鸿蒙Next中arkts应用开发如何使用编译好的静态库
HarmonyOS 鸿蒙Next中arkts应用开发如何使用编译好的静态库
// oh-package.json5
{
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"libroot.o": "file:./libs/libroot"
}
}
// build-profile.json5
{
"apiType": "stageMode",
"buildOption": {
"arkOptions": {
"runtimeOnly": {
"packages": [
"libroot.o"
]
}
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}
// libs/libroot/index.d.ts
export const add: (a: number, b: number) => number;
// Index.ets
import { hilog } from "@kit.PerformanceAnalysisKit";
let module: ESObject = loadNativeModule("libroot.o");
@Entry
@Component
struct CityList {
build() {
List() {
ListItem() {
Text('北京').fontSize(24)
}
ListItem() {
Text('杭州').fontSize(24)
}
ListItem() {
Text('上海').fontSize(24)
}
ListItem() {
Button(){
Text("add")
}.onClick(_ => {
let sum:number = module.add(1,2);
hilog.debug(0xFF00, "testTag","%{public}d",sum);
})
}
}
.backgroundColor('#FFF1F3F5')
.alignListItem(ListItemAlign.Center)
}
}
在应用打开后(DevStudio虚拟机和真实设备)报错
Module name:com.cdev.auth.totp
Version:1.0.0
VersionCode:1000000
PreInstalled:No
Foreground:Yes
Pid:24174
Uid:20020045
Reason:BusinessError
Error name:BusinessError
Error message:load native module failed.
Error code:
Stacktrace:
at anonymous (entry/src/main/ets/pages/Index.ets:24:34)
解包生成的hap文件,未在文件中发现对应的libs目录以及静态库文件
静态库文件由zig编译生成
const std = @import("std");
const testing = std.testing;
pub export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}
编译指令为
zig build-lib -static -O ReleaseSafe -target aarch64-linux-ohos src/root.zig
更多关于HarmonyOS 鸿蒙Next中arkts应用开发如何使用编译好的静态库的实战教程也可以访问 https://www.itying.com/category-93-b0.html
直接对接上层的Arkts 只支持动态库。静态库需要包一层。
https://juejin.cn/post/7368741477214486538
zig可以参考这个demo。
https://github.com/openharmony-zig/zig-addon/tree/77762b63c6238b6c2e6e43960160c778c2be2a1d
更多关于HarmonyOS 鸿蒙Next中arkts应用开发如何使用编译好的静态库的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
感谢,
ArkTS应用开发中使用编译好的静态库主要为配置依赖和导入使用,下面是步骤
配置静态库依赖
在应用主模块的oh-package.json5文件中添加静态库路径依赖:
"dependencies": {
"my_static_lib": "file:../my_static_lib" // 路径指向静态库模块目录
}
然后在DevEco Studio中执行ohpm install命令,将静态库关联到主模块的oh_modules目录下。
导入静态库模块
在ArkTS文件中通过import语句引入静态库:
import { ComponentName } from 'my_static_lib'
然后使用静态库组件/接口
@Component
struct MainPage {
build() {
Column() {
// 调用静态库导出的组件
ComponentFromLib()
}
}
}
HAR静态库使用流程
在应用模块的 oh-package.json5 文件中添加对本地HAR包的依赖
"dependencies": {
"my_library": "file:../my_library" // HAR包所在目录
}
// 导入HAR中的组件或接口
import { CustomComponent } from 'my_library'
@Component
struct MainPage {
build() {
Column() {
CustomComponent() // 使用HAR中的组件
}
}
}
期待HarmonyOS能在未来推出更多针对特定场景的优化功能。
感谢,之前理解错了,
好,我再去找找文档,
在HarmonyOS Next中使用静态库时,静态库文件需要正确打包到HAP中。从你的配置来看,问题可能在于静态库未正确集成到构建产物中。
检查build-profile.json5
中的runtimeOnly
配置,确保静态库路径正确。同时,确认libs
目录结构符合要求,静态库文件应位于libs/libroot/
路径下,且包含正确的.o
文件及类型定义。
建议验证静态库的编译目标架构是否与HarmonyOS Next兼容,并检查构建日志确认库文件是否被正确打包。