diff --git a/AppScope/app.json5 b/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..caa51bdf88f3a3b2ddc65e6fea8091a4f6bb9ba7 --- /dev/null +++ b/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.example.crossmodulereference", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/AppScope/resources/base/element/string.json b/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..9d24880f80710d0a9ff66bf558f10c76f0381d8d --- /dev/null +++ b/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "CrossModuleReference" + } + ] +} diff --git a/AppScope/resources/base/media/app_icon.png b/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/AppScope/resources/base/media/app_icon.png differ diff --git a/README.en.md b/README.en.md deleted file mode 100644 index d0091f28f14c874d50592f9bac554448d4388b56..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# CrossModuleReference - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md index 776b6cf7705a18509116d91a3099b51e6d744d3a..c8d45e44989b4538c6dc0ff7f5dce77ee5b9eb01 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,93 @@ -# CrossModuleReference +# Native侧跨HAR/HSP模块调用 -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} +### 介绍 -#### 软件架构 -软件架构说明 +本示例展示了Native侧调用HAR/HSP模块的接口,包括调用HAR/HSP模块的Native接口和ArkTS接口。 +### 效果预览 -#### 安装教程 +![](screenshots/device/nativeCall_cn.png) -1. xxxx -2. xxxx -3. xxxx -#### 使用说明 +使用说明 -1. xxxx -2. xxxx -3. xxxx +1、点击【调用Native方法】按钮,调用Native方法。 -#### 参与贡献 +2、点击【调用HarNative方法】按钮,调用staticModule模块的Native方法。 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +3、点击【通过Native方法调用HarArkTS方法】按钮,通过staticModule模块的Native方法调用Har模块的ArkTS方法。 +4、点击【调用HspNative方法】按钮,调用sharedModule模块的Native方法。 -#### 特技 +5、点击【通过Native方法调用HspArkTS方法】按钮,通过sharedModule模块的Native方法调用sharedModule模块的ArkTS方法。 -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +注意:在运行应用时,需要设置Deploy Multi Hap。点击Edit Configuration -> 选择entry -> 点击Deploy Multi Hap -> 勾选Deploy Multi Hap Packages。 + +### 工程目录 +``` +├──entry/src/main // Native层 +│ ├──cpp +│ │ ├──types // Native层暴露上来的接口 +│ │ │ └──libfile_access // 暴露给UI层的接口 +│ │ ├──CMakeLists.txt // 编译入口 +│ │ └──napi_init.cpp // Native文件操作方法 +│ └──ets // UI层 +│ ├──entryability // 应用的入口 +│ │ └──EntryAbility.ets +│ ├──entrybackupability +│ │ └──EntryBackupAbility.ets +│ ├──model +│ │ └──FileNameModel.ets +│ └──pages // EntryAbility 包含的页面 +│ └──Index.ets // 应用主页面 +├──staticModule/src/main // har模块 +│ ├──cpp +│ │ ├──types // Native层暴露上来的接口 +│ │ │ └──libfile_access // 暴露给UI层的接口 +│ │ ├──CMakeLists.txt // 编译入口 +│ │ ├──napi_har.cpp // har模块提供的native方法 +│ │ ├──napi_har.h // napi_har的头文件 +│ │ └──napi_init.cpp +│ └──ets // UI层 +│ ├──components +│ │ └──MainPage.ets +│ └──utils +│ └──Util.ets // ArkTS方法 +└──sharedModule/src/main // hsp模块 + ├──cpp + │ ├──types // Native层暴露上来的接口 + │ │ └──libfile_access // 暴露给UI层的接口 + │ ├──CMakeLists.txt // 编译入口 + │ ├──napi_hsp.cpp // hsp模块提供的native方法 + │ ├──napi_hsp.h // napi_hsp的头文件 + │ └──napi_init.cpp + └──ets // UI层 + ├──components + │ └──MainPage.ets + └──utils + └──Calc.ets // ArkTS方法 + +``` +### 具体实现 +* 创建staticModule(HAR)/sharedModule(HSP)模块,并在entry的oh-package.json5配置对应的依赖。 +* 在staticModule(HAR)/sharedModule(HSP)模块中创建Native方法及其头文件,在staticModule(HAR)/sharedModule(HSP)模块的build-profile.json5进行头文件导出配置。 +* 在staticModule(HAR)/sharedModule(HSP)模块中Native侧引用ArkTS方法。 +* 在entry的Native侧引用staticModule(HAR)/sharedModule(HSP)模块中Native方法 + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS NEXT Developer Release及以上。 + +3. DevEco Studio版本:DevEco Studio NEXT Developer Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS NEXT Developer Release SDK及以上。 \ No newline at end of file diff --git a/build-profile.json5 b/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..57760a7527912901c9f7c9f39c9ed1189d095fd6 --- /dev/null +++ b/build-profile.json5 @@ -0,0 +1,58 @@ +{ + "app": { + "signingConfigs": [ + ], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.0(12)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "staticModule", + "srcPath": "./staticModule" + }, + { + "name": "sharedModule", + "srcPath": "./sharedModule", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code-linter.json5 b/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..77b31b517a3e5c2f34c3ae1bf44083c0c06cbd6d --- /dev/null +++ b/code-linter.json5 @@ -0,0 +1,20 @@ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/entry/build-profile.json5 b/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1001ffffbf271f344cc8709bea10faa96edfe6d7 --- /dev/null +++ b/entry/build-profile.json5 @@ -0,0 +1,40 @@ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": ["x86_64","arm64-v8a"] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/entry/hvigorfile.ts b/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8bcd44ae69c56645de5e13f8f330e7388896196e --- /dev/null +++ b/entry/oh-package.json5 @@ -0,0 +1,13 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry", + "static_module": "file:../staticModule", + "shared_module": "file:../sharedModule" + } +} \ No newline at end of file diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bf0dfedbc609235383c8f434cc603f705cdb4cf4 --- /dev/null +++ b/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(CrossModuleReference) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so static_module::add shared_module::calc) diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbaad7d05730501f11e1f003c5561a1d2cb61756 --- /dev/null +++ b/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,174 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "napi/native_api.h" +#include "napi_har.h" +#include "napi_hsp.h" + +static napi_value Add(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + napi_value sum; + napi_create_double(env, value0 + value1, &sum); + + return sum; + +} + +static napi_value invokeHarNative(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + napi_value sum; + + napi_create_double(env, harNativeAdd(value0, value1), &sum); + + return sum; +} + +static napi_value invokeHarArkTS(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + return harArkTSAdd(value0, value1); +} + + +static napi_value invokeHspNative(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + napi_value sum; + + napi_create_double(env, hspNativeAdd(value0, value1), &sum); + + return sum; +} + +static napi_value invokeHspArkTS(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + return hspArkTSAdd(value0, value1); +} + + + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "invokeHarNative", nullptr, invokeHarNative, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "invokeHarArkTS", nullptr, invokeHarArkTS, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "invokeHspNative", nullptr, invokeHspNative, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "invokeHspArkTS", nullptr, invokeHspArkTS, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + setHarEnv(env); + setHspEnv(env); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/entry/src/main/cpp/types/libentry/Index.d.ts b/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..764eb54845f7fc8e495fa9a6cd4b430ad25b95e2 --- /dev/null +++ b/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,5 @@ +export const add: (a: number, b: number) => number; +export const invokeHarNative: (a: number, b: number) => number; +export const invokeHarArkTS: (a: number, b: number) => number; +export const invokeHspNative: (a: number, b: number) => number; +export const invokeHspArkTS: (a: number, b: number) => number; \ No newline at end of file diff --git a/entry/src/main/cpp/types/libentry/oh-package.json5 b/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ea410725a8826704d061021d98cf02aa76cd8016 --- /dev/null +++ b/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..9e26b358f9947166c848581cbc760ea44d873fa2 --- /dev/null +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,42 @@ +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + windowStage.getMainWindowSync().setWindowBackgroundColor('#F1F3F5'); + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..dc55c03d3eea7ce53d5346c732a39ce9bf5267e1 --- /dev/null +++ b/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,12 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..28412bb767908ba160b464c172ae70e81911bcb1 --- /dev/null +++ b/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,89 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import napi from 'libentry.so'; +import { promptAction } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + build() { + Column() { + Text($r('app.string.title')) + .width('100%') + .fontColor('#E6000000') + .fontSize(30) + .fontWeight(700) + .lineHeight(40) + .textAlign(TextAlign.Start) + .margin({ top: 64 }) + + Blank() + + Button($r('app.string.call_native_method')) + .fontSize(16) + .width('100%') + .onClick(() => { + promptAction.showToast({ message: 'Native method call succeed, result is ' + napi.add(2, 3).toString() }); + }) + + Button($r('app.string.call_har_native_method')) + .fontSize(16) + .width('100%') + .margin({ top: 12 }) + .onClick(() => { + promptAction.showToast({ + message: 'HarNative method call succeed, result is ' + napi.invokeHarNative(2, 3).toString() + }); + }) + + Button($r('app.string.call_har_ArkTS_method')) + .fontSize(16) + .width('100%') + .margin({ top: 12 }) + .onClick(() => { + promptAction.showToast({ message: 'HarArkTS method call succeed, result is ' + + napi.invokeHarArkTS(2, 3).toString() }); + }) + + Button($r('app.string.call_hsp_native_method')) + .fontSize(16) + .width('100%') + .margin({ top: 12 }) + .onClick(() => { + promptAction.showToast({ + message: 'HspNative method call succeed, result is ' + napi.invokeHspNative(2, 3).toString() + }); + }) + + Button($r('app.string.call_hsp_ArkTS_method')) + .fontSize(16) + .width('100%') + .margin({ top: 12 }) + .onClick(() => { + promptAction.showToast({ message: 'HspArkTS method call succeed, result is ' + + napi.invokeHspArkTS(2, 3).toString() }); + }) + } + .padding({ + bottom: 16, + left: 16, + right: 16 + }) + .justifyContent(FlexAlign.End) + .width('100%') + .height('100%') + } +} diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a1cea8b6a4560cee7bda7a2db52f310c035ab6c8 --- /dev/null +++ b/entry/src/main/module.json5 @@ -0,0 +1,52 @@ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ] + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/color.json b/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..69d1e82a2ebdf9731e589780417a867863570b4a --- /dev/null +++ b/entry/src/main/resources/base/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "CrossModuleReference" + }, + { + "name": "title", + "value": "Calling methods across HAR/HSP" + }, + { + "name": "call_native_method", + "value": "Call native method" + }, + { + "name": "call_har_native_method", + "value": "Call HarNative method" + }, + { + "name": "call_har_ArkTS_method", + "value": "Call HarArkTS methods" + }, + { + "name": "call_hsp_native_method", + "value": "Call HspNative method" + }, + { + "name": "call_hsp_ArkTS_method", + "value": "Call HspArkTS method" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/media/background.png b/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/entry/src/main/resources/base/media/background.png differ diff --git a/entry/src/main/resources/base/media/foreground.png b/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/entry/src/main/resources/base/media/foreground.png differ diff --git a/entry/src/main/resources/base/media/layered_image.json b/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/media/startIcon.png b/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/entry/src/main/resources/base/media/startIcon.png differ diff --git a/entry/src/main/resources/base/profile/backup_config.json b/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..69d1e82a2ebdf9731e589780417a867863570b4a --- /dev/null +++ b/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "CrossModuleReference" + }, + { + "name": "title", + "value": "Calling methods across HAR/HSP" + }, + { + "name": "call_native_method", + "value": "Call native method" + }, + { + "name": "call_har_native_method", + "value": "Call HarNative method" + }, + { + "name": "call_har_ArkTS_method", + "value": "Call HarArkTS methods" + }, + { + "name": "call_hsp_native_method", + "value": "Call HspNative method" + }, + { + "name": "call_hsp_ArkTS_method", + "value": "Call HspArkTS method" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..16841772cc65df2b23ba1b4dff3826fe892b7adf --- /dev/null +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "CrossModuleReference" + }, + { + "name": "title", + "value": "Native跨HSP/HAR调用" + }, + { + "name": "call_native_method", + "value": "调用Native方法" + }, + { + "name": "call_har_native_method", + "value": "调用HarNative方法" + }, + { + "name": "call_har_ArkTS_method", + "value": "通过Native方法调用HarArkTS方法" + }, + { + "name": "call_hsp_native_method", + "value": "调用HspNative方法" + }, + { + "name": "call_hsp_ArkTS_method", + "value": "通过Native方法调用HspArkTS方法" + } + ] +} \ No newline at end of file diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..06b2783670a348f95533b352c1ceda909a842bbc --- /dev/null +++ b/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/hvigorfile.ts b/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/oh-package.json5 b/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8bf916d4a165e0466c65bc49f58ab2e3c27e8931 --- /dev/null +++ b/oh-package.json5 @@ -0,0 +1,8 @@ +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + } +} diff --git a/screenshots/device/nativeCall.png b/screenshots/device/nativeCall.png new file mode 100644 index 0000000000000000000000000000000000000000..3771b30e0540813fb89840ed76f58f6562692205 Binary files /dev/null and b/screenshots/device/nativeCall.png differ diff --git a/screenshots/device/nativeCall_cn.png b/screenshots/device/nativeCall_cn.png new file mode 100644 index 0000000000000000000000000000000000000000..13702cc8b09e9f62619701e3d0c5ccba24b05610 Binary files /dev/null and b/screenshots/device/nativeCall_cn.png differ diff --git a/sharedModule/Index.ets b/sharedModule/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..3cb1d2e3e323d0bf5b1976e1682033bf68d97c7e --- /dev/null +++ b/sharedModule/Index.ets @@ -0,0 +1 @@ +export { add } from './src/main/ets/utils/Calc' \ No newline at end of file diff --git a/sharedModule/build-profile.json5 b/sharedModule/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..52a50d2e0936d7fb7fcbb0cbfcb118b78b3fb7a5 --- /dev/null +++ b/sharedModule/build-profile.json5 @@ -0,0 +1,43 @@ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": ["x86_64", "arm64-v8a"] + }, + "nativeLib": { + "headerPath": "./src/main/cpp" + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} \ No newline at end of file diff --git a/sharedModule/hvigorfile.ts b/sharedModule/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..d993120bd71f5f5dfe04480be9dc5f73c00c2599 --- /dev/null +++ b/sharedModule/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hspTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/sharedModule/oh-package.json5 b/sharedModule/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..16f7048c95e2d190f7127d19acf8df7dd0176f1d --- /dev/null +++ b/sharedModule/oh-package.json5 @@ -0,0 +1,12 @@ +{ + "name": "shared_module", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "packageType": "InterfaceHar", + "dependencies": { + "libcalc.so": "file:./src/main/cpp/types/libhsp" + } +} \ No newline at end of file diff --git a/sharedModule/src/main/cpp/CMakeLists.txt b/sharedModule/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..67ef09110362af4090c99baf07f9ca706e2171d5 --- /dev/null +++ b/sharedModule/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(myNpmLib) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(calc SHARED napi_hsp.cpp) +target_link_libraries(calc PUBLIC libace_napi.z.so) \ No newline at end of file diff --git a/sharedModule/src/main/cpp/napi_hsp.cpp b/sharedModule/src/main/cpp/napi_hsp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaf6f9699992d2f6b26eaa0ecdcdab70bca26bdd --- /dev/null +++ b/sharedModule/src/main/cpp/napi_hsp.cpp @@ -0,0 +1,46 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "napi/native_api.h" +#include "napi_hsp.h" + +double hspNativeAdd(double a, double b) { + return a + b; +} + +void setHspEnv(napi_env env) { + main_env = env; +} + +napi_value hspArkTSAdd(double a, double b) { + napi_env env = main_env; + napi_value module; + napi_status status = napi_load_module_with_info(env, "shared_module/src/main/ets/utils/Calc", "com.example.crossmodulereference/entry", &module); + if (napi_ok != status) { + return 0; + } + + napi_value addFunc; + napi_get_named_property(env, module, "add", &addFunc); + + napi_value addResult; + napi_value argv[2] = {nullptr, nullptr}; + napi_create_double(env, a, &argv[0]); + napi_create_double(env, b, &argv[1]); + napi_call_function(env, module, addFunc, 2, argv, &addResult); + + return addResult; +} + diff --git a/sharedModule/src/main/cpp/napi_hsp.h b/sharedModule/src/main/cpp/napi_hsp.h new file mode 100644 index 0000000000000000000000000000000000000000..3e1df841743a1f2e88dc201a95b6a6488b339a0d --- /dev/null +++ b/sharedModule/src/main/cpp/napi_hsp.h @@ -0,0 +1,23 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef CROSSMODULEREFERENCE_NAPI_HSP_H +#define CROSSMODULEREFERENCE_NAPI_HSP_H +#include +napi_env main_env; +void setHspEnv(napi_env env); +double hspNativeAdd(double a, double b); +napi_value hspArkTSAdd(double a, double b); +#endif //CROSSMODULEREFERENCE_NAPI_HSP_H diff --git a/sharedModule/src/main/cpp/napi_init.cpp b/sharedModule/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4df80029750da3eb1b8428483a14f40fc53471e0 --- /dev/null +++ b/sharedModule/src/main/cpp/napi_init.cpp @@ -0,0 +1,68 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "napi/native_api.h" + +static napi_value Add(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + napi_value sum; + napi_create_double(env, value0 + value1, &sum); + + return sum; + +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "calc", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterHspModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/sharedModule/src/main/cpp/types/libhsp/Index.d.ts b/sharedModule/src/main/cpp/types/libhsp/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e44f3615a4767c36b4fdc841b781af96bf1971a8 --- /dev/null +++ b/sharedModule/src/main/cpp/types/libhsp/Index.d.ts @@ -0,0 +1 @@ +export const add: (a: number, b: number) => number; \ No newline at end of file diff --git a/sharedModule/src/main/cpp/types/libhsp/oh-package.json5 b/sharedModule/src/main/cpp/types/libhsp/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..543773a5eb39f0661b01376962e64eafd3f3cf81 --- /dev/null +++ b/sharedModule/src/main/cpp/types/libhsp/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libcalc.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/sharedModule/src/main/ets/pages/Index.ets b/sharedModule/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..df13bd8ebd428df8403a2f94379e5a4c8a8f6a78 --- /dev/null +++ b/sharedModule/src/main/ets/pages/Index.ets @@ -0,0 +1,38 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import testNapi from 'libcalc.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/sharedModule/src/main/ets/utils/Calc.ets b/sharedModule/src/main/ets/utils/Calc.ets new file mode 100644 index 0000000000000000000000000000000000000000..fc74f0e28cbae7f1ad678b8dc3a4abd10773f300 --- /dev/null +++ b/sharedModule/src/main/ets/utils/Calc.ets @@ -0,0 +1,18 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +export function add(a: number, b: number) { + return a + b; +} \ No newline at end of file diff --git a/sharedModule/src/main/module.json5 b/sharedModule/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b06784741a6e1cb93961c15e6e645180f935c831 --- /dev/null +++ b/sharedModule/src/main/module.json5 @@ -0,0 +1,14 @@ +{ + "module": { + "name": "sharedModule", + "type": "shared", + "description": "$string:shared_desc", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "pages": "$profile:main_pages" + } +} \ No newline at end of file diff --git a/sharedModule/src/main/resources/base/element/string.json b/sharedModule/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..98e1d8a84b3d8539ea33e79fe2ac593a05d23bb4 --- /dev/null +++ b/sharedModule/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "shared_desc", + "value": "description" + } + ] +} \ No newline at end of file diff --git a/sharedModule/src/main/resources/base/profile/main_pages.json b/sharedModule/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/sharedModule/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/staticModule/Index.ets b/staticModule/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..10ebb3efed9a6e0ed13465ad26519d94270f1ca8 --- /dev/null +++ b/staticModule/Index.ets @@ -0,0 +1 @@ +export { MainPage } from './src/main/ets/components/MainPage' diff --git a/staticModule/build-profile.json5 b/staticModule/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4654414ecbb4fbe73547440b02a5a2e287fc78b9 --- /dev/null +++ b/staticModule/build-profile.json5 @@ -0,0 +1,53 @@ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": ["x86_64", "arm64-v8a"] + }, + "nativeLib": { + "headerPath": "./src/main/cpp" + }, + "arkOptions" : { + "runtimeOnly" : { + "sources": [ + "./src/main/ets/utils/Util.ets" + ] + } + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + }, + "consumerFiles": [ + "./consumer-rules.txt" + ] + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} diff --git a/staticModule/consumer-rules.txt b/staticModule/consumer-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/staticModule/hvigorfile.ts b/staticModule/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..42187071482d292588ad40babeda74f7b8d97a23 --- /dev/null +++ b/staticModule/hvigorfile.ts @@ -0,0 +1,6 @@ +import { harTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/staticModule/oh-package.json5 b/staticModule/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4c89a4c4cc654f6bc370bea1bb593e060d6cf1a0 --- /dev/null +++ b/staticModule/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "static_module", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "dependencies": { + "libadd.so": "file:./src/main/cpp/types/liblibrary" + } +} \ No newline at end of file diff --git a/staticModule/src/main/cpp/CMakeLists.txt b/staticModule/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bba3961ccb7acc8f7364f957c6c096f395167123 --- /dev/null +++ b/staticModule/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(myNpmLib) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(add SHARED napi_init.cpp napi_har.cpp) +target_link_libraries(add PUBLIC libace_napi.z.so) diff --git a/staticModule/src/main/cpp/napi_har.cpp b/staticModule/src/main/cpp/napi_har.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec61919733601668d0ded4e5d2686a3d94e95fbf --- /dev/null +++ b/staticModule/src/main/cpp/napi_har.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "napi/native_api.h" +#include "napi_har.h" + +double harNativeAdd(double a, double b) { + return a + b; +} + +void setHarEnv(napi_env env) { + g_main_env = env; +} + +napi_value harArkTSAdd(double a, double b) { + napi_env env = g_main_env; + napi_value module; + napi_status status = napi_load_module_with_info(env, "static_module/src/main/ets/utils/Util", "com.example.crossmodulereference/entry", &module); + if (napi_ok != status) { + return 0; + } + + napi_value addFunc; + napi_get_named_property(env, module, "add", &addFunc); + + napi_value addResult; + napi_value argv[2] = {nullptr, nullptr}; + napi_create_double(env, a, &argv[0]); + napi_create_double(env, b, &argv[1]); + napi_call_function(env, module, addFunc, 2, argv, &addResult); + + return addResult; +} + diff --git a/staticModule/src/main/cpp/napi_har.h b/staticModule/src/main/cpp/napi_har.h new file mode 100644 index 0000000000000000000000000000000000000000..64879003ed11e11c0b2341370686c2f02fd2ab65 --- /dev/null +++ b/staticModule/src/main/cpp/napi_har.h @@ -0,0 +1,23 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef CROSSMODULEREFERENCE_NAPI_HAR_H +#define CROSSMODULEREFERENCE_NAPI_HAR_H +#include +napi_env g_main_env; +void setHarEnv(napi_env env); +double harNativeAdd(double a, double b); +napi_value harArkTSAdd(double a, double b); +#endif //CROSSMODULEREFERENCE_NAPI_HAR_H diff --git a/staticModule/src/main/cpp/napi_init.cpp b/staticModule/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce3735d39ace8c133ca982293eabd1363c6b5284 --- /dev/null +++ b/staticModule/src/main/cpp/napi_init.cpp @@ -0,0 +1,67 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "napi/native_api.h" + +static napi_value Add(napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + double value0; + napi_get_value_double(env, args[0], &value0); + + double value1; + napi_get_value_double(env, args[1], &value1); + + napi_value sum; + napi_create_double(env, value0 + value1, &sum); + + return sum; + +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "add", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterLibraryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/staticModule/src/main/cpp/types/liblibrary/Index.d.ts b/staticModule/src/main/cpp/types/liblibrary/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e44f3615a4767c36b4fdc841b781af96bf1971a8 --- /dev/null +++ b/staticModule/src/main/cpp/types/liblibrary/Index.d.ts @@ -0,0 +1 @@ +export const add: (a: number, b: number) => number; \ No newline at end of file diff --git a/staticModule/src/main/cpp/types/liblibrary/oh-package.json5 b/staticModule/src/main/cpp/types/liblibrary/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7565cd3b83424782d5e5df2f9c59698e8c7b982c --- /dev/null +++ b/staticModule/src/main/cpp/types/liblibrary/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libadd.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} diff --git a/staticModule/src/main/ets/components/MainPage.ets b/staticModule/src/main/ets/components/MainPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..f30aea0e321f120612433aa5e56dfb0985b5cf8a --- /dev/null +++ b/staticModule/src/main/ets/components/MainPage.ets @@ -0,0 +1,36 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import testNapi from 'libadd.so'; + +@Component +export struct MainPage { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/staticModule/src/main/ets/utils/Util.ets b/staticModule/src/main/ets/utils/Util.ets new file mode 100644 index 0000000000000000000000000000000000000000..d22eab8459c08d22925eb842e326f51fa517cee9 --- /dev/null +++ b/staticModule/src/main/ets/utils/Util.ets @@ -0,0 +1,17 @@ +/* +* Copyright (C) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +export function add(a: number, b: number): number { + return a + b; +} \ No newline at end of file diff --git a/staticModule/src/main/module.json5 b/staticModule/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..affaabaa2e59174a186f412c4d006e63e9030e8e --- /dev/null +++ b/staticModule/src/main/module.json5 @@ -0,0 +1,11 @@ +{ + "module": { + "name": "staticModule", + "type": "har", + "deviceTypes": [ + "default", + "tablet", + "2in1" + ] + } +} \ No newline at end of file diff --git a/staticModule/src/main/resources/base/element/string.json b/staticModule/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f51a9c8461a55f6312ef950344e3145b7f82d607 --- /dev/null +++ b/staticModule/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/staticModule/src/main/resources/en_US/element/string.json b/staticModule/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f51a9c8461a55f6312ef950344e3145b7f82d607 --- /dev/null +++ b/staticModule/src/main/resources/en_US/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/staticModule/src/main/resources/zh_CN/element/string.json b/staticModule/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f51a9c8461a55f6312ef950344e3145b7f82d607 --- /dev/null +++ b/staticModule/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +}