diff --git a/NdkDevelopment/AppScope/app.json5 b/NdkDevelopment/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9ff755fb5c90085d615f1175a502bd066cc023b7 --- /dev/null +++ b/NdkDevelopment/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.example.ndkdevelopment", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:layered_image", + "label": "$string:app_name" + } +} diff --git a/NdkDevelopment/AppScope/resources/base/element/string.json b/NdkDevelopment/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1a57d87c7d352e41ace1d91dd8321fbe9926612b --- /dev/null +++ b/NdkDevelopment/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "NdkDevelopment" + } + ] +} diff --git a/NdkDevelopment/AppScope/resources/base/media/background.png b/NdkDevelopment/AppScope/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/NdkDevelopment/AppScope/resources/base/media/background.png differ diff --git a/NdkDevelopment/AppScope/resources/base/media/foreground.png b/NdkDevelopment/AppScope/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/NdkDevelopment/AppScope/resources/base/media/foreground.png differ diff --git a/NdkDevelopment/AppScope/resources/base/media/layered_image.json b/NdkDevelopment/AppScope/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/NdkDevelopment/AppScope/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/NdkDevelopment/build-profile.json5 b/NdkDevelopment/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..03ac3a3ae9dd397844173066e5d5ee21b49455a1 --- /dev/null +++ b/NdkDevelopment/build-profile.json5 @@ -0,0 +1,41 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.4(16)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/code-linter.json5 b/NdkDevelopment/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..073990fa45394e1f8e85d85418ee60a8953f9b99 --- /dev/null +++ b/NdkDevelopment/code-linter.json5 @@ -0,0 +1,32 @@ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/NdkDevelopment/entry/build-profile.json5 b/NdkDevelopment/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4d611879c7913fb0610c686e2399258ab3a6dad1 --- /dev/null +++ b/NdkDevelopment/entry/build-profile.json5 @@ -0,0 +1,28 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/entry/hvigorfile.ts b/NdkDevelopment/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/NdkDevelopment/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/NdkDevelopment/entry/obfuscation-rules.txt b/NdkDevelopment/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/NdkDevelopment/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/NdkDevelopment/entry/oh-package.json5 b/NdkDevelopment/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/NdkDevelopment/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/NdkDevelopment/entry/src/main/ets/entryability/EntryAbility.ets b/NdkDevelopment/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..508880af8c33aa838016d1cd4b2c68be2f447540 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/NdkDevelopment/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e4de99282050bad799ac892eb85ac5449364a51 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/AsyncMethodForArkTSFromNative.ets b/NdkDevelopment/entry/src/main/ets/pages/AsyncMethodForArkTSFromNative.ets new file mode 100644 index 0000000000000000000000000000000000000000..c9aa22b7aa35d527691d06745469fffcdc39ef29 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/AsyncMethodForArkTSFromNative.ets @@ -0,0 +1,148 @@ +/* +* 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. +*/ + +/* +* FAQ:Native侧如何访问ArkTS侧系统定义的异步方法 +*/ + +// DocsCode 1 +#include "napi/native_api.h" +#include +#include + +#define LOG_TAG "Pure" // 全局tag宏,标识模块日志tag + +// 上下文数据,用于线程间传递数据 +struct CallbackData { + napi_threadsafe_function tsfn; + napi_async_work work; + napi_deferred deferred = nullptr; + double res; +}; +static napi_value ResolvedCallback(napi_env env, napi_callback_info info) { + void *data = nullptr; + size_t argc = 1; + napi_value argv[1]; + napi_get_cb_info(env, info, &argc, argv, nullptr, &data); + napi_value widthProp = nullptr; + napi_get_named_property(env, argv[0], "width", &widthProp); + double result = 0; + napi_get_value_double(env, widthProp, &result); + OH_LOG_INFO(LOG_APP, "width in ResolvedCallback is %{public}f", result); + // data被重新解释为指向std::promise的指针,并设置该promise的值为width + reinterpret_cast *>(data)->set_value(result); + return nullptr; +} +static void CallJs(napi_env env, napi_value jsCb, void *context, void *data) { + // 导入系统库模块,逐层向下调用到方法 + napi_value systemModule; + napi_load_module(env, "@ohos.display", &systemModule); + napi_value displayFunc = nullptr; + napi_get_named_property(env, systemModule, "getDefaultDisplay", &displayFunc); + napi_value promise = nullptr; + napi_call_function(env, systemModule, displayFunc, 0, nullptr, &promise); + napi_value thenFunc = nullptr; + napi_get_named_property(env, promise, "then", &thenFunc); + napi_value resolvedCallback; + // promise resolve回调 + napi_create_function(env, "resolvedCallback", NAPI_AUTO_LENGTH, ResolvedCallback, data, &resolvedCallback); + napi_value argv[] = {resolvedCallback}; + napi_call_function(env, promise, thenFunc, 1, argv, nullptr); +} +static void ExecuteWork(napi_env env, void *data) { + CallbackData *callbackData = reinterpret_cast(data); + std::promise promise; + auto future = promise.get_future(); + napi_call_threadsafe_function(callbackData->tsfn, &promise, napi_tsfn_nonblocking); + try { + auto result = future.get(); + callbackData->res = result; + OH_LOG_INFO(LOG_APP, "width in ExecuteWork %{public}f", result); + } catch (const std::exception &e) { + OH_LOG_INFO(LOG_APP, "XXX, Result from JS %{public}s", e.what()); + } +} +static void WorkComplete(napi_env env, napi_status status, void *data) { + CallbackData *callbackData = reinterpret_cast(data); + // 回传业务代码计算结果给应用 + napi_value result = nullptr; + napi_create_double(env, callbackData->res, &result); + napi_resolve_deferred(env, callbackData->deferred, result); + // 释放线程安全方法 + napi_release_threadsafe_function(callbackData->tsfn, napi_tsfn_release); + // 删除异步工作项 + napi_delete_async_work(env, callbackData->work); + callbackData->tsfn = nullptr; + callbackData->work = nullptr; +} +static napi_value getDisplayWidthAsync(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value jsCb = nullptr; + CallbackData *callbackData = nullptr; + napi_get_cb_info(env, info, &argc, &jsCb, nullptr, reinterpret_cast(&callbackData)); + + napi_value sysModule; + napi_load_module(env, "@ohos.display", &sysModule); + napi_value getDefaultDisplay; + napi_get_named_property(env, sysModule, "getDefaultDisplay", &getDefaultDisplay); + // 创建一个线程安全函数 + napi_value resourceName = nullptr; + napi_create_string_utf8(env, "getDisplayWidthAsync", NAPI_AUTO_LENGTH, &resourceName); + napi_create_threadsafe_function(env, getDefaultDisplay, nullptr, resourceName, 0, 1, callbackData, nullptr, + callbackData, CallJs, &callbackData->tsfn); + // 创建一个异步任务 + napi_create_async_work(env, nullptr, resourceName, ExecuteWork, WorkComplete, callbackData, &callbackData->work); + // 将异步任务加入到异步队列中,由底层调度执行 + napi_queue_async_work(env, callbackData->work); + // 方法返回promise + napi_value result = nullptr; + napi_create_promise(env, &callbackData->deferred, &result); + return result; +} +// DocsCode 1 + +// DocsCode 2 +export const getDisplayWidthAsync: () => Promise; +// DocsCode 2 + +// DocsCode 3 +import testNapi from 'libcallsystemasyncwork.so'; + +@Entry +@Component +struct Index { + build() { + Row() { + Column() { + Text('c++异步调用ts') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .onClick(() => { + testNapi.getDisplayWidthAsync().then((res: number) =>{ + console.info(`display width = ${res.toString()}`); + this.getUIContext().getPromptAction().showToast({ + message: "屏幕宽度:" + res.toString() + }) + }); + }) + .margin('30') + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + } + .height('100%') + } +} +// DocsCode 3 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/CSideOpenFile.ets b/NdkDevelopment/entry/src/main/ets/pages/CSideOpenFile.ets new file mode 100644 index 0000000000000000000000000000000000000000..5d9791448e9fa2773a5222a1ee2ff7e680e51e76 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/CSideOpenFile.ets @@ -0,0 +1,116 @@ +/* +* 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. +*/ + +/* +* FAQ:C侧如何打开文件 +*/ + +// DocsCode 1 +import { fileIo, picker } from '@kit.CoreFileKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import testNapi from 'libentry.so'; +import { photoAccessHelper } from '@kit.MediaLibraryKit'; + +@Entry +@Component +struct Index { + @State message: string = '打开文件'; + + async open() { + const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); + photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE + photoSelectOptions.maxSelectNumber = 5; // 选择媒体文件的最大数目 + let uris: Array = []; + const photoViewPicker = new photoAccessHelper.PhotoViewPicker(); + await photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => { + uris = photoSelectResult.photoUris; + console.info('photoViewPicker.select to file succeed and uris are:' + uris); + }).catch((err: BusinessError) => { + console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); + }) + let uri: string = uris[0]; + let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY); + console.info('file fd: ' + file.fd); + let fd = file.fd + let filesDir = getContext(this).filesDir; + fileIo.copyFileSync(fd, filesDir + '/test2.jpg') + let file2 = fileIo.openSync(filesDir + '/test2.jpg', fileIo.OpenMode.READ_ONLY); + let file3 = fileIo.openSync(filesDir + '/test3.jpg', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); + testNapi.ReadFile(file2.fd, file3.fd) + } + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + } + .onClick(() => { + this.open(); + }) + .width('100%') + } + .height('100%') + } +} +// DocsCode 1 + +// DocsCode 2 +static napi_value OpenFile(unsigned int fd, unsigned int fd2) { + OH_LOG_INFO(LOG_APP, "OpenFile"); + + if (fd != -1) { + char buffer[4096]; + ssize_t bytesRead; + // 读取文件内容到缓冲区中 + bytesRead = read(fd, buffer, sizeof(buffer)); + if (bytesRead == -1) { + OH_LOG_INFO(LOG_APP, "读取文件失败"); + close(fd); // 关闭文件描述符 + return nullptr; + } + while (bytesRead != 0) { + OH_LOG_INFO(LOG_APP, "读取文件大小 %{public}lu", bytesRead); + OH_LOG_INFO(LOG_APP, "读取文件cg"); + char *pData1 = buffer; + OH_LOG_INFO(LOG_APP, "文件内容: \n%{public}s", pData1); + ssize_t bytesWrite; + bytesWrite = write(fd2, pData1, bytesRead); + if (bytesWrite == -1) { + OH_LOG_INFO(LOG_APP, "写文件失败"); + close(fd2); // 关闭文件描述符 + return nullptr; + } + bytesRead = read(fd, buffer, sizeof(buffer)); + } + // 关闭文件描述符 + close(fd); + close(fd2); // 关闭文件描述符 + } + return nullptr; +} +static napi_value ReadFile(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); + unsigned int fd = -1; + napi_get_value_uint32(env, args[0], &fd); + unsigned int fd2 = -1; + napi_get_value_uint32(env, args[1], &fd2); + OpenFile(fd, fd2); + return nullptr; +} +// DocsCode 2 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/DynamicallyLoadSOLibrary.ets b/NdkDevelopment/entry/src/main/ets/pages/DynamicallyLoadSOLibrary.ets new file mode 100644 index 0000000000000000000000000000000000000000..3163a1979ae35e8b8b13651ec37c947d0ecd22a3 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/DynamicallyLoadSOLibrary.ets @@ -0,0 +1,104 @@ +/* +* 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. +*/ + +/* +* FAQ:ArkTS侧与Native侧分别如何动态加载SO库 +*/ + +// DocsCode 1 +import { hilog } from '@kit.PerformanceAnalysisKit'; +// import testNapi from 'libentry.so'; + +@Entry +@Component +struct LoadSoLibrary { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(async() => { + let testNapi = await import("libentry.so") // 加载so库 + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.default.add(2, 3)); // 通过default调用库函数 + // hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + }) + } + .width('100%') + } + .height('100%') + } +} +// DocsCode 1 + +// DocsCode 2 +import { hilog } from '@kit.PerformanceAnalysisKit'; +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + context = this.getUIContext(); + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let path = this.context.getHostContext()?.bundleCodeDir; // 获取项目路径 + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.addByLibPath(2, 3, path + '/libs/arm64/liba.so')); // 传递参数路径信息到Native侧 + }) + } + .width('100%') + } + .height('100%') + } +} +// DocsCode 2 + +// DocsCode 3 +#include "napi/native_api.h" +#include + typedef double (*FUNC_ADD)(int, int); +static napi_value Add(napi_env env, napi_callback_info info) { + size_t requireArgc = 3; + size_t argc = 3; + napi_value args[3] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + double value0; + napi_get_value_double(env, args[0], &value0); + double value1; + napi_get_value_double(env, args[1], &value1); + char path[255]; + size_t size = 255; + napi_get_value_string_utf8(env, args[2], path, 255, &size); // 获取动态库路径信息 + void *handle = dlopen(path, RTLD_LAZY); // 打开一个动态链接库.路径为path + dlerror(); + FUNC_ADD add_func = (FUNC_ADD)dlsym(handle, "add"); // 获取函数名为add的函数 + if (dlerror()) { + return nullptr; + } + double res = add_func(value0, value1); // 调用add并传递参数信息 + dlclose(handle); // 最后记得close动态库 + napi_value sum; + napi_create_double(env, res, &sum); + return sum; +} +// DocsCode 3 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/GetObjectForArkTSFromNative.ets b/NdkDevelopment/entry/src/main/ets/pages/GetObjectForArkTSFromNative.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb1609a576814d5848562f92875ea36c699a33f3 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/GetObjectForArkTSFromNative.ets @@ -0,0 +1,40 @@ +/* +* 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. +*/ + +/* +* FAQ:Native侧如何获取ArkTS侧Object对象及其成员变量 +*/ + +// DocsCode 1 +// index.ets +import testNapi from 'libentry.so'; + +class A { + name:string ='username' + onCall() { + this.getUIContext().getPromptAction().showToast({ + message:'Message Info', + duration:2000 + }); + console.log("testTag user onCall"); + } +} +build() { + ... + .onClick(() => { + testNapi.callFunction(new A()); + }) +} +// DocsCode 1 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/Index.ets b/NdkDevelopment/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e2d24ad42693fc877d51bb7820f0a9da68fa135 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/IntegrateSOLibrary.ets b/NdkDevelopment/entry/src/main/ets/pages/IntegrateSOLibrary.ets new file mode 100644 index 0000000000000000000000000000000000000000..18254a414d6937080eacd4e33812a57d5a30a0a7 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/IntegrateSOLibrary.ets @@ -0,0 +1,123 @@ +/* +* 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. +*/ + +/* +* FAQ:在Native侧如何集成三方SO库 +*/ + +// DocsCode 1 +// sub.h +extern "C" { + double sub(double a, double b); +} +// sub.cpp +#include +#include "sub.h" +double sub(double a, double b) +{ + return a - b; +} +// CMakeLists.txt +cmake_minimum_required(VERSION 3.4.1) +project(libSub) +#编译源码 +add_library(nativeSub SHARED sub.cpp) +// DocsCode 1 + +// DocsCode 2 +# CMakeLists.txt +cmake_minimum_required(VERSION 3.4.1) +project(hello) +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${NATIVERENDER_ROOT_PATH} +${NATIVERENDER_ROOT_PATH}/include) +add_library(library SHARED hello.cpp) +target_link_libraries(library PUBLIC libace_napi.z.so libhilog_ndk.z.so) +target_link_libraries(library PUBLIC ${NATIVERENDER_ROOT_PATH}/../../../libs/arm64-v8a/libnativeSub.so) +// DocsCode 2 + +// DocsCode 3 +#include "sub.h" +static napi_value Sub(napi_env env, napi_callback_info info) +{ + size_t requireArgc = 2; + 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, sub(value0 - value1), &sum); + return sum; +} +// DocsCode 3 + +// DocsCode 4 +import { hilog } from '@kit.PerformanceAnalysisKit'; +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + context = this.getUIContext(); + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let path = this.context.getHostContext()?.bundleCodeDir; // 获取项目路径 + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.subSobyDlOpenSo(2, 3, path + '/libs/arm64/libnativeSub.so')); // 传递参数路径信息到Native侧 + }) + } + .width('100%') + } + .height('100%') + } +} +// DocsCode 4 + +// DocsCode 5 +#include + typedef double (*Sub)(double, double); +static napi_value SubSobyDlOpenSo(napi_env env, napi_callback_info info) { + size_t requireArgc = 3; + size_t argc = 3; + napi_value args[3] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + double value0; + napi_get_value_double(env, args[0], &value0); + double value1; + napi_get_value_double(env, args[1], &value1); + char* path = new char[1024]; + size_t size = 1024; + napi_get_value_string_utf8(env, args[2], path, 255, &size); // 获取动态库路径信息 + void *handle = dlopen(path, RTLD_LAZY); // 打开一个动态链接库.路径为path + napi_value result; + Sub sub_func = (Sub)dlsym(handle, "sub"); // 获取函数名为sub的函数 + napi_create_double(env, sub_func(value0, value1), &result); + dlclose(handle); // 最后记得close动态库 + return result; +} +// DocsCode 5 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/MultiSoInterdependenceDecoupling.ets b/NdkDevelopment/entry/src/main/ets/pages/MultiSoInterdependenceDecoupling.ets new file mode 100644 index 0000000000000000000000000000000000000000..508eb056d64edb11f30a2a421bd6754bbe029cf7 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/MultiSoInterdependenceDecoupling.ets @@ -0,0 +1,196 @@ +/* +* 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. +*/ + +/* +* FAQ:多so相互依赖场景下如何解耦 +*/ + +// DocsCode 1 +// a.cpp +extern "C" { // 一定要用extern "C" {}括起来 + #include "a.h" + #include + #include "stdio.h" + typedef int (*FUNC_SUB)(int, int); + int add(int a, int b) { return a + b; } + int getb(char *path, int a, int b) { // path:从ArkTS侧传递So文件的沙箱路径(注意路径要从arkts侧传递、不然有可能找不到路径、具体代码后续会列出) + void *handle = dlopen(path, RTLD_LAZY); // 打开一个动态链接库.路径为path + if (!handle) { + return 0; + } + FUNC_SUB sub_func = (FUNC_SUB)dlsym(handle, "sub"); // 获取函数名为sub的函数 + int res = sub_func(a, b); // 调用函数 + dlclose(handle); // close动态链接库 + return res; + } +} +// a.h +extern "C" { + #ifndef DemoSO_a_H + #define DemoSO_a_H + int add(int a, int b); + int getb(char *path, int a, int b); + #endif // DemoSO_a_H +} +// b.cpp +extern "C" { // 一定要用extern "C" {}括起来 + #include "b.h" + #include + #include "stdio.h" + typedef int (*FUNC_ADD)(int, int); + int sub(int a, int b) { return a - b; } + int geta(char *path, int a, int b) { // path:从ArkTS侧传递So文件的沙箱路径(注意路径要从arkts侧传递、不然有可能找不到路径、具体代码后续会列出) + void *handle = dlopen(path, RTLD_LAZY); // 打开一个动态链接库.路径为path + if (!handle) { + return 0; + } + FUNC_ADD add_func = (FUNC_ADD)dlsym(handle, "add"); // 获取函数名为add的函数 + int res = add_func(a, b); // 调用函数 + dlclose(handle); // close动态链接库 + return res; + } +} +// b.h +extern "C" { + #ifndef DemoSO_b_H + #define DemoSO_b_H + int sub(int a, int b); + int geta(char *path, int a, int b); + #endif // DemoSO_b_H +} +// CMakeLists.txt +cmake_minimum_required(VERSION 3.4.1) +project(liba) +add_library(a SHARED a.cpp) // 编译库liba.so +target_link_libraries(a PUBLIC libace_napi.z.so libhilog_ndk.z.so) +project(libb) +add_library(b SHARED b.cpp) // 编译库libb.so +target_link_libraries(b PUBLIC libace_napi.z.so libhilog_ndk.z.so) +// DocsCode 1 + +// DocsCode 2 +// CMakeLists.txt +cmake_minimum_required(VERSION 3.4.1) +project(DemoSO) +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${NATIVERENDER_ROOT_PATH} +${NATIVERENDER_ROOT_PATH}/include) +add_library(demoso SHARED hello.cpp) // 添加libdemoso.so文件 +// 添加依赖库liba.so、libb.so。 注意需要带上路径、不然无法找到对应的so库 +target_link_libraries(demoso PUBLIC libace_napi.z.so ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}/liba.so ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}/libb.so) + +// index.ets +import { hilog } from '@kit.LancetKit'; +import testNapi from 'libdemoso.so'; +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + private path: string = ''; + context = this.getUIContext(); + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.path = this.context.getHostContext()?.bundleCodeDir; // 获取路径 + hilog.info(0x0000, 'testTag', 'Test NAPI 5 + 3 = %{public}d', testNapi.add(5, 3, this.path + '/libs/arm64/liba.so')); // 调用native侧函数 + hilog.info(0x0000, 'testTag', 'Test NAPI 5 - 3 = %{public}d', testNapi.sub(5, 3, this.path + '/libs/arm64/libb.so')); + }) + } + .width('100%') + } + .height('100%') + } +} + +// index.d.ts +export const add: (a: number, b: number, path: string) => number; +export const sub: (a: number, b: number, path: string) => number; + +// hello.cpp +#include "a.h" +#include "b.h" +#include "napi/native_api.h" + +static napi_value Add(napi_env env, napi_callback_info info) { + size_t requireArgc = 3; + size_t argc = 3; + napi_value args[3] = {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); + napi_valuetype valuetype2; + napi_typeof(env, args[2], &valuetype2); + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + char path[255]; + size_t size = 255; + napi_get_value_string_utf8(env, args[2], path, 255, &size); + int res = geta(path, value0, value1); // 调用函数并传递沙箱路径 + napi_value sum; + napi_create_int32(env, res, &sum); + return sum; +} +static napi_value Sub(napi_env env, napi_callback_info info) { + size_t requireArgc = 3; + size_t argc = 3; + napi_value args[3] = {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); + napi_valuetype valuetype2; + napi_typeof(env, args[2], &valuetype2); + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + char path[255]; + size_t size = 255; + napi_get_value_string_utf8(env, args[2], path, 255, &size); + int res = getb(path, value0, value1); // 调用函数并传递沙箱路径 + napi_value sum; + napi_create_int32(env, res, &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}, + {"sub", nullptr, Sub, 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 = "demoso", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterDemosoModule(void) { napi_module_register(&demoModule); } +// DocsCode 2 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/NapiModuleStructure.ets b/NdkDevelopment/entry/src/main/ets/pages/NapiModuleStructure.ets new file mode 100644 index 0000000000000000000000000000000000000000..d673717bae15cb0be6e6b187a9600ecc2d22744a --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/NapiModuleStructure.ets @@ -0,0 +1,30 @@ +/* +* 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. +*/ + +/* +* FAQ:napi_module结构体字段描述解析 +*/ + +// DocsCode 1 +static napi_module demoModule = { + .nm_version = 1, // nm版本号,默认值为1 + .nm_flags = 0, // nm标识符 + .nm_filename = nullptr, // 文件名,暂不关注,使用默认值即可 + .nm_register_func = Init, // 指定nm的入口函数 + .nm_modname = "entry", // 指定ArkTS页面导入的模块名 + .nm_priv = ((void *)0), // 暂不关注,使用默认即可 + .reserved = {0}, // 暂不关注,使用默认值即可 +}; +// DocsCode 1 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/NativeSideAccessRawfile.ets b/NdkDevelopment/entry/src/main/ets/pages/NativeSideAccessRawfile.ets new file mode 100644 index 0000000000000000000000000000000000000000..1d674bc22d392d1174b3418dbf7112681bf9ac99 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/NativeSideAccessRawfile.ets @@ -0,0 +1,114 @@ +/* +* 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. +*/ + +/* +* FAQ:如何在Native侧访问应用包内Rawfile资源 +*/ + +// DocsCode 1 +import { resourceManager } from "@kit.LocalizationKit"; +export const getRawFileContent: (resMgr: resourceManager.ResourceManager, path: string) => Uint8Array; +// DocsCode 1 + +// DocsCode 2 +#include "napi/native_api.h" +#include +#include +#include "hilog/log.h" + +const int GLOBAL_RESMGR = 0xFF00; +const char *TAG = "[Sample_rawfile]"; +namespace { + napi_value CreateJsArrayValue(napi_env env, std::unique_ptr &data, long length) + { + napi_value buffer; + napi_status status = napi_create_external_arraybuffer( + env, data.get(), length, + [](napi_env env, void *data, void *hint) { + delete[] static_cast(data); + }, + nullptr, &buffer); + if (status != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create external array buffer"); + return nullptr; + } + napi_value result = nullptr; + status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result); + if (status != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create media typed array"); + return nullptr; + } + data.release(); + return result; + } +} +static napi_value GetRawFileContent(napi_env env, napi_callback_info info) +{ + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "GetFileContent Begin"); + size_t requireArgc = 3; + size_t argc = 2; + napi_value argv[2] = { nullptr }; + // 获取参数信息 + napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。 + NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); + size_t strSize; + char strBuf[256]; + napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize); + std::string filename(strBuf, strSize); + // 获取rawfile指针对象 + RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); + if (rawFile != nullptr) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); + } + // 获取rawfile大小并申请内存 + long len = OH_ResourceManager_GetRawFileSize(rawFile); + std::unique_ptr data= std::make_unique(len); + // 一次性读取rawfile全部内容 + int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len); + // 关闭打开的指针对象 + OH_ResourceManager_CloseRawFile(rawFile); + OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); + // 转为js对象 + return CreateJsArrayValue(env, data, len); +} +// DocsCode 2 + +// DocsCode 3 +import testNapi from 'libnativeaccessres.so' // 导入so + +@Entry +@Component +struct Index { + @State message: string = 'Native Access Resource'; + context = this.getUIContext(); + private resMgr = this.context.getHostContext()?.resourceManager; // 获取本应用包的资源对象 + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let rawfileContext = testNapi.getRawFileContent(this.resMgr, 'rawfile.txt'); + console.log("rawfileContext" + rawfileContext); + }) + } + .width('100%') + } + .height('100%') + } +} +// DocsCode 3 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/NativeSideCrossModuleAccessR.ets b/NdkDevelopment/entry/src/main/ets/pages/NativeSideCrossModuleAccessR.ets new file mode 100644 index 0000000000000000000000000000000000000000..91c20a994586fe91d5b332ef9ddab6a2da7d9c12 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/NativeSideCrossModuleAccessR.ets @@ -0,0 +1,114 @@ +/* +* 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. +*/ + +/* +* FAQ:如何在Native侧跨模块访问资源 +*/ + +// DocsCode 1 +import { resourceManager } from "@kit.LocalizationKit"; +export const getRawFileContent: (resMgr: resourceManager.ResourceManager, path: string) => Uint8Array; +// DocsCode 1 + +// DocsCode 2 +#include "napi/native_api.h" +#include +#include +#include "hilog/log.h" + +const int GLOBAL_RESMGR = 0xFF00; +const char *TAG = "[Sample_rawfile]"; +namespace { + napi_value CreateJsArrayValue(napi_env env, std::unique_ptr &data, long length) + { + napi_value buffer; + napi_status status = napi_create_external_arraybuffer( + env, data.get(), length, + [](napi_env env, void *data, void *hint) { + delete[] static_cast(data); + }, + nullptr, &buffer); + if (status != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create external array buffer"); + return nullptr; + } + napi_value result = nullptr; + status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result); + if (status != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create media typed array"); + return nullptr; + } + data.release(); + return result; + } +} +static napi_value GetRawFileContent(napi_env env, napi_callback_info info) +{ + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "GetFileContent Begin"); + size_t requireArgc = 3; + size_t argc = 2; + napi_value argv[2] = { nullptr }; + // 获取参数信息 + napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。 + NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); + size_t strSize; + char strBuf[256]; + napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize); + std::string filename(strBuf, strSize); + // 获取rawfile指针对象 + RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); + if (rawFile != nullptr) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); + } + // 获取rawfile大小并申请内存 + long len = OH_ResourceManager_GetRawFileSize(rawFile); + std::unique_ptr data= std::make_unique(len); + // 一次性读取rawfile全部内容 + int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len); + // 关闭打开的指针对象 + OH_ResourceManager_CloseRawFile(rawFile); + OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); + // 转为js对象 + return CreateJsArrayValue(env, data, len); +} +// DocsCode 2 + +// DocsCode 3 +import testNapi from 'libnativecrossmoduleaccessres.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Native Cross Module Access Resource'; + context = this.getUIContext(); + private resMgr = this.context.getHostContext()?.createModuleContext('NativeAccessRes').resourceManager; // 获取本应用包的资源对象 + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let rawfileContext = testNapi.getRawFileContent(this.resMgr, 'rawfile.txt'); + console.log("rawfileContext" + rawfileContext); + }) + } + .width('100%') + } + .height('100%') + } +} +// DocsCode 3 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/NativeSideOperableFileDirectory.ets b/NdkDevelopment/entry/src/main/ets/pages/NativeSideOperableFileDirectory.ets new file mode 100644 index 0000000000000000000000000000000000000000..4fb3f430f6ecadd52e1a002e02e464eac7b27477 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/NativeSideOperableFileDirectory.ets @@ -0,0 +1,26 @@ +/* +* 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. +*/ + +/* +* FAQ:Native侧如何获取可操作的文件目录 +*/ + +// DocsCode 1 +import { common } from '@kit.AbilityKit'; + +context = this.getUIContext(); +let hostContext = this.context.getHostContext() as common.UIAbilityContext; +let filesDir = hostContext.filesDir; +// DocsCode 1 \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/module.json5 b/NdkDevelopment/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a1cea8b6a4560cee7bda7a2db52f310c035ab6c8 --- /dev/null +++ b/NdkDevelopment/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/NdkDevelopment/entry/src/main/resources/base/element/color.json b/NdkDevelopment/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/NdkDevelopment/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/NdkDevelopment/entry/src/main/resources/base/element/float.json b/NdkDevelopment/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..a0a93dd91fd48f08f3a9532c76e9b26e68d4c034 --- /dev/null +++ b/NdkDevelopment/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/resources/base/element/string.json b/NdkDevelopment/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f94595515a99e0c828807e243494f57f09251930 --- /dev/null +++ b/NdkDevelopment/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "label" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/resources/base/media/background.png b/NdkDevelopment/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/NdkDevelopment/entry/src/main/resources/base/media/background.png differ diff --git a/NdkDevelopment/entry/src/main/resources/base/media/foreground.png b/NdkDevelopment/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/NdkDevelopment/entry/src/main/resources/base/media/foreground.png differ diff --git a/NdkDevelopment/entry/src/main/resources/base/media/layered_image.json b/NdkDevelopment/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/NdkDevelopment/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/NdkDevelopment/entry/src/main/resources/base/media/startIcon.png b/NdkDevelopment/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/NdkDevelopment/entry/src/main/resources/base/media/startIcon.png differ diff --git a/NdkDevelopment/entry/src/main/resources/base/profile/backup_config.json b/NdkDevelopment/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/NdkDevelopment/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/resources/base/profile/main_pages.json b/NdkDevelopment/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..09ab5f84a5d6aa6153214fa2a404752f172a10f3 --- /dev/null +++ b/NdkDevelopment/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,6 @@ +{ + "src": [ + "pages/Index", + "pages/CSideOpenFile" + ] +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/resources/dark/element/color.json b/NdkDevelopment/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/NdkDevelopment/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/mock/mock-config.json5 b/NdkDevelopment/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7a73a41bfdf76d6f793007240d80983a52f15f97 --- /dev/null +++ b/NdkDevelopment/entry/src/mock/mock-config.json5 @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/ohosTest/ets/test/Ability.test.ets b/NdkDevelopment/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..85c78f67579d6e31b5f5aeea463e216b9b141048 --- /dev/null +++ b/NdkDevelopment/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,35 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/ohosTest/ets/test/List.test.ets b/NdkDevelopment/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/NdkDevelopment/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,5 @@ +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/ohosTest/module.json5 b/NdkDevelopment/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..55725a929993a8a18b3808d41ef037759440488b --- /dev/null +++ b/NdkDevelopment/entry/src/ohosTest/module.json5 @@ -0,0 +1,13 @@ +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/NdkDevelopment/entry/src/test/List.test.ets b/NdkDevelopment/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb5b5c3731e283dd507c847560ee59bde477bbc7 --- /dev/null +++ b/NdkDevelopment/entry/src/test/List.test.ets @@ -0,0 +1,5 @@ +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/NdkDevelopment/entry/src/test/LocalUnit.test.ets b/NdkDevelopment/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..165fc1615ee8618b4cb6a622f144a9a707eee99f --- /dev/null +++ b/NdkDevelopment/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,33 @@ +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/NdkDevelopment/hvigor/hvigor-config.json5 b/NdkDevelopment/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a63d34ae5ce5833b3874807e2b8d472687c6c5bf --- /dev/null +++ b/NdkDevelopment/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.0.4", + "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/NdkDevelopment/hvigorfile.ts b/NdkDevelopment/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/NdkDevelopment/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/NdkDevelopment/oh-package-lock.json5 b/NdkDevelopment/oh-package-lock.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7fcf818273347b97063c0c0a151bb14770ca1c79 --- /dev/null +++ b/NdkDevelopment/oh-package-lock.json5 @@ -0,0 +1,27 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "@ohos/hamock@1.0.0": "@ohos/hamock@1.0.0", + "@ohos/hypium@1.0.21": "@ohos/hypium@1.0.21" + }, + "packages": { + "@ohos/hamock@1.0.0": { + "name": "@ohos/hamock", + "version": "1.0.0", + "integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==", + "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har", + "registryType": "ohpm" + }, + "@ohos/hypium@1.0.21": { + "name": "@ohos/hypium", + "version": "1.0.21", + "integrity": "sha512-iyKGMXxE+9PpCkqEwu0VykN/7hNpb+QOeIuHwkmZnxOpI+dFZt6yhPB7k89EgV1MiSK/ieV/hMjr5Z2mWwRfMQ==", + "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.21.har", + "registryType": "ohpm" + } + } +} \ No newline at end of file diff --git a/NdkDevelopment/oh-package.json5 b/NdkDevelopment/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..75e4e229db0f608fc3d9471c8819d0e52fb403c5 --- /dev/null +++ b/NdkDevelopment/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "modelVersion": "5.0.4", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.21", + "@ohos/hamock": "1.0.0" + } +}