From 6eb4b3af7cd7a4a44341451c063bba5c650d057f Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Thu, 23 May 2024 22:51:23 -0400 Subject: [PATCH 1/3] Applying some things learned from NowInAndroid to the project to see if we can get compileOnly dependencies + compose compiler gradle plugin working in build-logic --- build-logic/gradle.properties | 4 ++ build-logic/plugins/build.gradle.kts | 17 +++------ .../main/kotlin/com/soberg/gradle/Versions.kt | 2 +- .../plugin/AndroidComposeConventionPlugin.kt | 38 ++++++++----------- .../soberg/gradle/plugin/RootProjectPlugin.kt | 12 +++--- .../plugin/ext/AndroidProjectExtensions.kt | 6 +++ .../gradle/plugin/ext/ProjectExtensions.kt | 9 +++-- build-logic/settings.gradle.kts | 10 ++--- build.gradle.kts | 4 +- gradle.properties | 5 ++- gradle/libs.versions.toml | 6 +-- settings.gradle.kts | 17 +++++++-- 12 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 build-logic/gradle.properties diff --git a/build-logic/gradle.properties b/build-logic/gradle.properties new file mode 100644 index 0000000..907425d --- /dev/null +++ b/build-logic/gradle.properties @@ -0,0 +1,4 @@ +# Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534 +org.gradle.parallel=true +org.gradle.caching=true +org.gradle.configureondemand=false \ No newline at end of file diff --git a/build-logic/plugins/build.gradle.kts b/build-logic/plugins/build.gradle.kts index ec997cf..7d7c164 100644 --- a/build-logic/plugins/build.gradle.kts +++ b/build-logic/plugins/build.gradle.kts @@ -2,6 +2,12 @@ plugins { `kotlin-dsl` } +dependencies { + compileOnly(libs.android.gradlePlugin) + compileOnly(libs.composeCompiler.gradlePlugin) + compileOnly(libs.kotlin.gradlePlugin) +} + gradlePlugin { plugins { register("local.android.compose") { @@ -29,15 +35,4 @@ gradlePlugin { implementationClass = "com.soberg.gradle.plugin.RootSettingsPlugin" } } -} - -dependencies { - // Give plugin code access to the libs version catalog. - implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) - - implementation(libs.android.gradlePlugin) - implementation(libs.kotlin.gradlePlugin) - - // TODO: Fixes dependency conflict issues with Dagger/Hilt, re: https://github.com/google/dagger/issues/3068. Once this issues is resolved and we upgrade dagger/hilt, remove this dependency. - implementation(libs.javapoet) } \ No newline at end of file diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/Versions.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/Versions.kt index 7275d06..cae8c87 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/Versions.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/Versions.kt @@ -22,6 +22,6 @@ object Versions { // See https://kotlinlang.org/docs/gradle-compiler-options.html#types-for-compiler-options object Kotlin { val jvmTarget = JvmTarget.JVM_17 - val languageVersion = KotlinVersion.KOTLIN_1_9 + val languageVersion = KotlinVersion.KOTLIN_2_0 } } \ No newline at end of file diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt index 5bb92f6..5acf9fb 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt @@ -2,24 +2,21 @@ package com.soberg.gradle.plugin import com.soberg.gradle.plugin.ext.android import com.soberg.gradle.plugin.ext.androidTestImplementation +import com.soberg.gradle.plugin.ext.composeCompiler import com.soberg.gradle.plugin.ext.implementation import com.soberg.gradle.plugin.ext.libs +import com.soberg.gradle.plugin.ext.plugins import com.soberg.gradle.plugin.ext.testImplementation import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.withType -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - class AndroidComposeConventionPlugin : Plugin { - private companion object { - private const val ComposeCompilerPluginPrepend = - "plugin:androidx.compose.compiler.plugins.kotlin" - } - override fun apply(project: Project) = with(project) { + plugins { + apply("org.jetbrains.kotlin.plugin.compose") + } configureCompose() configureComposeMetrics() } @@ -27,11 +24,10 @@ class AndroidComposeConventionPlugin : Plugin { private fun Project.configureCompose() { android { buildFeatures.compose = true - composeOptions.kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get() } dependencies { - val bom = platform(libs.compose.bom) + val bom = platform(libs.findLibrary("compose-bom").get()) implementation(bom) androidTestImplementation(bom) testImplementation(bom) @@ -39,19 +35,15 @@ class AndroidComposeConventionPlugin : Plugin { } private fun Project.configureComposeMetrics() { - val composeMetricsDir = layout.buildDirectory.dir("compose-metrics").get().asFile - val composeReportsDir = layout.buildDirectory.dir("compose-reports").get().asFile - - tasks.withType().configureEach { - kotlinOptions { - freeCompilerArgs = freeCompilerArgs + listOf( - "-P", - "$ComposeCompilerPluginPrepend:metricsDestination=${composeMetricsDir.absolutePath}", - - "-P", - "$ComposeCompilerPluginPrepend:reportsDestination=${composeReportsDir.absolutePath}", - ) - } + composeCompiler { + enableStrongSkippingMode.set(true) + + stabilityConfigurationFile.set( + layout.buildDirectory.dir("compose-metrics").get().asFile + ) + reportsDestination.set( + layout.buildDirectory.dir("compose-reports").get().asFile + ) } } } \ No newline at end of file diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/RootProjectPlugin.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/RootProjectPlugin.kt index ea2a7f8..629eb6e 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/RootProjectPlugin.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/RootProjectPlugin.kt @@ -21,12 +21,6 @@ class RootProjectPlugin : Plugin { configureSubProjects() } - private fun Project.applyTestOptions() { - tasks.withType { - useJUnitPlatform() - } - } - private fun Project.configureSubProjects() { subprojects { applyTestOptions() @@ -34,6 +28,12 @@ class RootProjectPlugin : Plugin { } } + private fun Project.applyTestOptions() { + tasks.withType { + useJUnitPlatform() + } + } + private fun Project.applyKotlinCompileOptions() { tasks.withType().configureEach { kotlinOptions { diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/AndroidProjectExtensions.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/AndroidProjectExtensions.kt index 8f4161e..e1a893e 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/AndroidProjectExtensions.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/AndroidProjectExtensions.kt @@ -4,6 +4,7 @@ import com.android.build.gradle.BaseExtension import org.gradle.api.Project import org.gradle.api.plugins.ExtensionAware import org.gradle.api.plugins.ExtensionContainer +import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions fun Project.android(configure: BaseExtension.() -> Unit) { @@ -11,6 +12,11 @@ fun Project.android(configure: BaseExtension.() -> Unit) { configure(android) } +fun Project.composeCompiler(configure: ComposeCompilerGradlePluginExtension.() -> Unit) { + val android = extensions.findByName("composeCompiler") as ComposeCompilerGradlePluginExtension + configure(android) +} + fun BaseExtension.kotlinOptions(configure: KotlinJvmOptions.() -> Unit) = androidExtensions.configure("kotlinOptions", configure) diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/ProjectExtensions.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/ProjectExtensions.kt index 0ff4b00..112db44 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/ProjectExtensions.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/ext/ProjectExtensions.kt @@ -1,9 +1,10 @@ package com.soberg.gradle.plugin.ext -import org.gradle.accessors.dm.LibrariesForLibs import org.gradle.api.Project +import org.gradle.api.artifacts.VersionCatalog +import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.api.plugins.PluginContainer -import org.gradle.kotlin.dsl.the +import org.gradle.kotlin.dsl.getByType import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension internal fun Project.kotlin(configure: KotlinJvmProjectExtension.() -> Unit) = @@ -13,5 +14,5 @@ internal fun Project.plugins(configure: PluginContainer.() -> Unit) { configure(plugins) } -internal val Project.libs: LibrariesForLibs - get() = project.the() \ No newline at end of file +internal val Project.libs + get(): VersionCatalog = extensions.getByType().named("libs") \ No newline at end of file diff --git a/build-logic/settings.gradle.kts b/build-logic/settings.gradle.kts index 24cf57d..798fef0 100644 --- a/build-logic/settings.gradle.kts +++ b/build-logic/settings.gradle.kts @@ -1,14 +1,14 @@ dependencyResolutionManagement { + repositories { + google() + mavenCentral() + } + versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } - - repositories { - gradlePluginPortal() - google() - } } rootProject.name = "build-logic" diff --git a/build.gradle.kts b/build.gradle.kts index d740cf3..4149b85 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,9 +1,11 @@ plugins { - id("local.root.project") alias(libs.plugins.dependencyAnalysis) + alias(libs.plugins.compose.compiler) apply false alias(libs.plugins.hilt) apply false alias(libs.plugins.kover) apply false alias(libs.plugins.ksp) apply false + + id("local.root.project") } buildscript { diff --git a/gradle.properties b/gradle.properties index 87eb55d..9e20fb2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,10 +8,11 @@ # The setting is particularly useful for tweaking memory settings. # Note: Increased metaspace size is useful for metaspace-heavy tasks like lint org.gradle.jvmargs=-Xmx12g -XX:MaxMetaspaceSize=4g -XX:+UseParallelGC -# Turn on parallel compilation, caching and on-demand configuration -org.gradle.configureondemand=true +# Turn on parallel compilation and caching org.gradle.caching=true org.gradle.parallel=true +# Not encouraged by Gradle and can produce weird results. Wait for isolated projects instead. +org.gradle.configureondemand=false # Turn on the configuration cache org.gradle.configuration-cache=true org.gradle.configuration-cache.problems=warn diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5cefcf2..8329016 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,6 @@ androidxLifecycle = "2.8.0" androidxCoreTest = "1.5.0" assertk = "0.28.1" composeBom = "2024.05.00" -composeCompiler = "1.5.14" composeNavigation = "2.7.7" dagger = "2.51.1" dependencyAnalysis = "1.31.0" @@ -32,8 +31,6 @@ turbine = "1.1.0" # AndroidX androidX-activityCompose = { module = "androidx.activity:activity-compose", version.ref = "androidxActivity" } -## This is only present in the TOML so that Renovate will pickup the version. -android-composeCompiler = { module = "androidx.compose.compiler:compiler", version.ref = "composeCompiler" } androidX-navigationCompose = { module = "androidx.navigation:navigation-compose", version.ref = "composeNavigation" } androidX-lifecycle-composeRuntime = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidxLifecycle" } androidX-lifecycle-composeViewModelUtilities = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidxLifecycle" } @@ -97,10 +94,10 @@ test-turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" } # Plugins android-gradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "androidGradlePlugin" } +composeCompiler-gradlePlugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" } hilt-agp = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "dagger" } kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } - [bundles] compose-core = ["compose-ui", "compose-material3", "compose-ui-tooling"] ktor-client = ["ktor-client-contentNegotiation", "ktor-client-core", "ktor-client-json", "ktor-client-logging", "ktor-client-okhttp", "ktor-client-serialization"] @@ -109,6 +106,7 @@ robolectricTest = ["test-androidx-coreKtx", "test-junitJupiter", "test-junit4", [plugins] +compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } dependencyAnalysis = { id = "com.autonomousapps.dependency-analysis", version.ref = "dependencyAnalysis" } hilt = { id = "com.google.dagger.hilt.android", version.ref = "dagger" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 5fdb556..c3308f4 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -7,10 +7,21 @@ pluginManagement { } } -plugins { - id("local.root.settings") +enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + + repositories { + google { + content { + includeGroupByRegex("com\\.android\\..*") + includeGroupByRegex("com\\.google\\..*") + includeGroupByRegex("androidx\\..*") + } + } + mavenCentral() + } } - rootProject.name = "android-net-info" include(":app") From 8e2ad0b6f39a622aa332982de2ad3bce0602d5ec Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Mon, 27 May 2024 09:25:20 -0400 Subject: [PATCH 2/3] Add .kotlin to gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index beb42a1..96a2632 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,7 @@ lint/outputs/ lint/tmp/ # Android Profiling -*.hprof \ No newline at end of file +*.hprof + +# Kotlin 2.0+ +.kotlin/ \ No newline at end of file From 36aa878ac6cad51b741b58693922accd94f880a5 Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Mon, 27 May 2024 09:26:20 -0400 Subject: [PATCH 3/3] Get K2.0 + compose gradle working --- build-logic/plugins/build.gradle.kts | 9 ++++++--- .../plugin/AndroidComposeConventionPlugin.kt | 13 +++++++------ build.gradle.kts | 1 - settings.gradle.kts | 17 ++--------------- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/build-logic/plugins/build.gradle.kts b/build-logic/plugins/build.gradle.kts index 7d7c164..3ae3659 100644 --- a/build-logic/plugins/build.gradle.kts +++ b/build-logic/plugins/build.gradle.kts @@ -3,9 +3,12 @@ plugins { } dependencies { - compileOnly(libs.android.gradlePlugin) - compileOnly(libs.composeCompiler.gradlePlugin) - compileOnly(libs.kotlin.gradlePlugin) + implementation(libs.android.gradlePlugin) + implementation(libs.composeCompiler.gradlePlugin) + implementation(libs.kotlin.gradlePlugin) + + // TODO: Fixes dependency conflict issues with Dagger/Hilt, re: https://github.com/google/dagger/issues/3068. Once this issues is resolved and we upgrade dagger/hilt, remove this dependency. + implementation(libs.javapoet) } gradlePlugin { diff --git a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt index 5acf9fb..66769c0 100644 --- a/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt +++ b/build-logic/plugins/src/main/kotlin/com/soberg/gradle/plugin/AndroidComposeConventionPlugin.kt @@ -38,12 +38,13 @@ class AndroidComposeConventionPlugin : Plugin { composeCompiler { enableStrongSkippingMode.set(true) - stabilityConfigurationFile.set( - layout.buildDirectory.dir("compose-metrics").get().asFile - ) - reportsDestination.set( - layout.buildDirectory.dir("compose-reports").get().asFile - ) + // TODO Enable config/reports + /* stabilityConfigurationFile.set( + layout.buildDirectory.dir("compose_compiler").get().asFile + ) + reportsDestination.set( + rootProject.layout.projectDirectory.file("stability_config.conf").asFile + )*/ } } } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 4149b85..07b9b56 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,5 @@ plugins { alias(libs.plugins.dependencyAnalysis) - alias(libs.plugins.compose.compiler) apply false alias(libs.plugins.hilt) apply false alias(libs.plugins.kover) apply false alias(libs.plugins.ksp) apply false diff --git a/settings.gradle.kts b/settings.gradle.kts index c3308f4..dbf603c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -7,22 +7,9 @@ pluginManagement { } } -enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") -dependencyResolutionManagement { - repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) - - repositories { - google { - content { - includeGroupByRegex("com\\.android\\..*") - includeGroupByRegex("com\\.google\\..*") - includeGroupByRegex("androidx\\..*") - } - } - mavenCentral() - } +plugins { + id("local.root.settings") } -rootProject.name = "android-net-info" include(":app") include(":base:annotations")