HarmonyOS鸿蒙Next中ArkTS调C的实现方法

HarmonyOS鸿蒙Next中ArkTS调C的实现方法

import entry from 'libentry.so'
aboutToAppear() {
  AppStorage.setOrCreate<UIContext>("context", this.getUIContext());
  // 2. 在生命周期函数中注册接口
  // entry.OnClickXComponenButton();
  entry.RegisterArkTsInterface({
    createMixedEmbedded: createMixedEmbedded
  })
}

aboutToAppear不能调用c侧代码吗、和加载时机有关系吗、aboutToAppear时libentry.so还没有生成吗

Error message:is not callable

Stacktrace:

at aboutToAppear (entry/src/main/ets/pages/MainWindowNativeNode.ets:22:5)


更多关于HarmonyOS鸿蒙Next中ArkTS调C的实现方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

本地正常运行,aboutToAppear方法内可以执行so方法。和你说的加载时机没啥关系,大概率是你的so有问题把,但看你的堆栈没有有用信息,下面是我的demo,可以参考下。

demo:

import nativeFun from 'libentry.so';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  aboutToAppear(): void {
    let tt = nativeFun.add(1,2)
    console.log('aaaaaaa '+ tt.toString())
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
      }
      .width('100%')
    }
    .height('100%')
  }
}

日志

10-31 17:46:05.466   35276-31264   A00000/testTag                                        I     Ability onCreate
10-31 17:46:05.466   35276-31264   A00000/testTag                                        I     Ability onWindowStageCreate
10-31 17:46:05.467   35276-31264   A00000/testTag                                        I     Ability onForeground
10-31 17:46:05.592   35276-31264   A00000/testTag                                        I     Succeeded in loading the content.
10-31 17:46:05.607   35276-31264   A03d00/JSAPP                                          I     aaaaaaa 3

更多关于HarmonyOS鸿蒙Next中ArkTS调C的实现方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可是我在别的时机的地方可以在正常执行so的、只有在aboutToAppear()报上面的错误,

在HarmonyOS鸿蒙Next中,ArkTS调用C的实现方法主要依赖于Native API(Native Development Kit, NDK)。具体步骤如下:

  1. 创建Native模块:首先,在鸿蒙项目中创建一个Native模块,编写C/C++代码,并生成对应的动态库(如.so文件)。

  2. 定义Native接口:在ArkTS中,通过@ohos.napi模块定义Native接口,使用native关键字声明需要调用的C函数。

  3. 加载动态库:在ArkTS代码中,使用System.loadLibrary加载生成的动态库。

  4. 调用C函数:通过定义的Native接口,直接调用C函数。

示例代码:

import { native } from '@ohos.napi';

// 加载动态库
System.loadLibrary('mylib');

// 定义Native接口
@native
function nativeFunction(param: number): number;

// 调用C函数
let result = nativeFunction(42);

在C代码中,对应的函数声明如下:

#include <napi/native_api.h>

// 定义Native函数
napi_value NativeFunction(napi_env env, napi_callback_info info) {
    // 函数实现
}

// 注册Native函数
napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc = {"nativeFunction", 0, NativeFunction, 0, 0, 0, napi_default, 0};
    napi_define_properties(env, exports, 1, &desc);
    return exports;
}

NAPI_MODULE(mylib, Init)

通过上述步骤,ArkTS可以成功调用C函数。

在HarmonyOS鸿蒙Next中,ArkTS(Ark TypeScript)调用C代码的实现方法如下:

  1. 创建Native API:首先,使用C/C++编写需要的功能,并生成.so动态库。

  2. 定义Native接口:在ArkTS中,使用@native装饰器定义与C代码交互的接口。

  3. 加载动态库:通过System.loadLibrary加载编译好的.so库。

  4. 调用Native方法:在ArkTS中直接调用定义的Native接口,实现与C代码的交互。

这种方法允许ArkTS高效调用C语言编写的底层功能,提升性能并扩展应用能力。

回到顶部