Skip to content

Commit

Permalink
[commonize] #496 Create gradle plugin for compose hmpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Jul 28, 2024
1 parent 6b8eb11 commit 57065d2
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 47 additions & 0 deletions buildSrc/src/main/kotlin/ani-compose-hmpp.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
111 changes: 109 additions & 2 deletions buildSrc/src/main/kotlin/ani-mpp-lib-targets.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<KotlinMultiplatformExtension> {
/**
* 平台架构:
* ```
* 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<KotlinMultiplatformExtension> {
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
}
}
}
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/mpp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import org.gradle.api.attributes.Attribute

val AniTarget = Attribute.of("AniTarget", String::class.java)
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57065d2

Please sign in to comment.