diff --git a/NdkDevelopment/Screenbrightness/.gitignore b/NdkDevelopment/Screenbrightness/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/build-profile.json5 b/NdkDevelopment/Screenbrightness/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1d15b2984ca1ef176735bc5bc4857df468ff00da --- /dev/null +++ b/NdkDevelopment/Screenbrightness/build-profile.json5 @@ -0,0 +1,49 @@ +// FAQ:如何在Native侧直接使用其他模块的ArkTS方法 +{ + "apiType": "stageMode", + // [Start load_module_option] + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + }, + "arkOptions": { + "runtimeOnly": { + "sources": [ + "./src/main/ets/pages/Test.ets" + ] + } + }, + }, + // [End load_module_option] + "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/NdkDevelopment/Screenbrightness/hvigorfile.ts b/NdkDevelopment/Screenbrightness/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/libs/arm64-v8a/libapplication.so b/NdkDevelopment/Screenbrightness/libs/arm64-v8a/libapplication.so new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/NdkDevelopment/Screenbrightness/libs/arm64-v8a/libimportthirdpartylibraries.so b/NdkDevelopment/Screenbrightness/libs/arm64-v8a/libimportthirdpartylibraries.so new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/NdkDevelopment/Screenbrightness/obfuscation-rules.txt b/NdkDevelopment/Screenbrightness/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/oh-package-lock.json5 b/NdkDevelopment/Screenbrightness/oh-package-lock.json5 new file mode 100644 index 0000000000000000000000000000000000000000..aa3fe25012e754b496a06c865feb4f18c8115fdd --- /dev/null +++ b/NdkDevelopment/Screenbrightness/oh-package-lock.json5 @@ -0,0 +1,39 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "libapplication.so@src/main/cpp/types/libapplication": "libapplication.so@src/main/cpp/types/libapplication", + "libfuncencapsulation.so@src/main/cpp/types/libfuncencapsulation": "libfuncencapsulation.so@src/main/cpp/types/libfuncencapsulation", + "libimportthirdpartylibraries.so@src/main/cpp/types/libimportthirdpartylibraries": "libimportthirdpartylibraries.so@src/main/cpp/types/libimportthirdpartylibraries", + "libscreenbrightness.so@src/main/cpp/types/libscreenbrightness": "libscreenbrightness.so@src/main/cpp/types/libscreenbrightness" + }, + "packages": { + "libapplication.so@src/main/cpp/types/libapplication": { + "name": "libapplication.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libapplication", + "registryType": "local" + }, + "libfuncencapsulation.so@src/main/cpp/types/libfuncencapsulation": { + "name": "libfuncencapsulation.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libfuncencapsulation", + "registryType": "local" + }, + "libimportthirdpartylibraries.so@src/main/cpp/types/libimportthirdpartylibraries": { + "name": "libimportthirdpartylibraries.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libimportthirdpartylibraries", + "registryType": "local" + }, + "libscreenbrightness.so@src/main/cpp/types/libscreenbrightness": { + "name": "libscreenbrightness.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libscreenbrightness", + "registryType": "local" + } + } +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/oh-package.json5 b/NdkDevelopment/Screenbrightness/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fd9bf069393b5a08c47a08bf64611ad363123cfc --- /dev/null +++ b/NdkDevelopment/Screenbrightness/oh-package.json5 @@ -0,0 +1,14 @@ +{ + "name": "screenbrightness", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libscreenbrightness.so": "file:./src/main/cpp/types/libscreenbrightness", + "libfuncencapsulation.so": "file:./src/main/cpp/types/libfuncencapsulation", + "libimportthirdpartylibraries.so": "file:./src/main/cpp/types/libimportthirdpartylibraries", + "libapplication.so": "file:./src/main/cpp/types/libapplication" + } +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/CMakeLists.txt b/NdkDevelopment/Screenbrightness/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f65ebb9776d93bd17c92b147f06cd6de96031c70 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/CMakeLists.txt @@ -0,0 +1,27 @@ +# FAQ:如何在Native侧获取屏幕亮度 +# FAQ:如何在Native侧添加debug版本声明 +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(NdkDevelopment) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +#[Start native_debug] +if(CMAKE_BUILD_TYPE STREQUAL Debug) + add_definitions(-D_DEBUG) +endif() +#[End native_debug] + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(screenbrightness SHARED napi_init.cpp) +add_library(funcencapsulation SHARED napi_native_class.cpp) +# [Start screen_brightness_index] +# Screenbrightness/src/main/cpp/CMakeLists.txt +target_link_libraries(screenbrightness PUBLIC libace_napi.z.so libhilog_ndk.z.so) +# [End screen_brightness_index] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/DefDebug.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/DefDebug.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2a84c24622d482c2061bc18fb98f09ea579b8a7 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/DefDebug.cpp @@ -0,0 +1,20 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何在Native侧添加debug版本声明 +//[Start native_debug_cpp] +#include "napi/native_api.h" +#include "hilog/log.h" +#define LOG_TAG "Pure" + +static napi_value DefDebug(napi_env env, napi_callback_info info) { +#ifdef _DEBUG + OH_LOG_INFO(LOG_APP, "debug enter Project"); +#else + OH_LOG_INFO(LOG_APP, "release enter Project"); +#endif + return nullptr; +} +//[End native_debug_cpp] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/WriteFile.h b/NdkDevelopment/Screenbrightness/src/main/cpp/WriteFile.h new file mode 100644 index 0000000000000000000000000000000000000000..7f0f67c5fcc93b57d55f6dc70de8356586c1fffa --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/WriteFile.h @@ -0,0 +1,16 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +#include "napi/native_api.h" + +#ifndef NDKDEVELOPMENT_WRITEFILE_H +#define NDKDEVELOPMENT_WRITEFILE_H + +class WriteFile { + public: + static napi_value WriteTemporaryFile(napi_env env, napi_callback_info info); +}; + +#endif //NDKDEVELOPMENT_WRITEFILE_H diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/napi_get_env.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_get_env.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96ec297aef6e54f88a47385c75b40b4160705b37 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_get_env.cpp @@ -0,0 +1,71 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:NAPI执行上层回调时,如何获取env +// [Start get_env] +#include "napi/native_api.h" +#include +#include "hilog/log.h" + +napi_ref cbObj = nullptr; +// Thread safety function +napi_threadsafe_function tsfn; +// Native side Value Value +static int cValue; + + +// Subthread running function +static void CallJs(napi_env env, napi_value js_cb, void *context, void *data) { + std::thread::id this_id = std::this_thread::get_id(); + OH_LOG_INFO(LOG_APP, "threadId3 is +%{public}d", this_id); + // Get reference value + napi_get_reference_value(env, cbObj, &js_cb); + + // Create an ArkTS number as an input parameter for the ArkTS function. + napi_value argv; + napi_create_int32(env, cValue, &argv); + + napi_value result = nullptr; + napi_call_function(env, nullptr, js_cb, 1, &argv, &result); + + napi_get_value_int32(env, result, &cValue); + + napi_delete_reference(env, cbObj); +} + +// Native main thread +static napi_value ThreadsTest(napi_env env, napi_callback_info info) { + // The number of parameters obtained from ArkTS side + size_t argc = 1; + napi_value js_cb, work_name; + + // Get ArkTS parameters + napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr); + + // Napi_ref cbObj pointing to napi_value js_cb + napi_create_reference(env, js_cb, 1, &cbObj); + + // Create workname using UTF8 encoded C string data + napi_create_string_utf8(env, "Work Item", NAPI_AUTO_LENGTH, &work_name); + + // Create thread safe function + napi_create_threadsafe_function(env, js_cb, NULL, work_name, 0, 1, NULL, NULL, NULL, CallJs, &tsfn); + + std::thread::id this_id = std::this_thread::get_id(); + OH_LOG_INFO(LOG_APP, "threadId1 is +%{public}d", this_id); + + // Calling thread safe functions in other threads + std::thread t([]() { + // Can obtain thread ID + std::thread::id this_id = std::this_thread::get_id(); + OH_LOG_INFO(LOG_APP, "threadId2 is +%{public}d", this_id); + napi_acquire_threadsafe_function(tsfn); + napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking); + }); + t.detach(); + + return NULL; +} +// [End get_env] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/napi_init.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..080bf24f41f4c150e6473d3363e188c55bf395be --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_init.cpp @@ -0,0 +1,76 @@ +// FAQ:如何在Native侧获取屏幕亮度 +// [Start screen_brightness_cpp] +// Screenbrightness/src/main/cpp/napi_init.cpp +#include "hilog/log.h" +#include "napi/native_api.h" +#include + + +#define LOG_TAG "Pure" + + +static napi_value Success(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + napi_value obj = args[0]; + napi_value key; + napi_value rv; + std::string x = "value"; + napi_create_string_utf8(env, x.c_str(), 5, &key); + napi_get_property(env, obj, key, &rv); + uint32_t result; + napi_get_value_uint32(env, rv, &result); + OH_LOG_INFO(LOG_APP, "napi get brightness %{public}d", result); + return nullptr; +}; +static napi_value Fail(napi_env env, napi_callback_info info) { return nullptr; }; +static napi_value Complete(napi_env env, napi_callback_info info) { return nullptr; }; +static napi_value NapiLoadModule(napi_env env, napi_callback_info info) { + napi_value result; + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_status get = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + char path[64] = {0}; + size_t typeLen = 0; + napi_get_value_string_utf8(env, args[0], path, sizeof(path), &typeLen); + napi_status status = napi_load_module(env, path, &result); + if (status != napi_ok) { + OH_LOG_ERROR(LOG_APP, "napi_load_module failed"); + return nullptr; + } + napi_value outputObject; + napi_get_named_property(env, result, "getValue", &outputObject); + napi_value arg; + napi_create_object(env, &arg); + napi_property_descriptor desc[] = { + {"success", nullptr, Success, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"fail", nullptr, Fail, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"complete", nullptr, Complete, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, arg, sizeof(desc) / sizeof(desc[0]), desc); + napi_call_function(env, result, outputObject, 1, &arg, nullptr); + return result; +}; + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + {"napiLoadModule", nullptr, NapiLoadModule, 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 = "screenbrightness", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterScreenBrightnessModule(void) { napi_module_register(&demoModule); } +// [End screen_brightness_cpp] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/napi_load_module.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_load_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1879cbddfa560989fa21cea073924b751915e38c --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_load_module.cpp @@ -0,0 +1,28 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +// FAQ:如何在Native侧直接使用其他模块的ArkTS方法 +// [Start load_module_cpp] +#include "napi/native_api.h" +#include + +static napi_value LoadModule(napi_env env, napi_callback_info info) { + napi_value result; + // 1. Load modules from the Test file using napi_load_module + napi_status status = napi_load_module(env, "ets/pages/Test", &result); + napi_value testFn; + // 2. Use napi_get_named_property to obtain the test function + napi_get_named_property(env, result, "test", &testFn); + // 3. Call the function test using napi_call_function + napi_call_function(env, result, testFn, 0, nullptr, nullptr); + napi_value value; + napi_value key; + std::string keyStr = "value"; + napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); + // 4. Get variable value using napi_get_propey + napi_get_property(env, result, key, &value); + return value; +} +// [End load_module_cpp] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/napi_native_class.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_native_class.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24fd504d36fccece1daf4ee34e1780a949b9d01f --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/napi_native_class.cpp @@ -0,0 +1,71 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何将Native侧的函数封装到类中导出到ArkTS侧使用 +//[Start native_class_cpp] +#include "napi/native_api.h" +#include + +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 sub; + napi_create_double(env, value0 - value1, &sub); + return sub; +} +static napi_value Sum(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, value0 + value1, &sum); + return sum; +} +napi_value Constructor(napi_env env, napi_callback_info info) { return nullptr; } +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + std::string class_name = "TestEntryA"; + napi_property_descriptor desc[] = {{"sub", nullptr, Sub, nullptr, nullptr, nullptr, napi_static, nullptr}, + {"sum", nullptr, Sum, nullptr, nullptr, nullptr, napi_static, nullptr}}; + napi_value defined_class = nullptr; + napi_define_class(env, class_name.c_str(), class_name.length(), Constructor, nullptr, 2, desc, &defined_class); + const napi_property_descriptor exports_desc[] = { + {class_name.c_str(), nullptr, nullptr, nullptr, nullptr, defined_class, napi_default, nullptr}, + }; + napi_define_properties(env, exports, 2, exports_desc); + return exports; +} +EXTERN_C_END +//[End native_class_cpp] + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "funcencapsulation", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterScreenBrightnessModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/Index.d.ts b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4b23059d7faf64abdff7517129e81f4a04a180b9 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/Index.d.ts @@ -0,0 +1 @@ +export const add: (a: number, b: number) => number; diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/oh-package.json5 b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..efff24b1a77cb062e6e4644d46de9e5913a67d68 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libapplication/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libapplication.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/Index.d.ts b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..c14b0e1dc6e4e3c95ba86e16f95871d789037f0e --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/Index.d.ts @@ -0,0 +1,7 @@ +//FAQ:如何将Native侧的函数封装到类中导出到ArkTS侧使用 +//[Start native_class_index] +export declare class TestEntryA { + static sub (a: number, b: number) : number; + static sum (a: number, b: number) : number; +} +//[End native_class_index] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/oh-package.json5 b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3e7091b898c280fecb65ee8610e57a95d6f6c267 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libfuncencapsulation/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libfuncencapsulation.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/Index.d.ts b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..96426940037680fd64f901e83797bc4fd2256551 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/Index.d.ts @@ -0,0 +1 @@ +export const sub: (a: number, b: number) => number; diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/oh-package.json5 b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4bcd42537a4c74e8f003f7be2c852b3d06f86b96 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libimportthirdpartylibraries/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libimportthirdpartylibraries.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/Index.d.ts b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..d5ffd065165c0f4485d27d2e12036ffc03dee849 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/Index.d.ts @@ -0,0 +1,5 @@ +// FAQ:如何在Native侧获取屏幕亮度 +// [Start screen_brightness_index] +// Screenbrightness/src/main/cpp/types/libscreenbrightness/Index.d.ts +export const napiLoadModule: (a: string) => object; +// [End screen_brightness_index] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/oh-package.json5 b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7721fda4216ea5d28880d82b6e07c69b27ce7b0e --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/types/libscreenbrightness/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libscreenbrightness.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/cpp/writeFile.cpp b/NdkDevelopment/Screenbrightness/src/main/cpp/writeFile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de3218b94c61f3f77c68ea78b31166ef7bacbc86 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/cpp/writeFile.cpp @@ -0,0 +1,19 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何在Native侧往用户目录写临时文件 +//[Start write_file] +#include "WriteFile.h" +#include "napi/native_api.h" +#include +napi_value WriteFile::WriteTemporaryFile(napi_env env, napi_callback_info info) { + std::ofstream file("data/storage/el2/base/temp/2.txt"); + if (file.is_open()) { // Determine if the file can be opened normally + file << "Hello, World!"; // Write content to a file + file.close(); // close file + } + return nullptr; +} +//[End write_file] diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/pages/BundInfoPage.ets b/NdkDevelopment/Screenbrightness/src/main/ets/pages/BundInfoPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..dd4c46bf335c452ea813bc56b34615c88ce8dc9a --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/pages/BundInfoPage.ets @@ -0,0 +1,30 @@ +// FAQ:如何在Native侧获取APP版本信息 +// [Start bundle_info] +import { bundleManager } from '@kit.AbilityKit'; +import { BusinessError } from '@kit.BasicServicesKit'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{ + let versionName = bundleInfo.versionName;//Application version name + let versionNo = bundleInfo.versionCode;//Application version number + }).catch((error : BusinessError)=>{ + console.error("get bundleInfo failed,error is "+error)}) + }) + } + .width('100%') + } + .height('100%') + } +} +// [End bundle_info] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/pages/Index.ets b/NdkDevelopment/Screenbrightness/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a2987ac3419c02387454c596fbe6e0b1a5874ba8 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/pages/Index.ets @@ -0,0 +1,27 @@ +// FAQ:如何在Native侧获取屏幕亮度 +// [Start screen_brightness_ts] +// Screenbrightness/src/main/ets/pages/Index.ets +import testNapi from 'libscreenbrightness.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Get Screen Brightness'; + + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + testNapi.napiLoadModule("@system.brightness"); + }) + } + .width('100%') + } + .height('100%') + } +} +// [End screen_brightness_ts] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/pages/Test.ets b/NdkDevelopment/Screenbrightness/src/main/ets/pages/Test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6e72094a9ca51e3adb0fa7c180ef626e71ed2ffc --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/pages/Test.ets @@ -0,0 +1,9 @@ +// FAQ:如何在Native侧直接使用其他模块的ArkTS方法 +// [Start load_module_ts] +// src/main/ets/pages/Test.ets +let value = 123; +function test() { + console.log('Hello HarmonyOS'); +} +export {value, test}; +// [End load_module_ts] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestEntryA.ets b/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestEntryA.ets new file mode 100644 index 0000000000000000000000000000000000000000..c0be11b28c1d3250ca855296e65e3f0ef7e16c66 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestEntryA.ets @@ -0,0 +1,25 @@ +import { TestEntryA } from 'libfuncencapsulation.so'; +//FAQ:如何将Native侧的函数封装到类中导出到ArkTS侧使用 +//[Start native_class_ts] +@Entry +@Component +struct Index { + @State message: string = 'Function Encapsulation'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + console.info(`Test NAPI 2 + 3 = ${TestEntryA.sum(2, 3)}`); + console.info(`Test NAPI 2 - 3 = ${TestEntryA.sub(2, 3)}`); + }) + } + .width('100%') + } + .height('100%') + } +} +//[End native_class_ts] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestPage.ets b/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..5dd4d53da557404db74a099ee1a36565c23bd29a --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/pages/TestPage.ets @@ -0,0 +1,27 @@ +// FAQ:如何在ArkTS侧引用其他三方so库 +// [Start third_so] +import testNapi from 'libimportthirdpartylibraries.so'; +import myNapi from 'libapplication.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + console.info(`MyTest NAPI 2 + 3 = ${myNapi.add(2, 3)}`); + console.info(`MyTest NAPI 2 - 3 = ${testNapi.sub(2, 3)}`); + }) + } + .width('100%') + } + .height('100%') + } +} +// [End third_so] \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/ets/screenbrightnessability/ScreenbrightnessAbility.ets b/NdkDevelopment/Screenbrightness/src/main/ets/screenbrightnessability/ScreenbrightnessAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..35e4e9f7c66ceb26301ecc900cffa33fd8b9ea12 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/ets/screenbrightnessability/ScreenbrightnessAbility.ets @@ -0,0 +1,43 @@ +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class ScreenbrightnessAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + 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'); + } +} diff --git a/NdkDevelopment/Screenbrightness/src/main/module.json5 b/NdkDevelopment/Screenbrightness/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dd404e6209af2f63e3a05d279d043d1ddc2d636c --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/module.json5 @@ -0,0 +1,28 @@ +{ + "module": { + "name": "Screenbrightness", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ScreenbrightnessAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ScreenbrightnessAbility", + "srcEntry": "./ets/screenbrightnessability/ScreenbrightnessAbility.ets", + "description": "$string:ScreenbrightnessAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ScreenbrightnessAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/element/color.json b/NdkDevelopment/Screenbrightness/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/src/main/resources/base/element/float.json b/NdkDevelopment/Screenbrightness/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/element/string.json b/NdkDevelopment/Screenbrightness/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8b0d5071dbf61201c5ea4d5deb73e4c49cd774d7 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ScreenbrightnessAbility_desc", + "value": "description" + }, + { + "name": "ScreenbrightnessAbility_label", + "value": "label" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/media/background.png b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/background.png differ diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/media/foreground.png b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/foreground.png differ diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/media/layered_image.json b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/src/main/resources/base/media/startIcon.png b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/NdkDevelopment/Screenbrightness/src/main/resources/base/media/startIcon.png differ diff --git a/NdkDevelopment/Screenbrightness/src/main/resources/base/profile/main_pages.json b/NdkDevelopment/Screenbrightness/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/NdkDevelopment/Screenbrightness/src/mock/Libscreenbrightness.mock.ets b/NdkDevelopment/Screenbrightness/src/mock/Libscreenbrightness.mock.ets new file mode 100644 index 0000000000000000000000000000000000000000..c2171716d040a605ef6af71e90b937a945f2677d --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/mock/Libscreenbrightness.mock.ets @@ -0,0 +1,7 @@ +const NativeMock: Record = { + 'add': (a: number, b: number) => { + return a + b; + }, +}; + +export default NativeMock; \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/mock/mock-config.json5 b/NdkDevelopment/Screenbrightness/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..138bb4cc1842e1d9027ae5ebbce93607f64eda2d --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/mock/mock-config.json5 @@ -0,0 +1,5 @@ +{ + "libscreenbrightness.so": { + "source": "src/mock/Libscreenbrightness.mock.ets" + } +} \ No newline at end of file diff --git a/NdkDevelopment/Screenbrightness/src/ohosTest/ets/test/Ability.test.ets b/NdkDevelopment/Screenbrightness/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..85c78f67579d6e31b5f5aeea463e216b9b141048 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/src/ohosTest/ets/test/List.test.ets b/NdkDevelopment/Screenbrightness/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/src/ohosTest/module.json5 b/NdkDevelopment/Screenbrightness/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..083b041cb771ccfe3a3a82702f01cc3ee77cf8e0 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/src/ohosTest/module.json5 @@ -0,0 +1,13 @@ +{ + "module": { + "name": "Screenbrightness_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/NdkDevelopment/Screenbrightness/src/test/List.test.ets b/NdkDevelopment/Screenbrightness/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb5b5c3731e283dd507c847560ee59bde477bbc7 --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/Screenbrightness/src/test/LocalUnit.test.ets b/NdkDevelopment/Screenbrightness/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..165fc1615ee8618b4cb6a622f144a9a707eee99f --- /dev/null +++ b/NdkDevelopment/Screenbrightness/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/build-profile.json5 b/NdkDevelopment/build-profile.json5 index 03ac3a3ae9dd397844173066e5d5ee21b49455a1..ff8ff979fe2188006d40d82efd498727316e37e6 100644 --- a/NdkDevelopment/build-profile.json5 +++ b/NdkDevelopment/build-profile.json5 @@ -36,6 +36,42 @@ ] } ] + }, + { + "name": "cpplib", + "srcPath": "./cpplib", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "Screenbrightness", + "srcPath": "./Screenbrightness", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "libcpp1", + "srcPath": "./libcpp1", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] } ] } \ No newline at end of file diff --git a/NdkDevelopment/cpplib/.gitignore b/NdkDevelopment/cpplib/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/NdkDevelopment/cpplib/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/NdkDevelopment/cpplib/build-profile.json5 b/NdkDevelopment/cpplib/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ae7e98617afa8689201ca7b6cef3f7bf4e98d916 --- /dev/null +++ b/NdkDevelopment/cpplib/build-profile.json5 @@ -0,0 +1,43 @@ +//FAQ:如何修改代码工程所支持的C++语言版本 +{ + "apiType": "stageMode", + // [Start cpp_flag] + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + // Modify the following line + "cppFlags": "--std=c++17" + }, + // [End cpp_flag] + }, + "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/NdkDevelopment/cpplib/hvigorfile.ts b/NdkDevelopment/cpplib/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/obfuscation-rules.txt b/NdkDevelopment/cpplib/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/oh-package-lock.json5 b/NdkDevelopment/cpplib/oh-package-lock.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2515f108734b2ec53829cf8ba0f414b6ab370ca8 --- /dev/null +++ b/NdkDevelopment/cpplib/oh-package-lock.json5 @@ -0,0 +1,25 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "libentry.so@src/main/cpp/types/libentry": "libentry.so@src/main/cpp/types/libentry", + "libprintf.so@src/main/cpp/types/libprintf": "libprintf.so@src/main/cpp/types/libprintf" + }, + "packages": { + "libentry.so@src/main/cpp/types/libentry": { + "name": "libentry.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libentry", + "registryType": "local" + }, + "libprintf.so@src/main/cpp/types/libprintf": { + "name": "libprintf.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libprintf", + "registryType": "local" + } + } +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/oh-package.json5 b/NdkDevelopment/cpplib/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7753968fe5913feaf930bac70af175d27154a562 --- /dev/null +++ b/NdkDevelopment/cpplib/oh-package.json5 @@ -0,0 +1,12 @@ +{ + "name": "cpplib", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry", + "libprintf.so": "file:./src/main/cpp/types/libprintf" + } +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/CGetAppPackageName.h b/NdkDevelopment/cpplib/src/main/cpp/CGetAppPackageName.h new file mode 100644 index 0000000000000000000000000000000000000000..31048f6fb338a4e759d28ecf9cd6e5a7a0919681 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/CGetAppPackageName.h @@ -0,0 +1,16 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#ifndef NDKDEVELOPMENT_CGETAPPPACKAGENAME_H +#define NDKDEVELOPMENT_CGETAPPPACKAGENAME_H +#include + +class CGetAppPackageName { + public: + static napi_value GetCurrentApplicationPackageName(napi_env env, napi_callback_info info); +}; + +#endif //NDKDEVELOPMENT_CGETAPPPACKAGENAME_H diff --git a/NdkDevelopment/cpplib/src/main/cpp/CMakeLists.txt b/NdkDevelopment/cpplib/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..3ea087164cb442574b98832fb0b682de10c3c7de --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/CMakeLists.txt @@ -0,0 +1,16 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(NdkDevelopment) + +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) +add_library(printf SHARED napi_printf.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so) \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/CallSystemModule.h b/NdkDevelopment/cpplib/src/main/cpp/CallSystemModule.h new file mode 100644 index 0000000000000000000000000000000000000000..58e684e039b8d04434d36d38765ca911d3e08916 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/CallSystemModule.h @@ -0,0 +1,16 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#ifndef NDKDEVELOPMENT_CALLSYSTEMMODULE_H +#define NDKDEVELOPMENT_CALLSYSTEMMODULE_H + +#include + +class CallSystemModule { + public: + static napi_value GetDisplaySize(napi_env env, napi_callback_info info); +}; +#endif //NDKDEVELOPMENT_CALLSYSTEMMODULE_H diff --git a/NdkDevelopment/cpplib/src/main/cpp/MyDemo.h b/NdkDevelopment/cpplib/src/main/cpp/MyDemo.h new file mode 100644 index 0000000000000000000000000000000000000000..6dc6c15cbda5e0a87d4e9ad96e3d51b42d403985 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/MyDemo.h @@ -0,0 +1,24 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何导出C++自定义类,导出后如何在ArkTS侧进行类方法调用 +#include + +#ifndef NDKDEVELOPMENT_MYDEMO_H +#define NDKDEVELOPMENT_MYDEMO_H + +#endif //NDKDEVELOPMENT_MYDEMO_H +//[Start my_dome_h] +// MyDemo. h defines C++classes +class MyDemo { + public: + MyDemo(std::string m_name); + MyDemo(); + ~MyDemo(); + std::string name; + int add(int a, int b); + int sub(int a, int b); +}; +//[End my_dome_h] \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_aki.h b/NdkDevelopment/cpplib/src/main/cpp/napi_aki.h new file mode 100644 index 0000000000000000000000000000000000000000..3f2c5fccc49a3efa498fd7f638097543421097f9 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_aki.h @@ -0,0 +1,14 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#ifndef NDKDEVELOPMENT_NAPI_AKI_H +#define NDKDEVELOPMENT_NAPI_AKI_H + +class napi_aki { + +}; + +#endif //NDKDEVELOPMENT_NAPI_AKI_H diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_call_sys_module.cpp b/NdkDevelopment/cpplib/src/main/cpp/napi_call_sys_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c49ef0c39ef9892402ff17b8be9a5d1f8b511e9 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_call_sys_module.cpp @@ -0,0 +1,40 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何在Native侧调用ArkTS侧的系统能力 +// [Start call_sys_module] +#include "CallSystemModule.h" +#include "napi/native_api.h" +#include +#define LOG_TAG "Pure" + +napi_value CallSystemModule::GetDisplaySize(napi_env env, napi_callback_info info) { + // 获取arkts侧的系统库路径 + char path[64] = "@ohos.display"; + size_t typeLen = 0; + napi_value string; + napi_create_string_utf8(env, path, typeLen, &string); + // 加载系统库 + napi_value sysModule; + napi_load_module(env, path, &sysModule); + // 获取系统库中的"getDefaultDisplaySync"方法 + napi_value func = nullptr; + napi_get_named_property(env, sysModule, "getDefaultDisplaySync", &func); + napi_value funcResult; + napi_call_function(env, sysModule, func, 0, nullptr, &funcResult); + napi_value widthValue = nullptr; + napi_get_named_property(env, funcResult, "width", &widthValue); + double width; + napi_get_value_double(env, widthValue, &width); + OH_LOG_INFO(LOG_APP, "width: %{public}f", width); + napi_value heightValue = nullptr; + napi_get_named_property(env, funcResult, "height", &heightValue); + double height; + napi_get_value_double(env, heightValue, &height); + OH_LOG_INFO(LOG_APP, "height: %{public}f", height); + // 业务拿到width 和 height,可以进一步处理具体业务逻辑 + return nullptr; +} +// [End call_sys_module] \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_get_app_package_name.cpp b/NdkDevelopment/cpplib/src/main/cpp/napi_get_app_package_name.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1177f0f9d79f2b65bfc865878ba26d35a6842ed --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_get_app_package_name.cpp @@ -0,0 +1,27 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FQA:Native侧如何获取ArkTS侧的应用包名 +// [Start get_app_package_name] +#include "CGetAppPackageName.h" +#include "napi/native_api.h" +#include +#include +#include "hilog/log.h" +#define LOG_TAG "Pure" + +napi_value CGetAppPackageName::GetCurrentApplicationPackageName(napi_env env, napi_callback_info info) +{ + // Call the Native interface to obtain application information + OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); + // Convert the application package name obtained by the Native interface to the bundleName property in the JS object + napi_value bundleName; + napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName); + OH_LOG_INFO(LOG_APP, "Napi retrieves the application package name: %{public}s", nativeApplicationInfo.bundleName); + // Finally, to prevent memory leaks, manually release + free(nativeApplicationInfo.bundleName); + return nullptr; +} +// [End get_app_package_name] \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_http.cpp b/NdkDevelopment/cpplib/src/main/cpp/napi_http.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3392401d0e5b611a20e370f2db309f2e61d3e5d7 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_http.cpp @@ -0,0 +1,60 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何在Native侧集成三方库Curl,并进行HTTP数据请求 +#include "napi/native_api.h" +#include +#include + +// [Start napi_http] +#include "curl/curl.h" +// ... +// get请求和post请求数据响应函数 +size_t ReqReply(void *ptr, size_t size, size_t nmemb, void *userdata) { + string *str = reinterpret_cast(userdata); + (*str).append((char *)ptr, size * nmemb); + return size * nmemb; +} +// http GET请求配置 +CURLcode CurlGetReq(const std::string &url, std::string &response) { + // curl初始化 + CURL *curl = curl_easy_init(); + // curl返回值 + CURLcode res; + if (curl) { + // 设置Curl的请求头 + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers, "Content-Type:application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + // 设置请求的URL地址 + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + // 接收响应头数据,0代表不接收 1代表接收 + curl_easy_setopt(curl, CURLOPT_HEADER, 1); + // 设置数据接收函数 + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReqReply); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); + // 设置不使用任何信号/警报处理程序 + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); + // 设置超时时间 + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + // 开启请求 + res = curl_easy_perform(curl); + } + // 释放curl + curl_easy_cleanup(curl); + return res; +} + +static napi_value NatReq(napi_env env, napi_callback_info info) { + string getUrlStr = "http://app.huawei.com"; + string getResponseStr; + auto res = CurlGetReq(getUrlStr, getResponseStr); + if (res == CURLE_OK) { + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "pure", "response: \n%{public}s", getResponseStr.c_str()); + } + // ... +} +// [End napi_http] \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_init.cpp b/NdkDevelopment/cpplib/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cb3438618a0ccb340f2ffaa2adb22813534ef24 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_init.cpp @@ -0,0 +1,142 @@ +#include "napi/native_api.h" +#include "MyDemo.h" +#include +//[Start my_dome_cpp] +// ArkTS Object Constructor +static napi_value JsConstructor(napi_env env, napi_callback_info info) { + // Create Napi object + napi_value jDemo = nullptr; + size_t argc = 0; + napi_value args[1] = {0}; + // Get constructor input parameters + napi_get_cb_info(env, info, &argc, args, &jDemo, nullptr); + // Parameters passed in args [0] js + char name[50]; + size_t result = 0; + napi_get_value_string_utf8(env, args[0], name, sizeof(name) + 1, &result); + // Create C++objects + MyDemo *cDemo = new MyDemo(name); + OH_LOG_INFO(LOG_APP, "%{public}s", (cDemo->name).c_str()); + // Set the JS object name attribute + napi_set_named_property(env, jDemo, "name", args[0]); + // Binding JS objects with C++objects + napi_wrap( + env, jDemo, cDemo, + // Define callback function for recycling JS objects, used to destroy C++objects and prevent memory leaks + [](napi_env env, void *finalize_data, void *finalize_hint) { + MyDemo *cDemo = (MyDemo *)finalize_data; + delete cDemo; + cDemo = nullptr; + }, + nullptr, nullptr); + return jDemo; +} +// ArkTS object add function +static napi_value JsAdd(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + napi_value jDemo = nullptr; + napi_get_cb_info(env, info, &argc, args, &jDemo, nullptr); + MyDemo *cDemo = nullptr; + // Convert ArkTS object to c object + napi_unwrap(env, jDemo, (void **)&cDemo); + // Convert ArkTS object to c object + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + int cResult = cDemo->add(value0, value1); + napi_value jResult; + napi_create_int32(env, cResult, &jResult); + return jResult; +} +// arkTS object sub function +static napi_value JsSub(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2] = {nullptr}; + napi_value jDemo = nullptr; + napi_get_cb_info(env, info, &argc, args, &jDemo, nullptr); + MyDemo *cDemo = nullptr; + // Convert ArkTS object to c object + napi_unwrap(env, jDemo, (void **)&cDemo); + // Get parameters passed by ArkTS + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + int cResult = cDemo->sub(value0, value1); + napi_value jResult; + napi_create_int32(env, cResult, &jResult); + return jResult; +} +static napi_value Add(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); + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + MyDemo *demo = new MyDemo(); + // Call functions in so to perform operations + int result = demo->add(value0, value1); + napi_value sum; + napi_create_int32(env, result, &sum); + delete demo; + return sum; +} +static napi_value Sub(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); + int value0; + napi_get_value_int32(env, args[0], &value0); + int value1; + napi_get_value_int32(env, args[1], &value1); + MyDemo *demo = new MyDemo(); + // Call functions in so to perform operations + int result = demo->sub(value0, value1); + napi_value num; + napi_create_int32(env, result, &num); + delete demo; + return num; +} + +static napi_value Init(napi_env env, napi_value exports) { + // Define the methods that modules need to be exposed externally + 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); + // Establish the mapping relationship between ArkTS class and C++side through napi_fine_class, and then mount the corresponding object onto export + napi_property_descriptor classProp[] = {{"add", nullptr, JsAdd, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"sub", nullptr, JsSub, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_value jDemo = nullptr; + const char *jDemoName = "MyDemo"; + // Establish an association between ArkTS constructor and C++methods, specifying 2 props + napi_define_class(env, jDemoName, sizeof(jDemoName), JsConstructor, nullptr, + sizeof(classProp) / sizeof(classProp[0]), classProp, &jDemo); + napi_set_named_property(env, exports, jDemoName, jDemo); + return exports; +} +//[End my_dome_cpp] + +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); } \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/napi_printf.cpp b/NdkDevelopment/cpplib/src/main/cpp/napi_printf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f03c71168b40df86bcc57878211ee5fdef7b7951 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/napi_printf.cpp @@ -0,0 +1,72 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何获取Native侧printf等方法打印的信息 +//[Start native_printf_cpp] +#include "fstream" +#include "iostream" +#include "napi/native_api.h" +#include +#include +#define LOG_TAG "Pure" + +static napi_value Redirect(napi_env env, napi_callback_info info) { + // Get the JS parameters of the function + size_t argc = 1; + napi_value argv[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + // Resolve parameter 1, the destination directory for saving the file + size_t targetDirectoryNameSize; + char targetDirectoryNameBuf[512]; + napi_get_value_string_utf8(env, argv[0], targetDirectoryNameBuf, sizeof(targetDirectoryNameBuf), + &targetDirectoryNameSize); + std::string targetDirectoryName(targetDirectoryNameBuf, targetDirectoryNameSize); // target directory + OH_LOG_INFO(LOG_APP, "The target path received by the C++side === %{public}s", targetDirectoryNameBuf); + std::string targetSandboxPath = targetDirectoryName + "/Log.log"; // Saved file path + + // Use the freopen function to associate files with standard output + FILE *stdoutFile = NULL; + FILE *stderrFile = NULL; + stdoutFile = freopen(targetSandboxPath.c_str(), "a", stdout); + stderrFile = freopen(targetSandboxPath.c_str(), "a", stderr); + if (NULL == stdoutFile || NULL == stderrFile) { + OH_LOG_INFO(LOG_APP, "Recreate!"); + // Opening the file output stream of the sandbox file will create a file + std::ofstream outputFile(targetSandboxPath, std::ios::binary); + if (!outputFile) { + OH_LOG_ERROR(LOG_APP, "Unable to create target file!"); + return nullptr; + } + stdoutFile = freopen(targetSandboxPath.c_str(), "a", stdout); + stderrFile = freopen(targetSandboxPath.c_str(), "a", stderr); + if (NULL == stdoutFile || NULL == stderrFile) { + OH_LOG_ERROR(LOG_APP, "Failed!"); + return nullptr; + } + } + OH_LOG_WARN(LOG_APP, "Redirect!"); + printf("\n*****************redirect Divider*****************\n"); + return 0; +} +//[End native_printf_cpp] + +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + {"redirect", nullptr, Redirect, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "printf", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/types/libentry/Index.d.ts b/NdkDevelopment/cpplib/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f60653c14e9f05a6ea1cc2d07ab3df2fdb998de --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,12 @@ +declare namespace testNapi { + const add: (a: number, b: number) => number; + const sub: (a: number, b: number) => number; + // 定义ArkTS接口 + class MyDemo { + constructor(name:string) + name: string + add(a: number, b: number): number + sub(a: number, b: number): number + } +} +export default testNapi; \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/cpp/types/libentry/oh-package.json5 b/NdkDevelopment/cpplib/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ea410725a8826704d061021d98cf02aa76cd8016 --- /dev/null +++ b/NdkDevelopment/cpplib/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/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/Index.d.ts b/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..d16648171d0a1e8be0f6207cac665ed983d12b8e --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/Index.d.ts @@ -0,0 +1 @@ +export const redirect: (a: string) => void; diff --git a/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/oh-package.json5 b/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0d9c11c33b81165f150fe3565837390e9cb37fe3 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/cpp/types/libprintf/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libprintf.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/ets/cpplibability/CpplibAbility.ets b/NdkDevelopment/cpplib/src/main/ets/cpplibability/CpplibAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..fcdd5c0fff52ad0dcc9b24f1479e5f877652875f --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/ets/cpplibability/CpplibAbility.ets @@ -0,0 +1,49 @@ +//FAQ:如何获取Native侧printf等方法打印的信息 + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; +import testNapi from 'libprintf.so'; + +const DOMAIN = 0x0000; + +export default class CpplibAbility extends UIAbility { + // [Start native_printf_ts] + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + let file : string = this.context.getApplicationContext().filesDir; + testNapi.redirect(file); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + // [End native_printf_ts] + 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'); + } +} diff --git a/NdkDevelopment/cpplib/src/main/ets/pages/Index.ets b/NdkDevelopment/cpplib/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b0e978740a0714eb467251d3ae566d831be2160e --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/ets/pages/Index.ets @@ -0,0 +1,34 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +//[Start my_dome_ts] +import testNapi from 'libentry.so'; +// [StartExclude my_dome_ts] +const DOMAIN = 0x0000; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + aboutToAppear(): void { + // [EndExclude my_dome_ts] + new testNapi.MyDemo('abc'); + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + hilog.info(0x0000, 'testTag', 'Test NAPI 2 - 3 = %{public}d', testNapi.sub(2, 3)); + //[End my_dome_ts] + } + + build() { + Row() { + Column() { + Text(this.message) + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.message = 'Welcome'; + hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/NdkDevelopment/cpplib/src/main/module.json5 b/NdkDevelopment/cpplib/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..60b90c305f2649cfbe44e838bfee6167c012bd13 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/module.json5 @@ -0,0 +1,28 @@ +{ + "module": { + "name": "cpplib", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CpplibAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CpplibAbility", + "srcEntry": "./ets/cpplibability/CpplibAbility.ets", + "description": "$string:CpplibAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CpplibAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/resources/base/element/color.json b/NdkDevelopment/cpplib/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/src/main/resources/base/element/float.json b/NdkDevelopment/cpplib/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/NdkDevelopment/cpplib/src/main/resources/base/element/string.json b/NdkDevelopment/cpplib/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e8adf0c6f6c154414d6d702db28739772e07e5b4 --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CpplibAbility_desc", + "value": "description" + }, + { + "name": "CpplibAbility_label", + "value": "label" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/main/resources/base/media/background.png b/NdkDevelopment/cpplib/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/NdkDevelopment/cpplib/src/main/resources/base/media/background.png differ diff --git a/NdkDevelopment/cpplib/src/main/resources/base/media/foreground.png b/NdkDevelopment/cpplib/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/NdkDevelopment/cpplib/src/main/resources/base/media/foreground.png differ diff --git a/NdkDevelopment/cpplib/src/main/resources/base/media/layered_image.json b/NdkDevelopment/cpplib/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/src/main/resources/base/media/startIcon.png b/NdkDevelopment/cpplib/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/NdkDevelopment/cpplib/src/main/resources/base/media/startIcon.png differ diff --git a/NdkDevelopment/cpplib/src/main/resources/base/profile/main_pages.json b/NdkDevelopment/cpplib/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/NdkDevelopment/cpplib/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/NdkDevelopment/cpplib/src/mock/Libcpplib.mock.ets b/NdkDevelopment/cpplib/src/mock/Libcpplib.mock.ets new file mode 100644 index 0000000000000000000000000000000000000000..c2171716d040a605ef6af71e90b937a945f2677d --- /dev/null +++ b/NdkDevelopment/cpplib/src/mock/Libcpplib.mock.ets @@ -0,0 +1,7 @@ +const NativeMock: Record = { + 'add': (a: number, b: number) => { + return a + b; + }, +}; + +export default NativeMock; \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/mock/mock-config.json5 b/NdkDevelopment/cpplib/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0c0c15c6aa74eb13d16c83ebb57bf2f17b733237 --- /dev/null +++ b/NdkDevelopment/cpplib/src/mock/mock-config.json5 @@ -0,0 +1,5 @@ +{ + "libcpplib.so": { + "source": "src/mock/Libcpplib.mock.ets" + } +} \ No newline at end of file diff --git a/NdkDevelopment/cpplib/src/ohosTest/ets/test/Ability.test.ets b/NdkDevelopment/cpplib/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..85c78f67579d6e31b5f5aeea463e216b9b141048 --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/src/ohosTest/ets/test/List.test.ets b/NdkDevelopment/cpplib/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/src/ohosTest/module.json5 b/NdkDevelopment/cpplib/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1f12052da814e7dbd196a50b078032c5f58f1c9e --- /dev/null +++ b/NdkDevelopment/cpplib/src/ohosTest/module.json5 @@ -0,0 +1,13 @@ +{ + "module": { + "name": "cpplib_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/NdkDevelopment/cpplib/src/test/List.test.ets b/NdkDevelopment/cpplib/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb5b5c3731e283dd507c847560ee59bde477bbc7 --- /dev/null +++ b/NdkDevelopment/cpplib/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/cpplib/src/test/LocalUnit.test.ets b/NdkDevelopment/cpplib/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..165fc1615ee8618b4cb6a622f144a9a707eee99f --- /dev/null +++ b/NdkDevelopment/cpplib/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/entry/src/main/cpp/NativeMap.h b/NdkDevelopment/entry/src/main/cpp/NativeMap.h new file mode 100644 index 0000000000000000000000000000000000000000..30d6e3d25d15b01ce20fdca16d716e6d29d304ac --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/NativeMap.h @@ -0,0 +1,15 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +#include "napi/native_api.h" + +#ifndef NDKDEVELOPMENT_NATIVEMAP_H +#define NDKDEVELOPMENT_NATIVEMAP_H + +#endif //NDKDEVELOPMENT_NATIVEMAP_H +class NativeMap { +public: + static napi_value MapDemo(napi_env env, napi_callback_info info); +}; \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/NumberType.cpp b/NdkDevelopment/entry/src/main/cpp/NumberType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cce309c743c8207dc0c625104303ea8bc1a61aa --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/NumberType.cpp @@ -0,0 +1,14 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何对多个C++源文件中接口进行导出声明 +//[Start number_type] +// NumberType.cpp +#include "NumberType.h" // Import header file +// NumberType is the class name, and Add is its function +napi_value NumberType::Add(napi_env env, napi_callback_info info) { + // ... Business Function Implementation Code +} +//[End number_type] diff --git a/NdkDevelopment/entry/src/main/cpp/NumberType.h b/NdkDevelopment/entry/src/main/cpp/NumberType.h new file mode 100644 index 0000000000000000000000000000000000000000..d014dfab58a0195a55f41babbb3135fb10c01947 --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/NumberType.h @@ -0,0 +1,15 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +#include "napi/native_api.h" + +#ifndef NDKDEVELOPMENT_NUMBERTYPE_H +#define NDKDEVELOPMENT_NUMBERTYPE_H + +#endif //NDKDEVELOPMENT_NUMBERTYPE_H +class NumberType { +public: + static napi_value Add(napi_env env, napi_callback_info info); +}; \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/hello.cpp b/NdkDevelopment/entry/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c03478b701dc7e0ca5ae4296e9eb06b86b6df84e --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/hello.cpp @@ -0,0 +1,25 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何对多个C++源文件中接口进行导出声明 +//[Start number_type_add] + +#include "NumberType.h" +#include "napi/native_api.h" +// ... +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + /* Associate the externally provided interface with the written method, for example, associate add with the Add + * method. + */ + napi_property_descriptor desc[] = { + {"add", nullptr, NumberType::Add, nullptr, nullptr, nullptr, napi_default, nullptr}}; + // napi_define_properties construct a return value that contains a list of methods that correspond. + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END +// ... +//[End number_type_add] diff --git a/NdkDevelopment/entry/src/main/cpp/napi_aki.cpp b/NdkDevelopment/entry/src/main/cpp/napi_aki.cpp new file mode 100644 index 0000000000000000000000000000000000000000..116fc5746234347cecd143163e0b811b5e473b4c --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/napi_aki.cpp @@ -0,0 +1,22 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何通过AKI三方库实现ArkTS与C/C++之间的跨语言调用 +// [Start napi_aki_cpp] +#include +#include +// 1、用户自定义业务 +std::string SayHello(std::string msg){ return msg + " too.";} + +// 2、导出业务接口 +// Step 1 注册 AKI 插件 +JSBIND_ADDON(entry) // 注册 AKI 插件名: 即为编译*.so名称,规则与Node-API一致 + +// Step 2 注册 FFI 特性 +JSBIND_GLOBAL() +{ + JSBIND_FUNCTION(SayHello); +} +// [End napi_aki_cpp] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/napi_c_open_file.cpp b/NdkDevelopment/entry/src/main/cpp/napi_c_open_file.cpp index 590539199f944c6604995f09e95cd1cbf6c0a999..59dd6b63a3644d534a9dd8d026b4e08ed60bc1d0 100644 --- a/NdkDevelopment/entry/src/main/cpp/napi_c_open_file.cpp +++ b/NdkDevelopment/entry/src/main/cpp/napi_c_open_file.cpp @@ -19,7 +19,8 @@ #include "napi/native_api.h" #include -#define LOG_APP "C_OPEN_FOLE" + +#define LOG_TAG NULL void close(unsigned int fd2) { @@ -36,10 +37,9 @@ unsigned long write(unsigned int fd2, char * pData1, unsigned long bytesRead) { // [Start napi_c_read_file] 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; + size_t bytesRead; // Read the contents of the file into a buffer bytesRead = read(fd, buffer, sizeof(buffer)); if (bytesRead == -1) { @@ -52,7 +52,7 @@ static napi_value OpenFile(unsigned int fd, unsigned int fd2) { OH_LOG_INFO(LOG_APP, "read file cg"); char *pData1 = buffer; OH_LOG_INFO(LOG_APP, "file contents: \n%{public}s", pData1); - ssize_t bytesWrite; + size_t bytesWrite; bytesWrite = write(fd2, pData1, bytesRead); if (bytesWrite == -1) { OH_LOG_INFO(LOG_APP, "write file failed"); diff --git a/NdkDevelopment/entry/src/main/cpp/napi_hashmap.cpp b/NdkDevelopment/entry/src/main/cpp/napi_hashmap.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5fe5db2aee74e75003e65043f06abb8196fdd14 --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/napi_hashmap.cpp @@ -0,0 +1,34 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +// FAQ:ArkTS侧与Native侧如何进行map数据交互 +// [Start hash_map_napi] +#define LOG_DOMAIN 0x3200 // Global domain macro, identifying the business domain +#define LOG_TAG "MY_TAG" // Global tag macro, identifying module log tags +#include "NativeMap.h" +#include "hilog/log.h" +#include +#include +std::map testmap; +napi_value NativeMap::MapDemo(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); + char str1[1024]; + size_t str1_len; + napi_get_value_string_utf8(env, args[0], str1, 100, &str1_len); + int num; + napi_get_value_int32(env, args[1], &num); + testmap.insert(std::make_pair(str1, num)); + for(auto e: testmap){ + OH_LOG_ERROR(LOG_APP, "key is: %{public}s, value is %{public}d", (e.first).c_str(), e.second); + } + + return nullptr; +} +// [End hash_map_napi] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/napi_hilog.cpp b/NdkDevelopment/entry/src/main/cpp/napi_hilog.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26ddd2d78e55457250b89140971482b0f3afe469 --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/napi_hilog.cpp @@ -0,0 +1,21 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:Native侧如何使用hilog打印出日志信息 +#include +// [Start napi_hilog1] +#include "hilog/log.h" +#undef LOG_DOMAIN +#undef LOG_TAG +#define LOG_DOMAIN 0x3200 // Global domain macro, identifying the business domain +#define LOG_TAG "MY_TAG" // Global tag macro, identifying module log tags +// [End napi_hilog1] + +static napi_value NAPI_Global_SayHello(napi_env env, napi_callback_info info) { + // [Start napi_hilog2] + int a = 5, b = 10; + OH_LOG_ERROR(LOG_APP, "Pure a:%{public}d b:%{private}d.", a, b); + // [Start napi_hilog2] +} diff --git a/NdkDevelopment/entry/src/main/cpp/napi_init.cpp b/NdkDevelopment/entry/src/main/cpp/napi_init.cpp index 44bebbfb828bb7bbbb43a002ef8feaf92ab98103..ec5ae983a818fa4aab10cda75f759666978aab3c 100644 --- a/NdkDevelopment/entry/src/main/cpp/napi_init.cpp +++ b/NdkDevelopment/entry/src/main/cpp/napi_init.cpp @@ -1,11 +1,12 @@ +//FAQ:Native如何调ArkTS的方法 #include "napi/native_api.h" +#include -static napi_value Add(napi_env env, napi_callback_info info) -{ +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_get_cb_info(env, info, &argc, args, nullptr, nullptr); napi_valuetype valuetype0; napi_typeof(env, args[0], &valuetype0); @@ -23,45 +24,79 @@ static napi_value Add(napi_env env, napi_callback_info info) napi_create_double(env, value0 + value1, &sum); return sum; - } static napi_value NAPI_Global_ReadFile(napi_env env, napi_callback_info info) { // TODO: implements the code; + napi_value sum; + return sum; } static napi_value NAPI_Global_addByLibPath(napi_env env, napi_callback_info info) { // TODO: implements the code; + napi_value sum; + return sum; } -static napi_value NAPI_Global_callFunction(napi_env env, napi_callback_info info) -{ +static napi_value NAPI_Global_callFunction(napi_env env, napi_callback_info info) { // TODO: implements the code; + napi_value sum; + return sum; } static napi_value NAPI_Global_getDisplayWidthAsync(napi_env env, napi_callback_info info) { // TODO: implements the code; + napi_value sum; + return sum; } static napi_value NAPI_Global_subSobyDlOpenSo(napi_env env, napi_callback_info info) { // TODO: implements the code; + napi_value sum; + return sum; } +static napi_value NAPI_Global_getRawFileContent(napi_env env, napi_callback_info info) { + // TODO: implements the code; + napi_value sum; + return sum; +} +//[Start native_call_ts_cpp] +static napi_value NativeCallArkTS(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + // Declaring parameter array ARG + napi_value args[1] = { nullptr }; + + // Retrieve the passed parameters and place them in the parameter array 'rgs' + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + // Create int as an input parameter for ArkTS + napi_value argv = nullptr; + napi_create_int32(env, 2, &argv ); + + // Call the incoming callback and return the result + napi_value result = nullptr; + napi_call_function(env, nullptr, args[0], 1, &argv, &result); + return result; +} +//[End native_call_ts_cpp] -static napi_value NAPI_Global_getRawFileContent(napi_env env, napi_callback_info info) -{ +static napi_value NAPI_Global_SayHello(napi_env env, napi_callback_info info) { // TODO: implements the code; } 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}, + {"nativeCallArkTS", nullptr, NativeCallArkTS, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"mapDemo", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr}, {"ReadFile", nullptr, NAPI_Global_ReadFile, nullptr, nullptr, nullptr, napi_default, nullptr}, {"addByLibPath", nullptr, NAPI_Global_addByLibPath, nullptr, nullptr, nullptr, napi_default, nullptr}, {"callFunction", nullptr, NAPI_Global_callFunction, nullptr, nullptr, nullptr, napi_default, nullptr}, {"getDisplayWidthAsync", nullptr, NAPI_Global_getDisplayWidthAsync, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"subSobyDlOpenSo", nullptr, NAPI_Global_subSobyDlOpenSo, nullptr, nullptr, nullptr, napi_default, nullptr }, -{ "getRawFileContent", nullptr, NAPI_Global_getRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr } - }; + {"subSobyDlOpenSo", nullptr, NAPI_Global_subSobyDlOpenSo, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"getRawFileContent", nullptr, NAPI_Global_getRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"SayHello", nullptr, NAPI_Global_SayHello, nullptr, nullptr, nullptr, napi_default, nullptr }}; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; } @@ -73,11 +108,8 @@ static napi_module demoModule = { .nm_filename = nullptr, .nm_register_func = Init, .nm_modname = "entry", - .nm_priv = ((void*)0), - .reserved = { 0 }, + .nm_priv = ((void *)0), + .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) -{ - napi_module_register(&demoModule); -} +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/NdkDevelopment/entry/src/main/cpp/napi_read_arkts_object.cpp b/NdkDevelopment/entry/src/main/cpp/napi_read_arkts_object.cpp index 321c84310d9809f5a672a508548a45a101d61fdd..4272f91c21a38026690b6787533f37ee4e487595 100644 --- a/NdkDevelopment/entry/src/main/cpp/napi_read_arkts_object.cpp +++ b/NdkDevelopment/entry/src/main/cpp/napi_read_arkts_object.cpp @@ -1,45 +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. -*/ + * 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对象及其成员变量 -*/ + * FAQ:Native侧如何获取ArkTS侧Object对象及其成员变量 + */ // [Start napi_read_arkts_object] // Pass in the instance object and call the function in the object on the C side -#include "napi/native_api.h" -static napi_value CallFunction(napi_env env, napi_callback_info info) { +#include "napi/native_api.h" +static napi_value CallFunction(napi_env env, napi_callback_info info) { // Get the instance object - size_t argc = 1; - napi_value args[1] = {nullptr}; - napi_get_cb_info(env, info, &argc, args, NULL, NULL); + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, NULL, NULL); // A method to get an object - napi_value onCall; - napi_get_named_property(env, args[0], "onCall", &onCall); + napi_value onCall; + napi_get_named_property(env, args[0], "onCall", &onCall); // Call a function in an object - napi_value res; - napi_call_function(env, args[0], onCall, 0, nullptr, &res); - return onCall; -} -EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) { - napi_property_descriptor desc[] = { - {"callFunction", nullptr, CallFunction, nullptr, nullptr, nullptr, napi_default, nullptr}}; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; + napi_value res; + napi_call_function(env, args[0], onCall, 0, nullptr, &res); + return onCall; +} +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + {"callFunction", nullptr, CallFunction, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} } // [StartExclude napi_read_arkts_object] // [EndExclude napi_read_arkts_object] -// [End napi_read_arkts_object] +// [End napi_read_arkts_object] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/napi_str_len.cpp b/NdkDevelopment/entry/src/main/cpp/napi_str_len.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aae493f303baf432b9179fd6f2b0262868a7ec41 --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/napi_str_len.cpp @@ -0,0 +1,22 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:在Native侧如何精准获取ArkTS侧传入的字符串长度 +#include "napi/native_api.h" + +//[Start get_str_len] +static napi_value TestFunc(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + size_t len = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &len); // Get string length to len + char* buf = new char[len+1]; // Allocate a char array of appropriate size + napi_get_value_string_utf8(env, args[0], buf, len + 1, &len); // get string + // ... +} +//[End get_str_len] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/napi_str_truncation.cpp b/NdkDevelopment/entry/src/main/cpp/napi_str_truncation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e71a302fe484fa1bf410b176cc869421586f0761 --- /dev/null +++ b/NdkDevelopment/entry/src/main/cpp/napi_str_truncation.cpp @@ -0,0 +1,54 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +// FAQ:在Native侧如何处理ArkTS侧传入的字符串被截断的异常场景 +#include "napi/native_api.h" +#include +#define LOG_TAG NULL + + +class Test { +public: + static napi_value TestFunc(napi_env env, napi_callback_info info); +}; + +//[Start test_func1] +static napi_value TestFunc1(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + size_t len = 0; + char buf[5]; // Allocate a char array of length 5 + napi_get_value_string_utf8(env, args[0], buf, 1024, &len); // Get string, buf result is' abcde' + // ... +} +//[End test_func1] +//[Start test_func2] +static napi_value TestFunc2(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + size_t len = 0; + char buf[1024]; + napi_get_value_string_utf8(env, args[0], buf, 5, &len); // Get string, buf result is' abcde' + // ... +} +//[End test_func2] +//[Start test_func3] +napi_value Test::TestFunc(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + size_t len = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &len); // Get string length to len + char *buf = new char[len + 1]; // Allocate a char array of appropriate size + napi_get_value_string_utf8(env, args[0], buf, len + 1, &len); // get string + OH_LOG_ERROR(LOG_APP, "result is: b:%{public}s.", buf); + return nullptr; +} +//[End test_func3] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/cpp/types/libentry/Index.d.ts b/NdkDevelopment/entry/src/main/cpp/types/libentry/Index.d.ts index 54d8504161c599142ad3fc0489cd6a2cbd864fb0..0143de7899857937e7bd3240ff53ba5250002b6e 100644 --- a/NdkDevelopment/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/NdkDevelopment/entry/src/main/cpp/types/libentry/Index.d.ts @@ -1,3 +1,8 @@ +export const mapDemo: (a: string, b: number) => void; + +export const nativeCallArkTS: (a: object) => number; + +export const SayHello: (arg0: string) => string; export const add: (a: number, b: number) => number; export const ReadFile: (fd: number, fd1: number) => void; diff --git a/NdkDevelopment/entry/src/main/ets/pages/AkiPage.ets b/NdkDevelopment/entry/src/main/ets/pages/AkiPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..61b452015949c1a43307d19fb2ed4fe4a0d4b312 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/AkiPage.ets @@ -0,0 +1,25 @@ +// FAQ:如何通过AKI三方库实现ArkTS与C/C++之间的跨语言调用 +// [Start aki_ets] +import aki from 'libentry.so' // *. so compiled from the project + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + console.info(aki.SayHello("hello world")); // Call the code interface in the. so file + }) + } + .width('100%') + } + .height('100%') + } +} +// [End aki_ets] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/NativeCallArkTS.ets b/NdkDevelopment/entry/src/main/ets/pages/NativeCallArkTS.ets new file mode 100644 index 0000000000000000000000000000000000000000..30bc8aef2f435c0c82b03e693a0cf26675ce07b0 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/NativeCallArkTS.ets @@ -0,0 +1,31 @@ +//FAQ:Native如何调ArkTS的方法 +// [Start native_call_ts_ets] +// Introduce native capabilities through import. +import nativeModule from 'libentry.so' + + +@Entry +@Component +struct InvokeArkTSMethod { + @State message: string = 'Test Node-API nativeCallArkTS result: '; + + + build() { + Row() { + Column() { + // Call the nativeCallArkTS method, corresponding to the Native NativeCallArkTS, and call the ArkTS function in Native. + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.message += nativeModule.nativeCallArkTS((a: number) => { + return a * 2; + }); + }) + } + .width('100%') + } + .height('100%') + } +} +// [End native_call_ts_ets] \ No newline at end of file diff --git a/NdkDevelopment/entry/src/main/ets/pages/hashmap.ets b/NdkDevelopment/entry/src/main/ets/pages/hashmap.ets new file mode 100644 index 0000000000000000000000000000000000000000..5f60739ee34a4e451bdd43dae890b0ddb1b1abe9 --- /dev/null +++ b/NdkDevelopment/entry/src/main/ets/pages/hashmap.ets @@ -0,0 +1,16 @@ +// FAQ:ArkTS侧与Native侧如何进行map数据交互 +import { HashMap } from '@kit.ArkTS' +import testNapi from 'libentry.so'; + +const testFn = ()=>{ + // [Start hash_map_fn] + let hashmap: HashMap = new HashMap() + hashmap.set("Abc", 123) + hashmap.set("Bcd", 234) + hashmap.set("Cde", 345) + for (let key of hashmap.keys()) { + testNapi.mapDemo(key, hashmap.get(key)) + console.info(`key is ${key}, value is ${hashmap.get(key)}`) + } + // [End hash_map_fn] +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/.gitignore b/NdkDevelopment/libcpp1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/NdkDevelopment/libcpp1/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/build-profile.json5 b/NdkDevelopment/libcpp1/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..38bdcc9929e2c5bd7f51c4fc96a398ccebd0d6ce --- /dev/null +++ b/NdkDevelopment/libcpp1/build-profile.json5 @@ -0,0 +1,39 @@ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "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/NdkDevelopment/libcpp1/hvigorfile.ts b/NdkDevelopment/libcpp1/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/obfuscation-rules.txt b/NdkDevelopment/libcpp1/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/oh-package-lock.json5 b/NdkDevelopment/libcpp1/oh-package-lock.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5c8c8d64769f4712bca04df64e51eb46442cbc59 --- /dev/null +++ b/NdkDevelopment/libcpp1/oh-package-lock.json5 @@ -0,0 +1,18 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "libentry.so@src/main/cpp/types/libentry": "libentry.so@src/main/cpp/types/libentry" + }, + "packages": { + "libentry.so@src/main/cpp/types/libentry": { + "name": "libentry.so", + "version": "1.0.0", + "resolved": "src/main/cpp/types/libentry", + "registryType": "local" + } + } +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/oh-package.json5 b/NdkDevelopment/libcpp1/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fdf3fb68e2e294f7ec1538ccfbe930886c023b53 --- /dev/null +++ b/NdkDevelopment/libcpp1/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "libcpp1", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/cpp/CGetArkTSObject.h b/NdkDevelopment/libcpp1/src/main/cpp/CGetArkTSObject.h new file mode 100644 index 0000000000000000000000000000000000000000..46c8b5d244dbe6e987e7fe54add8be7d479b2df7 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/CGetArkTSObject.h @@ -0,0 +1,14 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +#include "napi/native_api.h" + +#ifndef NDKDEVELOPMENT_CGETARKTSOBJECT_H +#define NDKDEVELOPMENT_CGETARKTSOBJECT_H +class CGetArkTSObject { + public: + static napi_value Cal(napi_env env, napi_callback_info info); +}; +#endif //NDKDEVELOPMENT_CGETARKTSOBJECT_H diff --git a/NdkDevelopment/libcpp1/src/main/cpp/CMakeLists.txt b/NdkDevelopment/libcpp1/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4b817a33eabb1d3d65fb5350cb3a643fc1bbafca --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(NdkDevelopment) + +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) \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/cpp/UintArr.h b/NdkDevelopment/libcpp1/src/main/cpp/UintArr.h new file mode 100644 index 0000000000000000000000000000000000000000..aee8a051e7dfa3ea5da6355f963752022e4b4854 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/UintArr.h @@ -0,0 +1,14 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#ifndef NDKDEVELOPMENT_UINTARR_H +#define NDKDEVELOPMENT_UINTARR_H +#include "napi/native_api.h" +class Demo1 { + public: + static napi_value UintArr(napi_env env, napi_callback_info info); +}; +#endif //NDKDEVELOPMENT_UINTARR_H diff --git a/NdkDevelopment/libcpp1/src/main/cpp/napi_create_arkts.cpp b/NdkDevelopment/libcpp1/src/main/cpp/napi_create_arkts.cpp new file mode 100644 index 0000000000000000000000000000000000000000..137d2fa8074cff3cc95385e6f0189e189a557c0b --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/napi_create_arkts.cpp @@ -0,0 +1,25 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +//FAQ:如何在Native侧构建一个ArkTS对象 +#include "napi/native_api.h" + +static napi_value CallbackToArkTS(napi_env env, napi_callback_info info) { + // [Start napi_create_arkts1] + // Create object arg_order in the native layer + napi_value arg_object; + napi_create_object(env, &arg_object); + // [End napi_create_arkts1] + + // [Start napi_create_arkts2] + napi_value testNum, testString; + // Set the property testNum and assign a value of 123 to the arg_order object created above + napi_create_int32(env, 123, &testNum); + napi_set_named_property(env, arg_object, "testNum", testNum); + // Set the property testString and assign 'Pure' to the arg_order object created above + napi_create_string_utf8(env, "Pure", NAPI_AUTO_LENGTH, &testString); + napi_set_named_property(env, arg_object, "testString", testString); + // [End napi_create_arkts2] +} diff --git a/NdkDevelopment/libcpp1/src/main/cpp/napi_get_arkts_object.cpp b/NdkDevelopment/libcpp1/src/main/cpp/napi_get_arkts_object.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3b4870e52cf00aa580ca3ceb6617b5c58c74e80 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/napi_get_arkts_object.cpp @@ -0,0 +1,45 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". +// FAQ:Native侧如何获取ArkTS侧类实例 +#include "napi/native_api.h" +// [Start get_arkTs_object_cpp] +// Retrieve class information and call class functions +#include "CGetArkTSObject.h" +napi_value CGetArkTSObject::Cal(napi_env env, napi_callback_info info) { + size_t argc = 4; + napi_value args[4] = {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); + + // Construct class instances + napi_value demo; + napi_create_object(env, &demo); + napi_coerce_to_object(env, args[2], &demo); + + bool flag; + napi_get_value_bool(env, args[3], &flag); + + // Get the add and sub functions of class instances + napi_value add, sub, num; + napi_get_named_property(env, demo, "add", &add); + napi_get_named_property(env, demo, "sub", &sub); + + // Call ArkTS function + napi_value result; + if (flag) { + napi_call_function(env, nullptr, add, 2, args, &result); + } else { + napi_call_function(env, nullptr, sub, 2, args, &result); + } + + return result; +} +// [End get_arkTs_object_cpp] \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/cpp/napi_init.cpp b/NdkDevelopment/libcpp1/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03f28e735a7559f1b1c92f750201223fd1dcea80 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/napi_init.cpp @@ -0,0 +1,64 @@ +//FAQ:ArkTS侧如何接收Native侧的键值对进行修改并返回到Native侧 +// [Start call_back_to_arkts_cpp] +#include "napi/native_api.h" +#include "hilog/log.h" +#undef LOG_DOMAIN +#undef LOG_TAG +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "MY_TAG" + +static bool Napi_AddPropertyInt32(napi_env env, napi_value obj, const char *key, int32_t value) { + napi_value key_napi = nullptr; + napi_status status = napi_create_string_utf8(env, key, NAPI_AUTO_LENGTH, &key_napi); + napi_value value_napi = nullptr; + status = napi_create_int32(env, value, &value_napi); + status = napi_set_property(env, obj, key_napi, value_napi); + return true; +} +static napi_value CallbackToArkTS(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + // Native callback to ArkTS layer object + napi_value argv = nullptr; + napi_create_object(env, &argv); + Napi_AddPropertyInt32(env, argv, "type", 1); + Napi_AddPropertyInt32(env, argv, "index", 2); + // Native callback to ArkTS layer + napi_value result = nullptr; + napi_call_function(env, NULL, args[0], 1, &argv, &result); + // Obtain the modified object of ArkTS + napi_value typeNumber = nullptr; + napi_get_named_property(env, result, "type", &typeNumber); + int32_t number; + napi_get_value_int32(env, typeNumber, &number); + OH_LOG_INFO(LOG_APP, "ArkTS modified type:%{public}d", number); + // Return the modified object + return result; +} +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "callbackToArkTS", nullptr, CallbackToArkTS, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END +// [End call_back_to_arkts_cpp] + +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 RegisterLibcpp1Module(void) +{ + napi_module_register(&demoModule); +} diff --git a/NdkDevelopment/libcpp1/src/main/cpp/napi_unit8_array.cpp b/NdkDevelopment/libcpp1/src/main/cpp/napi_unit8_array.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cb908ad290f4b04aa7762f94aebed788ce7cbc4 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/napi_unit8_array.cpp @@ -0,0 +1,50 @@ +// +// Created on 2025/5/21. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +// FAQ:Native侧获取Uint8Array参数并返回到ArkTS侧 +#include "napi/native_api.h" +// [Start napi_unit8_array] +#include "UintArr.h" + +napi_value Demo1::UintArr(napi_env env, napi_callback_info info) { + size_t requireArgc = 1; + size_t argc = 1; + napi_value args[1] = {nullptr}; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + napi_value inputArray = args[0]; + + // Get the ArrayBuffer type + napi_typedarray_type type; + napi_value inArrayBuffer; + size_t byteOffset; + size_t length; + napi_get_typedarray_info(env, inputArray, &type, &length, nullptr, &inArrayBuffer, &byteOffset); + if (type != napi_uint8_array) { + return nullptr; + } + + // Retrieve information from the ArrayBuffer + void *data = nullptr; + size_t byte_length; + napi_get_arraybuffer_info(env, inArrayBuffer, &data, &byte_length); + + // Construct an ArrayBuffer and assign a value + napi_value output_buffer; + void *output_ptr = nullptr; + napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer); + napi_value outputArray; + napi_create_typedarray(env, type, length, output_buffer, byteOffset, &outputArray); + uint8_t *input_bytes = (uint8_t *)(data) + byteOffset; + uint8_t *array = (uint8_t *)(output_ptr); + for (size_t idx = 0; idx < length; idx++) { + array[idx] = 3; + } + + return outputArray; +} +// [End napi_unit8_array] \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/cpp/types/libentry/Index.d.ts b/NdkDevelopment/libcpp1/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..aecfaca2dc76f54078290872e2e727acdf178e07 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1 @@ +export const callbackToArkTS: (a: object) => Record \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/cpp/types/libentry/oh-package.json5 b/NdkDevelopment/libcpp1/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ea410725a8826704d061021d98cf02aa76cd8016 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/NdkDevelopment/libcpp1/src/main/ets/libcpp1ability/Libcpp1Ability.ets b/NdkDevelopment/libcpp1/src/main/ets/libcpp1ability/Libcpp1Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..701f4eea5e38563792a35ebacf2f9b7183450986 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/libcpp1ability/Libcpp1Ability.ets @@ -0,0 +1,43 @@ +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Libcpp1Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + 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'); + } +} diff --git a/NdkDevelopment/libcpp1/src/main/ets/pages/CallbackToArkts.ets b/NdkDevelopment/libcpp1/src/main/ets/pages/CallbackToArkts.ets new file mode 100644 index 0000000000000000000000000000000000000000..ad1aca6abb36a8a089a5c3acde555419f345645a --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/pages/CallbackToArkts.ets @@ -0,0 +1,36 @@ +//FAQ:ArkTS侧如何接收Native侧的键值对进行修改并返回到Native侧 +// [Start call_back_to_arkts_ets] +import testNapi from 'libentry.so'; +// [StartExclude call_back_to_arkts_ets1] +const DOMAIN = 0x0000; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + // [EndExclude call_back_to_arkts_ets1] + build() { + // [StartExclude call_back_to_arkts_ets2] + Row() { + Column() { + Text(this.message) + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + // [EndExclude call_back_to_arkts_ets2] + .onClick(() => { + let data: Record = testNapi.callbackToArkTS((value: object) => { + let obj: Record = value as Record; + console.info("pre type:" + obj["type"].toString()) + console.info(JSON.stringify(value)) + obj["type"] += 10; + return value; + }); + console.info(JSON.stringify(data)) + }) + } + // [End call_back_to_arkts_ets] + .width('100%') + } + .height('100%') + } +} diff --git a/NdkDevelopment/libcpp1/src/main/ets/pages/GetArktsObject.ets b/NdkDevelopment/libcpp1/src/main/ets/pages/GetArktsObject.ets new file mode 100644 index 0000000000000000000000000000000000000000..4923700d2176fb74b1b8f01929eb42047d66872e --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/pages/GetArktsObject.ets @@ -0,0 +1,29 @@ +// FAQ:Native侧如何获取ArkTS侧类实例 +// [Start get_arkTs_object_page] +// Pass parameters to the native side +import testNapi from 'libentry.so'; +import demo from './interface/ClassDemo1' + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let flag:Boolean = false; + console.info(`Test NAPI Result is ${testNapi.cal(2, 3, demo, true)}`) + console.info(`Num is ${demo.add(3,2)}`) + }) + } + .width('100%') + } + .height('100%') + } +} +// [End get_arkTs_object_page] \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/ets/pages/Index.ets b/NdkDevelopment/libcpp1/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..cd35f054ca946e612e4fb81900687857d40ae4c8 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/pages/Index.ets @@ -0,0 +1,24 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const DOMAIN = 0x0000; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/NdkDevelopment/libcpp1/src/main/ets/pages/Unit8ArrayPage.ets b/NdkDevelopment/libcpp1/src/main/ets/pages/Unit8ArrayPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d2ed30093040b9588bd3a985d1f0fa43ab1d660 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/pages/Unit8ArrayPage.ets @@ -0,0 +1,32 @@ +// FAQ:Native侧获取Uint8Array参数并返回到ArkTS侧 +// [Start napi_unit8_array] + +// ArkTS passes Uint8Array parameter +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + let temp = new Uint8Array(2); + temp[0] = 1; + temp[1] = 2; + console.info(`Pure inputBuffer length: ${temp.length}`); + let res = testNapi.uintArr(temp); + console.info(`Pure outputBuffer: ${res}`); + }) + } + .width('100%') + } + .height('100%') + } +} +// [End napi_unit8_array] \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/ets/pages/interface/ClassDemo1.ets b/NdkDevelopment/libcpp1/src/main/ets/pages/interface/ClassDemo1.ets new file mode 100644 index 0000000000000000000000000000000000000000..546ecaccdcf51aa69ea2692d4b653de190677ffb --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/ets/pages/interface/ClassDemo1.ets @@ -0,0 +1,15 @@ +// FAQ:Native侧如何获取ArkTS侧类实例 +// [Start get_arkTs_object_class] +// Declare Demo class +class Demo { + add(a: number, b: number): number { + return a + b; + } + + sub(a: number, b: number): number { + return a - b; + } +} + +export default new Demo(); +// [End get_arkTs_object_class] \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/module.json5 b/NdkDevelopment/libcpp1/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8950fbcee6974aaef5cba83a641e8fb4a978e589 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/module.json5 @@ -0,0 +1,28 @@ +{ + "module": { + "name": "libcpp1", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Libcpp1Ability", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Libcpp1Ability", + "srcEntry": "./ets/libcpp1ability/Libcpp1Ability.ets", + "description": "$string:Libcpp1Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Libcpp1Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/element/color.json b/NdkDevelopment/libcpp1/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/src/main/resources/base/element/float.json b/NdkDevelopment/libcpp1/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/element/string.json b/NdkDevelopment/libcpp1/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1092c6c2f8254adfca96361670e777e7cb905518 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Libcpp1Ability_desc", + "value": "description" + }, + { + "name": "Libcpp1Ability_label", + "value": "label" + } + ] +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/media/background.png b/NdkDevelopment/libcpp1/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/NdkDevelopment/libcpp1/src/main/resources/base/media/background.png differ diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/media/foreground.png b/NdkDevelopment/libcpp1/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/NdkDevelopment/libcpp1/src/main/resources/base/media/foreground.png differ diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/media/layered_image.json b/NdkDevelopment/libcpp1/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/src/main/resources/base/media/startIcon.png b/NdkDevelopment/libcpp1/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/NdkDevelopment/libcpp1/src/main/resources/base/media/startIcon.png differ diff --git a/NdkDevelopment/libcpp1/src/main/resources/base/profile/main_pages.json b/NdkDevelopment/libcpp1/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/NdkDevelopment/libcpp1/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/NdkDevelopment/libcpp1/src/mock/Liblibcpp1.mock.ets b/NdkDevelopment/libcpp1/src/mock/Liblibcpp1.mock.ets new file mode 100644 index 0000000000000000000000000000000000000000..c2171716d040a605ef6af71e90b937a945f2677d --- /dev/null +++ b/NdkDevelopment/libcpp1/src/mock/Liblibcpp1.mock.ets @@ -0,0 +1,7 @@ +const NativeMock: Record = { + 'add': (a: number, b: number) => { + return a + b; + }, +}; + +export default NativeMock; \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/mock/mock-config.json5 b/NdkDevelopment/libcpp1/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8360f55960934d9964eac50f67cb005c66b2682e --- /dev/null +++ b/NdkDevelopment/libcpp1/src/mock/mock-config.json5 @@ -0,0 +1,5 @@ +{ + "liblibcpp1.so": { + "source": "src/mock/Liblibcpp1.mock.ets" + } +} \ No newline at end of file diff --git a/NdkDevelopment/libcpp1/src/ohosTest/ets/test/Ability.test.ets b/NdkDevelopment/libcpp1/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..85c78f67579d6e31b5f5aeea463e216b9b141048 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/src/ohosTest/ets/test/List.test.ets b/NdkDevelopment/libcpp1/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/src/ohosTest/module.json5 b/NdkDevelopment/libcpp1/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..491584b73c38e3f43e7c958cd5be0e120e19fbd4 --- /dev/null +++ b/NdkDevelopment/libcpp1/src/ohosTest/module.json5 @@ -0,0 +1,13 @@ +{ + "module": { + "name": "libcpp1_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/NdkDevelopment/libcpp1/src/test/List.test.ets b/NdkDevelopment/libcpp1/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb5b5c3731e283dd507c847560ee59bde477bbc7 --- /dev/null +++ b/NdkDevelopment/libcpp1/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/libcpp1/src/test/LocalUnit.test.ets b/NdkDevelopment/libcpp1/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..165fc1615ee8618b4cb6a622f144a9a707eee99f --- /dev/null +++ b/NdkDevelopment/libcpp1/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