Skip to content

Commit

Permalink
Optimizing compose compiler config
Browse files Browse the repository at this point in the history
  • Loading branch information
syxc committed Jul 9, 2024
1 parent 2a0d0ad commit f5460c8
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 22 deletions.
9 changes: 2 additions & 7 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import java.util.Properties

plugins {
alias(libs.plugins.android.application)
kotlin("android")
alias(libs.plugins.android.application.compose)
alias(libs.plugins.compose.compiler)
kotlin("android")
}

android {
Expand Down Expand Up @@ -65,11 +65,6 @@ android {
}
}

composeCompiler {
enableStrongSkippingMode = true
reportsDestination = layout.buildDirectory.dir("compose_compiler")
}

dependencies {
implementation(projects.shared)
// implementation(projects.sharedCompose)
Expand Down
16 changes: 13 additions & 3 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ dependencies {
compileOnly(libs.gradlePlugin.kotlin) {
exclude(group = "org.ow2.asm")
}
implementation(libs.bundles.asm)
compileOnly(libs.bundles.asm)
implementation(libs.gradlePlugin.spotless)
implementation(libs.gradlePlugin.compose.compiler)
}

gradlePlugin {
plugins {
val prefix = "com.jithub.gradle"
register("build-logic") {
register("buildLogic") {
id = "$prefix.build-logic"
implementationClass = "BuildLogic"
}
register("build-support") {
register("buildSupport") {
id = "$prefix.build-support"
implementationClass = "BuildSupportPlugin"
}

register("androidApplicationCompose") {
id = "com.android.application.compose"
implementationClass = "AndroidApplicationComposeConventionPlugin"
}
register("androidLibraryCompose") {
id = "com.android.library.compose"
implementationClass = "AndroidLibraryComposeConventionPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType

/**
* Android application compose convention plugin
*
* If compose is required, then:
* ```
* plugins {
* id("com.android.application.compose")
* }
* ```
*
* If not required:
* ```
* plugins {
* id("com.android.application")
* }
* ```
*
* @constructor Create empty Android application compose convention plugin
*/
class AndroidApplicationComposeConventionPlugin : BasePlugin() {
override fun apply(target: Project) {
log("apply target: ${target.displayName}")
with(target) {
if (!plugins.hasPlugin("com.android.application")) {
pluginManager.apply("com.android.application")
}
extensions.getByType<BaseAppModuleExtension>().apply {
configureAndroidCompose(this)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import com.android.build.gradle.LibraryExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType

/**
* Android library compose convention plugin
*
* If compose is required, then:
* ```
* plugins {
* id("com.android.library.compose")
* }
* ```
*
* If not required:
* ```
* plugins {
* id("com.android.library")
* }
* ```
*
* @constructor Create empty Android library compose convention plugin
*/
class AndroidLibraryComposeConventionPlugin : BasePlugin() {
override fun apply(target: Project) {
log("apply target: ${target.displayName}")
with(target) {
if (!plugins.hasPlugin("com.android.library")) {
pluginManager.apply("com.android.library")
}
extensions.getByType<LibraryExtension>().apply {
configureAndroidCompose(this)
}
}
}
}
9 changes: 0 additions & 9 deletions build-logic/convention/src/main/kotlin/BuildSupportPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,4 @@ class BuildSupportPlugin : BasePlugin() {
}
}
}

private fun lintConfigure(): Lint.() -> Unit = {
abortOnError = true
warningsAsErrors = false
ignoreTestSources = true
checkDependencies = true
checkReleaseBuilds = false // Full lint runs as part of 'build' task.
htmlReport = true
}
}
46 changes: 46 additions & 0 deletions build-logic/convention/src/main/kotlin/ProjectExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import com.android.build.api.dsl.CommonExtension
import com.android.build.api.dsl.Lint
import org.gradle.api.Project
import org.gradle.api.plugins.PluginContainer
import org.gradle.kotlin.dsl.configure
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension

internal fun Project.lintConfigure(): Lint.() -> Unit = {
abortOnError = true
warningsAsErrors = false
ignoreTestSources = true
checkDependencies = true
checkReleaseBuilds = false // Full lint runs as part of 'build' task.
htmlReport = true
// baseline = rootProject.file("lint-config/lint-baseline.xml")
// lintConfig = rootProject.file("lint-config/lint-default.xml")
}

internal fun PluginContainer.hasKotlinComposePlugin() = hasPlugin("org.jetbrains.kotlin.plugin.compose")

/**
* Configure Compose-specific options
*
* @param commonExtension
* @param hasRedwood Is Redwood used?
*/
fun Project.configureAndroidCompose(
commonExtension: CommonExtension<*, *, *, *, *>,
hasRedwood: Boolean = true
) {
if (!plugins.hasKotlinComposePlugin()) {
pluginManager.apply("org.jetbrains.kotlin.plugin.compose")
}

commonExtension.apply {
if (!hasRedwood) {
// If you are using Redwood, you do not need to set this parameter.
buildFeatures.compose = true
}
}

extensions.configure<ComposeCompilerGradlePluginExtension> {
enableStrongSkippingMode.set(true)
reportsDestination.set(layout.buildDirectory.dir("compose_compiler"))
}
}
10 changes: 8 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ junit = "4.13.2"
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
gradlePlugin-spotless = "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
gradlePlugin-compose-compiler = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }

androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
Expand Down Expand Up @@ -63,13 +64,18 @@ androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref
junit = { group = "junit", name = "junit", version.ref = "junit" }

[plugins]
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

android-application-compose = { id = "com.android.application.compose" }
android-library-compose = { id = "com.android.library.compose" }

android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }

kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-cocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jbr-compose" }

compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jbr-compose" }

[bundles]
asm = []
2 changes: 1 addition & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.cocoapods)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.android.library)
alias(libs.plugins.android.library.compose)
id("dev.icerock.mobile.multiplatform-resources")
}

Expand Down

0 comments on commit f5460c8

Please sign in to comment.