Skip to content

Instantly share code, notes, and snippets.

@rakshitsoni02
Last active June 13, 2024 09:25
Show Gist options
  • Save rakshitsoni02/c9a8a557bb9316dd4c21ccc83135a4ff to your computer and use it in GitHub Desktop.
Save rakshitsoni02/c9a8a557bb9316dd4c21ccc83135a4ff to your computer and use it in GitHub Desktop.
Groovy to kotlin migration build logic, sample of common extensions and base config for reuse
@file:Suppress("UnstableApiUsage")
package com.orgname.samples.plugins
import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
internal fun Project.configureAndroidCompose(
commonExtension: CommonExtension<*, *, *, *, *, *>
) {
commonExtension.apply {
with(pluginManager) {
apply("org.jetbrains.kotlin.plugin.compose")
}
buildFeatures {
compose = true
}
dependencies {
val bom = libs.findLibrary("androidx-compose-bom").get()
add("implementation", platform(bom))
add("androidTestImplementation", platform(bom))
add("implementation", libs.findLibrary("androidx-compose-foundation").get())
add("implementation", libs.findLibrary("androidx-compose-runtime-livedata").get())
add("implementation", libs.findLibrary("androidx-compose-material-iconsExtended").get())
add("implementation", libs.findLibrary("androidx-compose-material3").get())
add("implementation", libs.findLibrary("androidx-navigation-compose").get())
add("implementation", libs.findLibrary("androidx-activity-compose").get())
add("implementation", libs.findLibrary("androidx-lifecycle-runtimeCompose").get())
add("implementation", libs.findLibrary("androidx-compose-ui-tooling-preview").get())
add("implementation", libs.findLibrary("androidx-compose-ui-testManifest").get())
add("debugImplementation", libs.findLibrary("androidx-compose-ui-tooling").get())
add("implementation", libs.findLibrary("kotlinx-kotlinx-collections").get())
add("implementation", libs.findLibrary("accompanist-adaptive").get())
add("implementation", libs.findLibrary("accompanist-permissions").get())
add("testImplementation", libs.findLibrary("androidx-compose-ui-test").get())
add(
"androidTestImplementation",
libs.findLibrary("androidx-compose-ui-test-android").get()
)
}
testOptions {
unitTests {
// For Robolectric
isIncludeAndroidResources = true
}
}
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs += buildComposeMetricsParameters()
}
}
}
private fun Project.buildComposeMetricsParameters(): List<String> {
val metricParameters = mutableListOf<String>()
val enableMetricsProvider = project.providers.gradleProperty("enableComposeCompilerMetrics")
val relativePath = projectDir.relativeTo(rootDir)
val buildDir = layout.buildDirectory.get().asFile
val enableMetrics = (enableMetricsProvider.orNull == "true")
if (enableMetrics) {
val metricsFolder = buildDir.resolve("compose-metrics").resolve(relativePath)
metricParameters.add("-P")
metricParameters.add(
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + metricsFolder.absolutePath,
)
}
val enableReportsProvider = project.providers.gradleProperty("enableComposeCompilerReports")
val enableReports = (enableReportsProvider.orNull == "true")
if (enableReports) {
val reportsFolder = buildDir.resolve("compose-reports").resolve(relativePath)
metricParameters.add("-P")
metricParameters.add(
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + reportsFolder.absolutePath
)
}
return metricParameters.toList()
}
package com.orgname.samples.plugins
import com.android.build.api.dsl.CommonExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
/**
* Configure base Kotlin with Android options
*/
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = 34
ndkVersion = "21.0.6113669"
defaultConfig {
minSdk = 26
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
configureKotlin()
}
/**
* Configure base Kotlin options for JVM (non-Android)
*/
internal fun Project.configureKotlinJvm() {
extensions.configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
configureKotlin()
}
/**
* Configure base Kotlin options
*/
private fun Project.configureKotlin() {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
val warningsAsErrors: String? by project
allWarningsAsErrors = warningsAsErrors.toBoolean()
}
}
dependencies {
add("implementation", libs.findLibrary("kotlin.stdlib").get())
}
}
internal const val APP_TARGET_SDK_VERSION = 34
@file:Suppress("UnstableApiUsage")
package com.orgname.samples.plugins
import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
internal fun Project.configureTest() {
tasks.withType<Test>().configureEach {
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
excludes = listOf("jdk.internal.*")
}
}
}
internal fun Project.configureAndroidTest(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
testOptions.unitTests.apply {
isReturnDefaultValues = true
isIncludeAndroidResources = true
all {
it.jvmArgs("-Djavax.net.ssl.trustStore=${project.rootDir}/certificate/cacerts")
it.systemProperty("robolectric.dependency.proxy.host",
project.findProperty("systemProp.https.proxyHost")!!
)
it.systemProperty("robolectric.dependency.proxy.port",
project.findProperty("systemProp.https.proxyPort")!!
)
}
sourceSets {
getByName("androidTest").java.srcDir("src/androidTest/kotlin")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment