diff --git a/build.gradle.kts b/build.gradle.kts index 55723c3a57..204fed68f1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,7 +30,7 @@ plugins { kotlin("android") apply false kotlin("jvm") apply false kotlin("plugin.serialization") version libs.versions.kotlin apply false - kotlin("plugin.compose") version libs.versions.kotlin apply false + kotlin("plugin.compose") apply false id("org.jetbrains.kotlinx.atomicfu") apply false id("org.jetbrains.compose") apply false id("com.android.library") apply false diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 6b9690f5c7..41604b40c1 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -54,6 +54,7 @@ dependencies { api(libs.android.application.gradle.plugin) api(libs.android.library.gradle.plugin) api(libs.compose.multiplatfrom.gradle.plugin) + api(libs.kotlin.compose.compiler.gradle.plugin) implementation(kotlin("script-runtime")) implementation(libs.snakeyaml) diff --git a/buildSrc/src/main/kotlin/ani-compose-hmpp.gradle.kts b/buildSrc/src/main/kotlin/ani-compose-hmpp.gradle.kts new file mode 100644 index 0000000000..713863cd1a --- /dev/null +++ b/buildSrc/src/main/kotlin/ani-compose-hmpp.gradle.kts @@ -0,0 +1,47 @@ +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.dsl.kotlinExtension + +/* + * 为 Compose 配置 JVM + Android 的 HMPP 源集结构 + */ + +(kotlinExtension as? KotlinMultiplatformExtension)?.run { + androidTarget { + attributes.attribute(AniTarget, "android") + } + jvm("desktop") + + sourceSets { + val commonMain by getting { + + } + + val jvmMain by creating { + dependsOn(commonMain) + } + + androidMain { + dependsOn(jvmMain) + } + + val desktopMain by getting { + dependsOn(jvmMain) + } + + val nativeMain by creating { + dependsOn(commonMain) + } + + val appleMain by creating { + dependsOn(nativeMain) + } + + val iosMain by creating { + dependsOn(appleMain) + } + + val iosArm64Main by getting { + dependsOn(iosMain) + } + } +} diff --git a/buildSrc/src/main/kotlin/ani-mpp-lib-targets.gradle.kts b/buildSrc/src/main/kotlin/ani-mpp-lib-targets.gradle.kts index af81692425..c93818c15c 100644 --- a/buildSrc/src/main/kotlin/ani-mpp-lib-targets.gradle.kts +++ b/buildSrc/src/main/kotlin/ani-mpp-lib-targets.gradle.kts @@ -1,7 +1,114 @@ +import com.android.build.api.dsl.LibraryExtension +import org.jetbrains.compose.ComposeExtension +import org.jetbrains.compose.ComposePlugin import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.kotlinExtension +import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask -(kotlinExtension as? KotlinMultiplatformExtension)?.run { - jvm() +/* + * 配置 JVM + Android 的 compose 项目. 默认不会配置 resources. + * + * 该插件必须在 kotlin, compose, android 之后引入. + * + * 如果开了 android, 就会配置 desktop + android, 否则只配置 jvm. + */ + +extra.set("ani.jvm.target", 17) + +val android = extensions.findByType(LibraryExtension::class) +val composeExtension = extensions.findByType(ComposeExtension::class) + +configure { + /** + * 平台架构: + * ``` + * common + * - jvm (可访问 JDK, 但不能使用 Android SDK 没有的 API) + * - android (可访问 Android SDK) + * - desktop (可访问 JDK) + * - native + * - apple + * - ios + * - iosArm64 + * - iosSimulatorArm64 TODO + * ``` + * + * `native - apple - ios` 的架构是为了契合 Kotlin 官方推荐的默认架构. 以后如果万一要添加其他平台, 可方便添加. + */ + if (android != null) { + jvm("desktop") + androidTarget { + attributes.attribute(AniTarget, "android") + } + } else { + jvm() + } iosArm64() + + compilerOptions { + freeCompilerArgs.add("-Xexpect-actual-classes") + } + + sourceSets.commonMain.dependencies { + // 添加常用依赖 + if (composeExtension != null) { + val compose = ComposePlugin.Dependencies(project) + // Compose + api(compose.foundation) + api(compose.animation) + api(compose.ui) + api(compose.material3) + api(compose.materialIconsExtended) + api(compose.runtime) + } + + if (project.path != ":utils:platform") { + implementation(project(":utils:platform")) + } + } +} + +extensions.findByType(org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension::class)?.apply { + enableStrongSkippingMode = true +} + +if (android != null) { + configure { + sourceSets { + // Workaround for MPP compose bug, don't change + removeIf { it.name == "androidAndroidTestRelease" } + removeIf { it.name == "androidTestFixtures" } + removeIf { it.name == "androidTestFixturesDebug" } + removeIf { it.name == "androidTestFixturesRelease" } + } + } + tasks.named("generateComposeResClass") { + dependsOn("generateResourceAccessorsForAndroidUnitTest") + } + tasks.withType(KotlinCompilationTask::class) { + dependsOn("generateComposeResClass") + dependsOn("generateResourceAccessorsForAndroidRelease") + dependsOn("generateResourceAccessorsForAndroidUnitTest") + dependsOn("generateResourceAccessorsForAndroidUnitTestRelease") + dependsOn("generateResourceAccessorsForAndroidUnitTestDebug") + dependsOn("generateResourceAccessorsForAndroidDebug") + } + + android.apply { + compileSdk = getIntProperty("android.compile.sdk") + defaultConfig { + minSdk = getIntProperty("android.min.sdk") + } + buildTypes.getByName("release") { + isMinifyEnabled = false // shared 不能 minify, 否则构建 app 会失败 + isShrinkResources = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + *sharedAndroidProguardRules(), + ) + } + buildFeatures { + compose = true + } + } } diff --git a/buildSrc/src/main/kotlin/mpp.kt b/buildSrc/src/main/kotlin/mpp.kt new file mode 100644 index 0000000000..d5e2fbf055 --- /dev/null +++ b/buildSrc/src/main/kotlin/mpp.kt @@ -0,0 +1,3 @@ +import org.gradle.api.attributes.Attribute + +val AniTarget = Attribute.of("AniTarget", String::class.java) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ed6dfc1125..d775202c21 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -57,6 +57,7 @@ android-application-gradle-plugin = { module = "com.android.application:com.andr kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } atomicfu-gradle-plugin = { module = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "atomicfu" } compose-multiplatfrom-gradle-plugin = { module = "org.jetbrains.compose:org.jetbrains.compose.gradle.plugin", version.ref = "compose-multiplatform" } +kotlin-compose-compiler-gradle-plugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" } #compose-multiplatfrom-compiler-plugin = { module = "org.jetbrains.compose:org.jetbrains.compose.compiler", version.ref = "compose-multiplatform-compiler" } # Kotlinx