// index.d.ts
 export const createArkRuntime: () => object;

编译配置

// CMakeLists.txt
the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
 project(MyApplication)set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${NATIVERENDER_ROOT_PATH}
 ${NATIVERENDER_ROOT_PATH}/include)
 add_library(entry SHARED create_ark_runtime.cpp)
 target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so)

模块注册

// create_ark_runtime.cpp
 EXTERN_C_START
 static napi_value Init(napi_env env, napi_value exports)
 {
 napi_property_descriptor desc[] = {
 { “createArkRuntime”, nullptr, CreateArkRuntime, nullptr, nullptr, nullptr, napi_default, nullptr }
 };
 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
 return exports;
 }
 EXTERN_C_ENDstatic napi_module nativeModule = {
 .nm_version = 1,
 .nm_flags = 0,
 .nm_filename = nullptr,
 .nm_register_func = Init,
 .nm_modname = “entry”,
 .nm_priv = nullptr,
 .reserved = { 0 },
 };extern “C” attribute((constructor)) void RegisterQueueWorkModule()
 {
 napi_module_register(&nativeModule);
 }
  1. 新建线程并创建基ArkTs础运行时环境
// create_ark_runtime.cpp
 #include <pthread.h>#include “napi/native_api.h”
static void *CreateArkRuntimeFunc(void *arg)
 {
 // 1. 创建基础运行环境
 napi_env env;
 napi_status ret = napi_create_ark_runtime(&env);
 if (ret != napi_ok) {
 return nullptr;
 }// 2. 加载自定义模块
 napi_value objUtils;
 ret = napi_load_module_with_info(env, “ets/pages/ObjectUtils”, “com.exmaple.myapplication/entry”, &objUtils);
 if (ret != napi_ok) {
 return nullptr;
 }// 3. 使用ArtTs中的logger
 napi_value logger;
 ret = napi_get_named_property(env, objUtils, “Logger”, &logger);
 if (ret != napi_ok) {
 return nullptr;
 }
 ret = napi_call_function(env, objUtils, logger, 0, nullptr, nullptr);// 4. 销毁arkts环境
 ret = napi_destroy_ark_runtime(&env);return nullptr;
 }static napi_value CreateArkRuntime(napi_env env, napi_callback_info info)
 {
 pthread_t tid;
 pthread_create(&tid, nullptr, CreateArkRuntimeFunc, nullptr);
 pthread_join(tid, nullptr);
 return nullptr;
 }
  1. ArkTS侧示例代码
// ObjectUtils.ets
 export function Logger() {
 console.log(“print log”);
 }