Commit 4ae287cc authored by dsq's avatar dsq

配置鸿蒙目标,未导入原生工程

parent 12d55b6b
......@@ -3,7 +3,5 @@ plugins {
// in each subproject's classloader
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.composeMultiplatform) apply false
alias(libs.plugins.composeCompiler) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
}
\ No newline at end of file
......@@ -4,8 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidApplication)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.composeCompiler)
}
kotlin {
......@@ -17,16 +15,9 @@ kotlin {
sourceSets {
androidMain.dependencies {
implementation(libs.compose.uiToolingPreview)
implementation(libs.androidx.activity.compose)
}
commonMain.dependencies {
implementation(libs.compose.runtime)
implementation(libs.compose.foundation)
implementation(libs.compose.material3)
implementation(libs.compose.ui)
implementation(libs.compose.components.resources)
implementation(libs.compose.uiToolingPreview)
implementation(libs.androidx.lifecycle.viewmodelCompose)
implementation(libs.androidx.lifecycle.runtimeCompose)
implementation(projects.shared)
......
[versions]
agp = "8.11.2"
agp = "8.2.2"
android-compileSdk = "36"
android-minSdk = "24"
android-targetSdk = "36"
......@@ -11,7 +11,7 @@ androidx-lifecycle = "2.9.6"
androidx-testExt = "1.3.0"
composeMultiplatform = "1.10.0"
junit = "4.13.2"
kotlin = "2.3.0"
kotlin = "2.2.21-OH.0.1.0-01"
material3 = "1.10.0-alpha05"
[libraries]
......
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.14.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
......
......@@ -147,23 +147,23 @@ if [ "$PLATFORM" = "ohosArm64" ]; then
if [ "$BUILD_MODE" = "release" ]; then
if [ -n "$LOCAL_OHOS_PATH" ]; then
echo "Using external OHOS path: $LOCAL_OHOS_PATH"
echo "/gradlew :composeApp:publishReleaseBinariesToHarmonyApp"
./gradlew :composeApp:publishReleaseBinariesToHarmonyApp -PharmonyAppPath="$LOCAL_OHOS_PATH" --refresh-dependencies
echo "/gradlew :shared:publishReleaseBinariesToHarmonyApp"
./gradlew :shared:publishReleaseBinariesToHarmonyApp -PharmonyAppPath="$LOCAL_OHOS_PATH" --refresh-dependencies
else
echo "/gradlew :composeApp:publishReleaseBinariesToHarmonyApp"
./gradlew :composeApp:publishReleaseBinariesToHarmonyApp --refresh-dependencies
echo "/gradlew :shared:publishReleaseBinariesToHarmonyApp"
./gradlew :shared:publishReleaseBinariesToHarmonyApp --refresh-dependencies
fi
else
if [ -n "$LOCAL_OHOS_PATH" ]; then
echo "Using external OHOS path: $LOCAL_OHOS_PATH"
./gradlew :composeApp:publishDebugBinariesToHarmonyApp -PharmonyAppPath="$LOCAL_OHOS_PATH" --refresh-dependencies
./gradlew :shared:publishDebugBinariesToHarmonyApp -PharmonyAppPath="$LOCAL_OHOS_PATH" --refresh-dependencies
else
echo "/gradlew :composeApp:publishDebugBinariesToHarmonyApp"
./gradlew :composeApp:publishDebugBinariesToHarmonyApp --refresh-dependencies
echo "/gradlew :shared:publishDebugBinariesToHarmonyApp"
./gradlew :shared:publishDebugBinariesToHarmonyApp --refresh-dependencies
fi
fi
elif [ "$PLATFORM" = "iosSimulatorArm64" ]; then
./gradlew :composeApp:linkDebugFrameworkIosSimulatorArm64 --refresh-dependencies
./gradlew :shared:linkDebugFrameworkIosSimulatorArm64 --refresh-dependencies
else
echo -e "\033[31mError: Unsupported platform '$PLATFORM'\033[0m"
exit 4
......
......@@ -3,14 +3,7 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
pluginManagement {
repositories {
google {
mavenContent {
includeGroupAndSubgroups("androidx")
includeGroupAndSubgroups("com.android")
includeGroupAndSubgroups("com.google")
}
}
mavenCentral()
maven("https://maven.eazytec-cloud.com/nexus/repository/maven-public/")
gradlePluginPortal()
}
}
......@@ -18,15 +11,12 @@ pluginManagement {
dependencyResolutionManagement {
repositories {
google {
mavenContent {
includeGroupAndSubgroups("androidx")
includeGroupAndSubgroups("com.android")
includeGroupAndSubgroups("com.google")
}
maven("https://maven.eazytec-cloud.com/nexus/repository/maven-public/")
gradlePluginPortal()
}
mavenCentral()
}
}
include(":composeApp")
//include(":composeApp")
include(":shared")
\ No newline at end of file
......@@ -22,6 +22,20 @@ kotlin {
}
}
ohosArm64 {
binaries {
sharedLib {
baseName = "kn"
// 链接静态库目录和库名,使用 --whole-archive 强制包含静态库所有符号
freeCompilerArgs += listOf("-Xbinary=sanitizer=address")
}
staticLib {
baseName = "kn"
}
}
}
sourceSets {
commonMain.dependencies {
// put your Multiplatform dependencies here
......@@ -29,9 +43,22 @@ kotlin {
commonTest.dependencies {
implementation(libs.kotlin.test)
}
val commonMain by getting
val ohosArm64Main by getting{
dependsOn(commonMain)
dependencies {
}
}
}
}
android {
namespace = "com.dong.nocomposedemo.shared"
compileSdk = libs.versions.android.compileSdk.get().toInt()
......@@ -43,3 +70,118 @@ android {
minSdk = libs.versions.android.minSdk.get().toInt()
}
}
// commonizer 只认识官方 Konan 目标,不认识 ohos_x64/ohos_arm64,任务可能延迟注册,故在 afterEvaluate 中禁用
// IDE 的 ohosMain 依赖解析会读取 commonizedNativeDistributionLocation.txt,必须在配置阶段就创建,否则 prepareKotlinIdeaImport 报 FileNotFoundException
val commonizedDir = file("build/kotlin")
commonizedDir.mkdirs()
File(commonizedDir, "commonizedNativeDistributionLocation.txt").writeText(commonizedDir.absolutePath)
afterEvaluate {
listOf("commonizeNativeDistribution", "commonizeCInterop").forEach { taskName ->
tasks.findByName(taskName)?.let { it.enabled = false }
}
}
android {
namespace = "com.eazytec.kmpdemo.shared"
compileSdk = libs.versions.android.compileSdk.get().toInt()
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
}
// 使用 Konan 自带的 OHOS LLVM 工具链(用于交叉编译静态库),根据主机平台自动选择
val konanDepsDir: File = file(System.getProperty("user.home") + "/.konan/dependencies")
val isWindows = System.getProperty("os.name").lowercase().contains("win")
val execExt = if (isWindows) ".exe" else ""
val konanOhosLlvmBin: File? by lazy {
val osArch = System.getProperty("os.arch")
val osName = System.getProperty("os.name").lowercase()
val toolchain = when {
osName.contains("mac") && osArch == "aarch64" -> "llvm-19.1.7-aarch64-macos-ohos-2"
osName.contains("mac") -> "llvm-19.1.7-x64-macos-ohos-2"
osName.contains("linux") -> "llvm-19.1.7-x64-linux-ohos-2"
osName.contains("win") -> "llvm-19.1.7-x64-mingw-ohos-2"
else -> null
}
toolchain?.let { konanDepsDir.resolve("$it/bin").takeIf { dir -> dir.isDirectory } }
}
val ohosSysrootArm64: File? by lazy {
listOf("sysroot-ohos-aarch64-6.0.2.640-02", "sysroot-ohos-aarch64-6.0.0.858-02")
.map { konanDepsDir.resolve(it) }.firstOrNull { it.isDirectory }
}
val ohosSysrootX64: File? by lazy {
listOf("sysroot-ohos-x86_64-6.0.0.858-02")
.map { konanDepsDir.resolve(it) }.firstOrNull { it.isDirectory }
}
// Harmony App 输出目录(支持命令行 --harmonyAppPath)
val harmonyAppDir: File = run {
val cliPath = project.findProperty("harmonyAppPath") as String?
if (cliPath.isNullOrBlank()) {
// 默认:项目根目录 /harmonyApp
rootProject.file("harmonyApp")
} else {
// 命令行传入的路径
file(cliPath)
}
}
// 字符串首字母大写工具函数
fun String.capitalizeUS(): String = this.replaceFirstChar {
if (it.isLowerCase()) it.titlecase() else it.toString()
}
// 为不同类型(debug、release)OHOS构建注册Copy任务并发布到Harmony App目录
arrayOf("debug", "release").forEach { type ->
tasks.register<Copy>("publish${type.capitalizeUS()}BinariesToHarmonyApp") {
group = "harmony" // 归类到harmony任务组
dependsOn("link${type.capitalizeUS()}SharedOhosArm64") // 依赖于OHOS shared lib的链接任务
into(harmonyAppDir) // 输出目标目录
from("build/bin/ohosArm64/${type}Shared/libkn_api.h") { // 复制头文件
into("entry/src/main/cpp/include/") // 指定目录
}
from(project.file("build/bin/ohosArm64/${type}Shared/libkn.so")) { // 复制共享库文件
into("/entry/libs/arm64-v8a/") // 指定目标目录
}
}
}
// 发布 ohosX64 产物到 harmonyapp,供模拟器(x86_64)使用
arrayOf("debug", "release").forEach { type ->
val capitalizedType = type.replaceFirstChar { it.uppercase() }
tasks.register<Copy>("publish${capitalizedType}BinariesToHarmonyAppX64") {
group = "harmony"
description = "Publish ohosX64 libkn.so to harmonyapp/entry/libs/x86_64 for emulator"
dependsOn("link${capitalizedType}SharedOhosX64")
into(rootProject.file("harmonyapp"))
val binaryPath = "build/bin/ohosX64/${type}Shared"
from("$binaryPath/libkn_api.h") {
into("entry/src/main/cpp/include/")
}
from("$binaryPath/libkn.so") {
into("entry/libs/x86_64/")
}
from("native-libs/ohosX64/libsample.a") {
into("entry/src/main/cpp/libs/")
}
doFirst {
if (!file(binaryPath).exists()) {
throw GradleException("未找到产物目录: $binaryPath")
}
}
}
}
package com.dong.nocomposedemo
actual fun pirntSingleHiLog() {
}
actual fun pirntAllLevelHiLog() {
}
actual fun pirntRandomHiLog() {
}
actual fun FaultOut() {
}
\ No newline at end of file
package com.dong.nocomposedemo
//打印所有层级日志
expect fun pirntAllLevelHiLog();
//单次打印日志
expect fun pirntSingleHiLog();
//触发崩溃
expect fun FaultOut();
//随机打印日志
expect fun pirntRandomHiLog();
\ No newline at end of file
package com.dong.nocomposedemo
actual fun pirntSingleHiLog() {
}
actual fun pirntAllLevelHiLog() {
}
actual fun pirntRandomHiLog() {
}
actual fun FaultOut() {
}
\ No newline at end of file
......@@ -7,3 +7,11 @@ class IOSPlatform: Platform {
}
actual fun getPlatform(): Platform = IOSPlatform()
actual fun pirntAllLevelHiLog() {
println("[iOS] All Level Hi Log")
}
actual fun pirntSingleHiLog() {
println("[iOS] Single Hi Log")
}
\ No newline at end of file
package com.dong.nocomposedemo
actual fun pirntSingleHiLog() {
}
actual fun pirntAllLevelHiLog() {
}
actual fun FaultOut() {
}
actual fun pirntRandomHiLog() {
}
\ No newline at end of file
package com.dong.nocomposedemo
class OhosPlatform: Platform {
override val name: String = "1"
}
actual fun getPlatform(): Platform {
return OhosPlatform()
}
\ No newline at end of file
package com.dong.nocomposedemo
import platform.PerformanceAnalysisKit.HiLog.OH_LOG_PrintMsg
import kotlin.experimental.ExperimentalNativeApi
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalNativeApi::class)
@CName("hiLogPrintMsg")
fun hiLogPrintMsg(type: UInt, level: UInt, domain: UInt, tag: String, message: String): Int {
return try {
OH_LOG_PrintMsg(type, level, domain, tag, message)
} catch (_: Throwable) {
-1
}
}
@OptIn(ExperimentalNativeApi::class)
@CName("pirntSingleHiLog")
actual fun pirntSingleHiLog(){
OH_LOG_PrintMsg(0u, 3u, 4660u, "TestTag", "单次日志打印");
}
@OptIn(ExperimentalNativeApi::class)
@CName("pirntAllLevelHiLog")
actual fun pirntAllLevelHiLog(){
// 输出 DEBUG 级别
// LOG_APP = 0 (应用日志类型)
// LOG_DEBUG = 3 (调试级别)
// domain = 0x1234 (任意自定义值,范围0x0000-0xFFFF)
// tag = "TestTag" (自定义标签)
OH_LOG_PrintMsg(0u, 3u, 4660u, "TestTag", "This is a DEBUG log from PrintMsg");
// 输出 INFO 级别
// LOG_INFO = 4 (信息级别)
OH_LOG_PrintMsg(0u, 4u, 4660u, "TestTag", "This is an INFO log from PrintMsg");
// 输出 WARN 级别
// LOG_WARN = 5 (警告级别)
OH_LOG_PrintMsg(0u, 5u, 4660u, "TestTag", "This is a WARN log from PrintMsg");
// 输出 ERROR 级别
// LOG_ERROR = 6 (错误级别)
OH_LOG_PrintMsg(0u, 6u, 4660u, "TestTag", "This is an ERROR log from PrintMsg");
// 输出 FATAL 级别
// LOG_FATAL = 7 (致命级别)
OH_LOG_PrintMsg(0u, 7u, 4660u, "TestTag", "This is a FATAL log from PrintMsg");
}
@OptIn(ExperimentalNativeApi::class)
@CName("pirntRandomHiLog")
actual fun pirntRandomHiLog() {
}
actual fun FaultOut() {
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment