Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
TestToolChainKmpDemo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dsq
TestToolChainKmpDemo
Commits
b9c31bfa
Commit
b9c31bfa
authored
Mar 04, 2026
by
dsq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
日志触发
parent
af7fc89e
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
144 additions
and
18 deletions
+144
-18
libkn.so
harmonyApp/entry/libs/arm64-v8a/libkn.so
+0
-0
CMakeLists.txt
harmonyApp/entry/src/main/cpp/CMakeLists.txt
+18
-3
libkn_api.h
harmonyApp/entry/src/main/cpp/include/libkn_api.h
+2
-1
napi_init.cpp
harmonyApp/entry/src/main/cpp/napi_init.cpp
+46
-1
Index.d.ts
harmonyApp/entry/src/main/cpp/types/libentry/Index.d.ts
+10
-1
Index.ets
harmonyApp/entry/src/main/ets/pages/Index.ets
+48
-11
TestLog.ohosArm64.kt
...64Main/kotlin/com/dong/nocomposedemo/TestLog.ohosArm64.kt
+8
-0
TestLog.ohosX64.kt
...sX64Main/kotlin/com/dong/nocomposedemo/TestLog.ohosX64.kt
+12
-1
No files found.
harmonyApp/entry/libs/arm64-v8a/libkn.so
View file @
b9c31bfa
No preview for this file type
harmonyApp/entry/src/main/cpp/CMakeLists.txt
View file @
b9c31bfa
# the minimum version of CMake.
# the minimum version of CMake.
cmake_minimum_required
(
VERSION 3.5.0
)
cmake_minimum_required
(
VERSION 3.5.0
)
project
(
harmonyApp
)
project
(
KmpDemo
)
set
(
NATIVERENDER_ROOT_PATH
${
CMAKE_CURRENT_SOURCE_DIR
}
)
set
(
NATIVERENDER_ROOT_PATH
${
CMAKE_CURRENT_SOURCE_DIR
}
)
...
@@ -9,7 +9,22 @@ if(DEFINED PACKAGE_FIND_FILE)
...
@@ -9,7 +9,22 @@ if(DEFINED PACKAGE_FIND_FILE)
endif
()
endif
()
include_directories
(
${
NATIVERENDER_ROOT_PATH
}
include_directories
(
${
NATIVERENDER_ROOT_PATH
}
${
NATIVERENDER_ROOT_PATH
}
/include
)
${
NATIVERENDER_ROOT_PATH
}
/include
)
add_library
(
entry SHARED napi_init.cpp
)
add_library
(
entry SHARED napi_init.cpp
)
target_link_libraries
(
entry PUBLIC libace_napi.z.so
)
\ No newline at end of file
# 按 ABI 选 libkn.so 路径(当前 abiFilters 仅 x86_64 时只构建模拟器)
if
(
CMAKE_OHOS_ARCH_ABI STREQUAL
"x86_64"
OR OHOS_ARCH STREQUAL
"x86_64"
)
set
(
LIBKN_ABI_DIR
"x86_64"
)
else
()
set
(
LIBKN_ABI_DIR
"arm64-v8a"
)
endif
()
add_library
(
kn SHARED IMPORTED
)
set_target_properties
(
kn PROPERTIES
IMPORTED_LOCATION
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../../../libs/
${
LIBKN_ABI_DIR
}
/libkn.so"
)
message
(
STATUS
"Loading libkn.so for
${
LIBKN_ABI_DIR
}
architecture"
)
target_link_libraries
(
entry PUBLIC libace_napi.z.so
)
target_link_libraries
(
entry PRIVATE kn
)
harmonyApp/entry/src/main/cpp/include/libkn_api.h
View file @
b9c31bfa
...
@@ -73,6 +73,7 @@ typedef struct {
...
@@ -73,6 +73,7 @@ typedef struct {
libkn_KNativePtr
pinned
;
libkn_KNativePtr
pinned
;
}
libkn_kref_com_dong_nocomposedemo_OhosPlatform
;
}
libkn_kref_com_dong_nocomposedemo_OhosPlatform
;
extern
void
FaultOut
();
extern
libkn_KInt
hiLogPrintMsg
(
libkn_KUInt
type
,
libkn_KUInt
level
,
libkn_KUInt
domain
,
const
char
*
tag
,
const
char
*
message
);
extern
libkn_KInt
hiLogPrintMsg
(
libkn_KUInt
type
,
libkn_KUInt
level
,
libkn_KUInt
domain
,
const
char
*
tag
,
const
char
*
message
);
extern
void
pirntAllLevelHiLog
();
extern
void
pirntAllLevelHiLog
();
extern
void
pirntRandomHiLog
();
extern
void
pirntRandomHiLog
();
...
@@ -130,7 +131,7 @@ typedef struct {
...
@@ -130,7 +131,7 @@ typedef struct {
const
char
*
(
*
get_name
)(
libkn_kref_com_dong_nocomposedemo_OhosPlatform
thiz
);
const
char
*
(
*
get_name
)(
libkn_kref_com_dong_nocomposedemo_OhosPlatform
thiz
);
}
OhosPlatform
;
}
OhosPlatform
;
libkn_kref_com_dong_nocomposedemo_Platform
(
*
getPlatform
)();
libkn_kref_com_dong_nocomposedemo_Platform
(
*
getPlatform
)();
void
(
*
FaultOut
)();
void
(
*
FaultOut
_
)();
libkn_KInt
(
*
hiLogPrintMsg_
)(
libkn_KUInt
type
,
libkn_KUInt
level
,
libkn_KUInt
domain
,
const
char
*
tag
,
const
char
*
message
);
libkn_KInt
(
*
hiLogPrintMsg_
)(
libkn_KUInt
type
,
libkn_KUInt
level
,
libkn_KUInt
domain
,
const
char
*
tag
,
const
char
*
message
);
void
(
*
pirntAllLevelHiLog_
)();
void
(
*
pirntAllLevelHiLog_
)();
void
(
*
pirntRandomHiLog_
)();
void
(
*
pirntRandomHiLog_
)();
...
...
harmonyApp/entry/src/main/cpp/napi_init.cpp
View file @
b9c31bfa
#include "napi/native_api.h"
#include "napi/native_api.h"
#include "libkn_api.h"
static
napi_value
Add
(
napi_env
env
,
napi_callback_info
info
)
static
napi_value
Add
(
napi_env
env
,
napi_callback_info
info
)
{
{
...
@@ -26,11 +27,55 @@ static napi_value Add(napi_env env, napi_callback_info info)
...
@@ -26,11 +27,55 @@ static napi_value Add(napi_env env, napi_callback_info info)
}
}
// pirntSingleHiLog 包装函数
static
napi_value
PirntSingleHiLog
(
napi_env
env
,
napi_callback_info
info
)
{
pirntSingleHiLog
();
napi_value
undefined
;
napi_get_undefined
(
env
,
&
undefined
);
return
undefined
;
}
// pirntAllLevelHiLog 包装函数
static
napi_value
PirntAllLevelHiLog
(
napi_env
env
,
napi_callback_info
info
)
{
pirntAllLevelHiLog
();
napi_value
undefined
;
napi_get_undefined
(
env
,
&
undefined
);
return
undefined
;
}
// pirntRandomHiLog 包装函数
static
napi_value
PirntRandomHiLog
(
napi_env
env
,
napi_callback_info
info
)
{
pirntRandomHiLog
();
napi_value
undefined
;
napi_get_undefined
(
env
,
&
undefined
);
return
undefined
;
}
// FaultOut 包装函数
static
napi_value
FaultOut
(
napi_env
env
,
napi_callback_info
info
)
{
FaultOut
();
napi_value
undefined
;
napi_get_undefined
(
env
,
&
undefined
);
return
undefined
;
}
EXTERN_C_START
EXTERN_C_START
static
napi_value
Init
(
napi_env
env
,
napi_value
exports
)
static
napi_value
Init
(
napi_env
env
,
napi_value
exports
)
{
{
napi_property_descriptor
desc
[]
=
{
napi_property_descriptor
desc
[]
=
{
{
"add"
,
nullptr
,
Add
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
}
{
"add"
,
nullptr
,
Add
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"pirntSingleHiLog"
,
nullptr
,
PirntSingleHiLog
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"pirntAllLevelHiLog"
,
nullptr
,
PirntAllLevelHiLog
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"pirntRandomHiLog"
,
nullptr
,
PirntRandomHiLog
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"FaultOut"
,
nullptr
,
FaultOut
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
}
};
};
napi_define_properties
(
env
,
exports
,
sizeof
(
desc
)
/
sizeof
(
desc
[
0
]),
desc
);
napi_define_properties
(
env
,
exports
,
sizeof
(
desc
)
/
sizeof
(
desc
[
0
]),
desc
);
return
exports
;
return
exports
;
...
...
harmonyApp/entry/src/main/cpp/types/libentry/Index.d.ts
View file @
b9c31bfa
export
const
add
:
(
a
:
number
,
b
:
number
)
=>
number
;
export
interface
testNapi
{
\ No newline at end of file
add
:
(
value1
:
number
,
value2
:
number
)
=>
number
;
pirntSingleHiLog
:
()
=>
void
;
pirntAllLevelHiLog
:
()
=>
void
;
pirntRandomHiLog
:
()
=>
void
;
FaultOut
:
()
=>
void
;
}
declare
const
testNapi
:
testNapi
;
export
default
testNapi
;
\ No newline at end of file
harmonyApp/entry/src/main/ets/pages/Index.ets
View file @
b9c31bfa
...
@@ -9,18 +9,55 @@ struct Index {
...
@@ -9,18 +9,55 @@ struct Index {
@State message: string = 'Hello World';
@State message: string = 'Hello World';
build() {
build() {
Row() {
Column() {
Column() {
// 标题
Text(this.message)
Text('日志测试模块')
.fontSize($r('app.float.page_text_font_size'))
.id('LogTitle')
.fontWeight(FontWeight.Bold)
.fontSize(24)
.onClick(() => {
.fontWeight(FontWeight.Bold)
this.message = 'Welcome';
.margin({ top: 50, bottom: 30 })
hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
})
// 单次日志测试按钮
}
Button('单次日志测试')
.width('100%')
.width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntSingleHiLog();
})
// 一次打印所有层级日志按钮
Button('一次打印所有层级日志')
.width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntAllLevelHiLog();
})
// 大量持续日志输出按钮
Button('大量持续日志输出')
.width('80%')
.height(50)
.backgroundColor('#007DFF')
.margin({ bottom: 20 })
.onClick(() => {
testNapi.pirntRandomHiLog();
})
// 越界崩溃测试按钮
Button('越界崩溃测试')
.width('80%')
.height(50)
.backgroundColor('#FF0000')
.onClick(() => {
testNapi.FaultOut();
})
}
}
.width('100%')
.height('100%')
.height('100%')
}
}
}
}
shared/src/ohosArm64Main/kotlin/com/dong/nocomposedemo/TestLog.ohosArm64.kt
View file @
b9c31bfa
...
@@ -52,7 +52,15 @@ actual fun pirntAllLevelHiLog(){
...
@@ -52,7 +52,15 @@ actual fun pirntAllLevelHiLog(){
@OptIn
(
ExperimentalNativeApi
::
class
)
@OptIn
(
ExperimentalNativeApi
::
class
)
@CName
(
"pirntRandomHiLog"
)
@CName
(
"pirntRandomHiLog"
)
actual
fun
pirntRandomHiLog
()
{
actual
fun
pirntRandomHiLog
()
{
val
levels
=
listOf
(
3
u
,
4
u
,
5
u
,
6
u
,
7
u
)
// DEBUG, INFO, WARN, ERROR, FATAL
val
randomLevel
=
levels
.
random
()
OH_LOG_PrintMsg
(
0
u
,
randomLevel
,
4660
u
,
"TestTag"
,
"随机日志:级别=$randomLevel"
)
}
}
@OptIn
(
ExperimentalNativeApi
::
class
)
@CName
(
"FaultOut"
)
actual
fun
FaultOut
()
{
actual
fun
FaultOut
()
{
val
array
=
intArrayOf
(
1
,
2
,
3
)
// 故意访问越界索引,触发崩溃
println
(
array
[
5
])
}
}
\ No newline at end of file
shared/src/ohosX64Main/kotlin/com/dong/nocomposedemo/TestLog.ohosX64.kt
View file @
b9c31bfa
...
@@ -52,8 +52,19 @@ actual fun pirntAllLevelHiLog(){
...
@@ -52,8 +52,19 @@ actual fun pirntAllLevelHiLog(){
@OptIn
(
ExperimentalNativeApi
::
class
)
@OptIn
(
ExperimentalNativeApi
::
class
)
@CName
(
"pirntRandomHiLog"
)
@CName
(
"pirntRandomHiLog"
)
actual
fun
pirntRandomHiLog
()
{
actual
fun
pirntRandomHiLog
()
{
val
levels
=
listOf
(
3
u
,
4
u
,
5
u
,
6
u
,
7
u
)
// DEBUG, INFO, WARN, ERROR, FATAL
val
randomLevel
=
levels
.
random
()
OH_LOG_PrintMsg
(
0
u
,
randomLevel
,
4660
u
,
"TestTag"
,
"随机日志:级别=$randomLevel"
)
}
}
@OptIn
(
ExperimentalNativeApi
::
class
)
@CName
(
"FaultOut"
)
actual
fun
FaultOut
()
{
actual
fun
FaultOut
()
{
val
array
=
intArrayOf
(
1
,
2
,
3
)
// 故意访问越界索引,触发崩溃
println
(
array
[
5
])
// 备用崩溃方式:空指针异常
val
nullStr
:
String
?
=
null
println
(
nullStr
!!
.
length
)
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment