Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
testDemo
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
jiangyh
testDemo
Commits
1f4ca7a2
Commit
1f4ca7a2
authored
Dec 25, 2025
by
jiangyh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lib-airth支持harmony端
parent
d84d12ae
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
85 additions
and
19 deletions
+85
-19
build.gradle.kts
composeApp/build.gradle.kts
+8
-0
OhosEntry.kt
...rc/ohosArm64Main/kotlin/com/example/testdemo/OhosEntry.kt
+11
-0
libkn.so
harmonyapp/entry/libs/arm64-v8a/libkn.so
+0
-0
libkn_api.h
harmonyapp/entry/src/main/cpp/include/libkn_api.h
+3
-0
libkn_napi.cpp
harmonyapp/entry/src/main/cpp/libkn_napi.cpp
+27
-3
napi_init.cpp
harmonyapp/entry/src/main/cpp/napi_init.cpp
+30
-12
Index.d.ts
harmonyapp/entry/src/main/cpp/types/libentry/Index.d.ts
+2
-1
libentry.d.ts
harmonyapp/entry/src/main/ets/libentry.d.ts
+1
-0
Index.ets
harmonyapp/entry/src/main/ets/pages/Index.ets
+1
-1
build.gradle.kts
lib-arith/build.gradle.kts
+2
-2
No files found.
composeApp/build.gradle.kts
View file @
1f4ca7a2
...
@@ -43,11 +43,19 @@ kotlin {
...
@@ -43,11 +43,19 @@ kotlin {
dependsOn
(
iosMain
)
dependsOn
(
iosMain
)
}
}
val
ohosArm64Main
by
getting
{
dependsOn
(
commonMain
)
dependencies
{
implementation
(
project
(
":lib-arith"
))
}
}
androidMain
.
dependencies
{
androidMain
.
dependencies
{
implementation
(
libs
.
androidx
.
activity
.
compose
)
implementation
(
libs
.
androidx
.
activity
.
compose
)
implementation
(
project
(
":lib-arith"
))
implementation
(
project
(
":lib-arith"
))
}
}
commonMain
.
dependencies
{
commonMain
.
dependencies
{
// 保持为空:不在 commonMain 引入 lib-arith
}
}
commonTest
.
dependencies
{
commonTest
.
dependencies
{
implementation
(
libs
.
kotlin
.
test
)
implementation
(
libs
.
kotlin
.
test
)
...
...
composeApp/src/ohosArm64Main/kotlin/com/example/testdemo/OhosEntry.kt
View file @
1f4ca7a2
...
@@ -4,9 +4,20 @@ package com.example.testdemo
...
@@ -4,9 +4,20 @@ package com.example.testdemo
import
kotlinx.cinterop.ExperimentalForeignApi
import
kotlinx.cinterop.ExperimentalForeignApi
import
kotlin.experimental.ExperimentalNativeApi
import
kotlin.experimental.ExperimentalNativeApi
import
com.example.arith.Arith
// 既然去掉了 Compose,我们先通过 NAPI 返回一个简单的标识,证明 Kotlin 代码已成功运行
// 既然去掉了 Compose,我们先通过 NAPI 返回一个简单的标识,证明 Kotlin 代码已成功运行
@CName
(
"getKotlinGreeting"
)
@CName
(
"getKotlinGreeting"
)
fun
getKotlinGreeting
():
String
{
fun
getKotlinGreeting
():
String
{
return
Greeting
().
greet
()
+
" (HarmonyOS Native)"
return
Greeting
().
greet
()
+
" (HarmonyOS Native)"
}
}
@CName
(
"getKotlinUiText"
)
fun
getKotlinUiText
():
String
=
ohosUiText
()
fun
ohosUiText
():
String
{
val
base
=
appInfo
()
val
add
=
Arith
.
add
(
7
,
3
)
val
sub
=
Arith
.
sub
(
7
,
3
)
return
"$base\nAdd is $add, Sub is $sub"
}
harmonyapp/entry/libs/arm64-v8a/libkn.so
View file @
1f4ca7a2
No preview for this file type
harmonyapp/entry/src/main/cpp/include/libkn_api.h
View file @
1f4ca7a2
...
@@ -74,6 +74,7 @@ typedef struct {
...
@@ -74,6 +74,7 @@ typedef struct {
}
libkn_kref_com_example_testdemo_OhosPlatform
;
}
libkn_kref_com_example_testdemo_OhosPlatform
;
extern
const
char
*
getKotlinGreeting
();
extern
const
char
*
getKotlinGreeting
();
extern
const
char
*
getKotlinUiText
();
typedef
struct
{
typedef
struct
{
/* Service functions. */
/* Service functions. */
...
@@ -128,6 +129,8 @@ typedef struct {
...
@@ -128,6 +129,8 @@ typedef struct {
}
OhosPlatform
;
}
OhosPlatform
;
const
char
*
(
*
appInfo
)();
const
char
*
(
*
appInfo
)();
const
char
*
(
*
getKotlinGreeting_
)();
const
char
*
(
*
getKotlinGreeting_
)();
const
char
*
(
*
getKotlinUiText_
)();
const
char
*
(
*
ohosUiText
)();
libkn_kref_com_example_testdemo_Platform
(
*
getPlatform
)();
libkn_kref_com_example_testdemo_Platform
(
*
getPlatform
)();
}
testdemo
;
}
testdemo
;
}
example
;
}
example
;
...
...
harmonyapp/entry/src/main/cpp/libkn_napi.cpp
View file @
1f4ca7a2
...
@@ -3,10 +3,13 @@
...
@@ -3,10 +3,13 @@
static
void
*
g_lib_handle
=
nullptr
;
static
void
*
g_lib_handle
=
nullptr
;
typedef
const
char
*
(
*
get_kotlin_greeting_t
)();
typedef
const
char
*
(
*
get_kotlin_greeting_t
)();
static
get_kotlin_greeting_t
g_get_kotlin_greeting
=
nullptr
;
static
get_kotlin_greeting_t
g_get_kotlin_greeting
=
nullptr
;
typedef
const
char
*
(
*
get_kotlin_ui_text_t
)();
static
get_kotlin_ui_text_t
g_get_kotlin_ui_text
=
nullptr
;
static
bool
LoadLibKn
()
{
static
bool
LoadLibKn
()
{
if
(
g_get_kotlin_greeting
)
return
true
;
if
(
g_get_kotlin_greeting
&&
g_get_kotlin_ui_text
)
return
true
;
if
(
!
g_lib_handle
)
{
if
(
!
g_lib_handle
)
{
g_lib_handle
=
dlopen
(
"libkn.so"
,
RTLD_LAZY
);
g_lib_handle
=
dlopen
(
"libkn.so"
,
RTLD_LAZY
);
if
(
!
g_lib_handle
)
{
if
(
!
g_lib_handle
)
{
...
@@ -16,7 +19,10 @@ static bool LoadLibKn() {
...
@@ -16,7 +19,10 @@ static bool LoadLibKn() {
g_get_kotlin_greeting
=
reinterpret_cast
<
get_kotlin_greeting_t
>
(
g_get_kotlin_greeting
=
reinterpret_cast
<
get_kotlin_greeting_t
>
(
dlsym
(
g_lib_handle
,
"getKotlinGreeting"
)
dlsym
(
g_lib_handle
,
"getKotlinGreeting"
)
);
);
return
g_get_kotlin_greeting
!=
nullptr
;
g_get_kotlin_ui_text
=
reinterpret_cast
<
get_kotlin_ui_text_t
>
(
dlsym
(
g_lib_handle
,
"getKotlinUiText"
)
);
return
g_get_kotlin_greeting
!=
nullptr
&&
g_get_kotlin_ui_text
!=
nullptr
;
}
}
static
napi_value
GetKotlinGreeting
(
napi_env
env
,
napi_callback_info
/*info*/
)
{
static
napi_value
GetKotlinGreeting
(
napi_env
env
,
napi_callback_info
/*info*/
)
{
...
@@ -34,11 +40,29 @@ static napi_value GetKotlinGreeting(napi_env env, napi_callback_info /*info*/) {
...
@@ -34,11 +40,29 @@ static napi_value GetKotlinGreeting(napi_env env, napi_callback_info /*info*/) {
return
out
;
return
out
;
}
}
static
napi_value
GetKotlinUiText
(
napi_env
env
,
napi_callback_info
/*info*/
)
{
if
(
!
LoadLibKn
())
{
napi_throw_error
(
env
,
nullptr
,
"dlopen/dlsym libkn.so|getKotlinUiText 失败"
);
return
nullptr
;
}
const
char
*
s
=
g_get_kotlin_ui_text
();
if
(
!
s
)
{
napi_throw_error
(
env
,
nullptr
,
"getKotlinUiText 返回空指针"
);
return
nullptr
;
}
napi_value
out
;
napi_create_string_utf8
(
env
,
s
,
NAPI_AUTO_LENGTH
,
&
out
);
return
out
;
}
static
napi_value
Init
(
napi_env
env
,
napi_value
exports
)
{
static
napi_value
Init
(
napi_env
env
,
napi_value
exports
)
{
// 仅导出 getKotlinGreeting
// 仅导出 getKotlinGreeting
napi_value
fn
;
napi_value
fn
;
napi_create_function
(
env
,
"getKotlinGreeting"
,
NAPI_AUTO_LENGTH
,
GetKotlinGreeting
,
nullptr
,
&
fn
);
napi_create_function
(
env
,
"getKotlinGreeting"
,
NAPI_AUTO_LENGTH
,
GetKotlinGreeting
,
nullptr
,
&
fn
);
napi_set_named_property
(
env
,
exports
,
"getKotlinGreeting"
,
fn
);
napi_set_named_property
(
env
,
exports
,
"getKotlinGreeting"
,
fn
);
napi_value
fn2
;
napi_create_function
(
env
,
"getKotlinUiText"
,
NAPI_AUTO_LENGTH
,
GetKotlinUiText
,
nullptr
,
&
fn2
);
napi_set_named_property
(
env
,
exports
,
"getKotlinUiText"
,
fn2
);
return
exports
;
return
exports
;
}
}
...
...
harmonyapp/entry/src/main/cpp/napi_init.cpp
View file @
1f4ca7a2
...
@@ -30,25 +30,32 @@ static napi_value Add(napi_env env, napi_callback_info info)
...
@@ -30,25 +30,32 @@ static napi_value Add(napi_env env, napi_callback_info info)
static
void
*
g_lib_handle
=
nullptr
;
static
void
*
g_lib_handle
=
nullptr
;
typedef
const
char
*
(
*
get_kotlin_greeting_t
)();
typedef
const
char
*
(
*
get_kotlin_greeting_t
)();
static
get_kotlin_greeting_t
g_get_kotlin_greeting
=
nullptr
;
static
get_kotlin_greeting_t
g_get_kotlin_greeting
=
nullptr
;
typedef
const
char
*
(
*
get_kotlin_ui_text_t
)();
static
get_kotlin_ui_text_t
g_get_kotlin_ui_text
=
nullptr
;
static
bool
LoadLibKn
()
{
static
bool
LoadLibKn
()
{
if
(
g_get_kotlin_greeting
)
return
true
;
if
(
!
g_lib_handle
)
{
if
(
!
g_lib_handle
)
{
g_lib_handle
=
dlopen
(
"libkn.so"
,
RTLD_LAZY
);
g_lib_handle
=
dlopen
(
"libkn.so"
,
RTLD_LAZY
);
if
(
!
g_lib_handle
)
{
if
(
!
g_lib_handle
)
{
//
如需调试可输出 dlerror()
//
可选:printf("%s\n", dlerror());
return
false
;
return
false
;
}
}
}
}
// 必须与 Kotlin 的 @CName("getKotlinGreeting") 完全一致
if
(
!
g_get_kotlin_greeting
)
{
g_get_kotlin_greeting
=
reinterpret_cast
<
get_kotlin_greeting_t
>
(
g_get_kotlin_greeting
=
reinterpret_cast
<
get_kotlin_greeting_t
>
(
dlsym
(
g_lib_handle
,
"getKotlinGreeting"
)
dlsym
(
g_lib_handle
,
"getKotlinGreeting"
)
);
);
return
g_get_kotlin_greeting
!=
nullptr
;
}
if
(
!
g_get_kotlin_ui_text
)
{
g_get_kotlin_ui_text
=
reinterpret_cast
<
get_kotlin_ui_text_t
>
(
dlsym
(
g_lib_handle
,
"getKotlinUiText"
)
);
}
return
true
;
}
}
static
napi_value
NAPI_Global_getKotlinGreeting
(
napi_env
env
,
napi_callback_info
info
)
{
static
napi_value
NAPI_Global_getKotlinGreeting
(
napi_env
env
,
napi_callback_info
/*info*/
)
{
if
(
!
LoadLibKn
())
{
if
(
!
LoadLibKn
()
||
!
g_get_kotlin_greeting
)
{
napi_throw_error
(
env
,
nullptr
,
"dlopen/dlsym libkn.so|getKotlinGreeting 失败"
);
napi_throw_error
(
env
,
nullptr
,
"dlopen/dlsym libkn.so|getKotlinGreeting 失败"
);
return
nullptr
;
return
nullptr
;
}
}
...
@@ -61,15 +68,26 @@ static napi_value NAPI_Global_getKotlinGreeting(napi_env env, napi_callback_info
...
@@ -61,15 +68,26 @@ static napi_value NAPI_Global_getKotlinGreeting(napi_env env, napi_callback_info
napi_create_string_utf8
(
env
,
s
,
NAPI_AUTO_LENGTH
,
&
out
);
napi_create_string_utf8
(
env
,
s
,
NAPI_AUTO_LENGTH
,
&
out
);
return
out
;
return
out
;
}
}
static
napi_value
NAPI_Global_getKotlinGreeting1
(
napi_env
env
,
napi_callback_info
info
)
{
static
napi_value
NAPI_Global_getKotlinUiText
(
napi_env
env
,
napi_callback_info
/*info*/
)
{
// TODO: implements the code;
if
(
!
LoadLibKn
()
||
!
g_get_kotlin_ui_text
)
{
napi_throw_error
(
env
,
nullptr
,
"dlopen/dlsym libkn.so|getKotlinUiText 失败"
);
return
nullptr
;
}
const
char
*
s
=
g_get_kotlin_ui_text
();
if
(
!
s
)
{
napi_throw_error
(
env
,
nullptr
,
"getKotlinUiText 返回空指针"
);
return
nullptr
;
}
napi_value
out
;
napi_create_string_utf8
(
env
,
s
,
NAPI_AUTO_LENGTH
,
&
out
);
return
out
;
}
}
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
},
{
"getKotlinGreeting"
,
nullptr
,
NAPI_Global_getKotlinGreeting
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"getKotlinGreeting"
,
nullptr
,
NAPI_Global_getKotlinGreeting
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
},
{
"getKotlin
Greeting"
,
nullptr
,
NAPI_Global_getKotlinGreeting1
,
nullptr
,
nullptr
,
nullptr
,
napi_default
,
nullptr
}
{
"getKotlin
UiText"
,
nullptr
,
NAPI_Global_getKotlinUiText
,
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 @
1f4ca7a2
export
const
getKotlinGreeting
:
()
=>
string
;
export
const
getKotlinGreeting
:
()
=>
string
;
\ No newline at end of file
export
const
getKotlinUiText
:
()
=>
string
;
\ No newline at end of file
harmonyapp/entry/src/main/ets/libentry.d.ts
View file @
1f4ca7a2
declare
module
'libentry.so'
{
declare
module
'libentry.so'
{
export
const
getKotlinGreeting
:
()
=>
string
;
export
const
getKotlinGreeting
:
()
=>
string
;
export
const
getKotlinUiText
:
()
=>
string
;
}
}
\ No newline at end of file
harmonyapp/entry/src/main/ets/pages/Index.ets
View file @
1f4ca7a2
...
@@ -36,7 +36,7 @@ struct Index {
...
@@ -36,7 +36,7 @@ struct Index {
refreshGreeting() {
refreshGreeting() {
try {
try {
this.kotlinMsg = testNapi.getKotlin
Greeting
();
this.kotlinMsg = testNapi.getKotlin
UiText
();
} catch (e) {
} catch (e) {
this.kotlinMsg = "调用失败: " + (e?.message ?? String(e));
this.kotlinMsg = "调用失败: " + (e?.message ?? String(e));
}
}
...
...
lib-arith/build.gradle.kts
View file @
1f4ca7a2
...
@@ -8,7 +8,7 @@ kotlin {
...
@@ -8,7 +8,7 @@ kotlin {
iosArm64
()
iosArm64
()
iosSimulatorArm64
()
iosSimulatorArm64
()
iosX64
()
iosX64
()
ohosArm64
()
sourceSets
{
sourceSets
{
val
commonMain
by
getting
val
commonMain
by
getting
...
@@ -17,7 +17,7 @@ kotlin {
...
@@ -17,7 +17,7 @@ kotlin {
val
iosArm64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
iosArm64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
iosSimulatorArm64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
iosSimulatorArm64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
iosX64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
iosX64Main
by
getting
{
dependsOn
(
iosMain
)
}
val
ohosArm64Main
by
getting
{
dependsOn
(
commonMain
)
}
}
}
}
}
...
...
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