添加调用native so的共享库

This commit is contained in:
DevWiki 2024-04-24 10:43:47 +08:00
parent 58575893e2
commit 235c409336
22 changed files with 390 additions and 0 deletions

View File

@ -79,6 +79,18 @@
]
}
]
},
{
"name": "native_lib",
"srcPath": "./native_lib",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}

7
native_lib/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test
oh-package-lock.json5

0
native_lib/Index.ets Normal file
View File

View File

@ -0,0 +1,36 @@
{
"apiType": "stageMode",
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "",
"cppFlags": ""
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": true,
"files": [
"./obfuscation-rules.txt"
]
}
}
},
"nativeLib": {
"debugSymbol": {
"strip": true,
"exclude": []
}
}
},
],
"targets": [
{
"name": "default"
}
]
}

6
native_lib/hvigorfile.ts Normal file
View File

@ -0,0 +1,6 @@
import { hspTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}

View File

@ -0,0 +1,18 @@
# 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://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
# 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

View File

@ -0,0 +1,12 @@
{
"name": "native_lib",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "Index.ets",
"author": "",
"license": "Apache-2.0",
"packageType": "InterfaceHar",
"dependencies": {
"libnatvie_lib.so": "file:./src/main/cpp/types/libnatvie_lib"
}
}

View File

@ -0,0 +1,16 @@
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(myNpmLib)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
if(DEFINED PACKAGE_FIND_FILE)
include(${PACKAGE_FIND_FILE})
endif()
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)
add_library(native_lib SHARED napi_init.cpp)
target_link_libraries(native_lib PUBLIC ../../../../cppLib/dist/lib/arm64-v8a)

View File

@ -0,0 +1,23 @@
//
// Created on 2024/4/22.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#ifndef HM4DEMO_CALCULATE_H
#define HM4DEMO_CALCULATE_H
#include <string>
struct CalculateInfo {
std::string name;
std::string versionName;
int versionCode;
};
class Calculate {
public:
static Calculate& getInstance();
int add(int a, int b);
CalculateInfo getInfo();
};
#endif //HM4DEMO_CALCULATE_H

View File

@ -0,0 +1,65 @@
//
// Created on 2024/4/22.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "Calculator.h"
#include "Calculate.h"
Calculator::Calculator() {
_calculate = &Calculate::getInstance();
}
Calculator::~Calculator() {
}
Calculator *util_get_napi_info(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv)
{
napi_value thisVar = nullptr;
void *data = nullptr;
napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
Calculator *calculator = nullptr;
napi_unwrap(env, thisVar, (void **)&calculator);
return calculator;
}
Calculate *util_get_napi_calculate(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv)
{
Calculator *calculator = util_get_napi_info(env, cbinfo, argc, argv);
if (calculator) {
return calculator->_calculate;
}
return nullptr;
}
napi_value calculate_add(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2] = {0};
Calculator *calculator = util_get_napi_info(env, info, argc, argv);
int32_t result = 0;
napi_status a = napi_ok;
a = napi_get_value_int32(env, argv[0], &result);
napi_status b = napi_ok;
b = napi_get_value_int32(env, argv[1], &result);
napi_value value;
napi_create_int32(env, calculator->_calculate->add(a, b), &value);
return value;
}
CalculateInfo calculate_getInfo(napi_env env, napi_callback_info info) {
size_t argc = 0;
napi_value argv[1] = {0};
Calculator *calculator = util_get_napi_info(env, info, argc, argv);
CalculateInfo info2;
info2.name = calculator->_calculate->getInfo().name;
info2.versionCode = calculator->_calculate->getInfo().versionCode;
info2.versionName = calculator->_calculate->getInfo().versionName;
napi_value value;
}

View File

@ -0,0 +1,27 @@
//
// Created on 2024/4/22.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#ifndef HM4DEMO_CALCULATOR_H
#define HM4DEMO_CALCULATOR_H
#include "Calculate.h"
#include "js_native_api.h"
class Calculator {
public:
Calculator();
~Calculator();
Calculate *_calculate;
};
Calculator *util_get_napi_info(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv);
Calculate *util_get_napi_calculate(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv);
int add(int a, int b);
CalculateInfo getInfo();
#endif //HM4DEMO_CALCULATOR_H

View File

@ -0,0 +1,55 @@
#include "napi/native_api.h"
#include "Calculator.h"
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);
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
napi_value sum;
napi_create_double(env, value0 + value1, &sum);
return sum;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {{"add", nullptr, Calculator::add, nullptr, nullptr, nullptr, napi_default, nullptr},
{"getInfo", nullptr, GetInfo, nullptr, nullptr, nullptr, napi_default, nullptr},
{"getInstance", nullptr, GetInstance, 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 = "native_lib",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterNatvie_libModule(void)
{
napi_module_register(&demoModule);
}

View File

@ -0,0 +1,5 @@
export class Calculate {
add(a: number, b: number): number;
static getInstance(): Calculate;
getInfo(): Object;
}

View File

@ -0,0 +1,6 @@
{
"name": "native_lib_lib.so",
"types": "./index.d.ts",
"version": "",
"description": "Please describe the basic information."
}

View File

@ -0,0 +1,21 @@
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
})
}
.width('100%')
}
.height('100%')
}
}

View File

@ -0,0 +1,8 @@
import { Calculate } from 'libnatvie_lib.so';
export class Calculator {
static add(a: number, b:number) : number {
return Calculate.add(a, b);
}
}

View File

@ -0,0 +1,14 @@
{
"module": {
"name": "native_lib",
"type": "shared",
"description": "$string:shared_desc",
"deviceTypes": [
"phone",
"tablet",
"2in1"
],
"deliveryWithInstall": true,
"pages": "$profile:main_pages"
}
}

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "white",
"value": "#FFFFFF"
}
]
}

View File

@ -0,0 +1,8 @@
{
"string": [
{
"name": "shared_desc",
"value": "description"
}
]
}

View File

@ -0,0 +1,5 @@
{
"src": [
"pages/Index"
]
}

View File

@ -0,0 +1,5 @@
import localUnitTest from './LocalUnit.test';
export default function testsuite() {
localUnitTest();
}

View File

@ -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);
});
});
}