Skip to content

Commit

Permalink
chore: add desugaring for android target in kmp modules
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Sep 2, 2024
1 parent 09b2d3a commit 00032b0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import extensions.configureDesugaring
import extensions.configureKotlinAndroid
import extensions.stringBuildConfigField
import extensions.toProperties
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType

class AndroidSampleApplicationPlugin: Plugin<Project> {
override fun apply(target: Project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import com.android.build.gradle.LibraryExtension
import extensions.configureDesugaring
import extensions.configureKotlinAndroid
import extensions.configureKotlinCompiler
import org.gradle.api.Plugin
Expand All @@ -16,11 +17,12 @@ class MultiplatformLibraryPlugin: Plugin<Project> {
extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
configureKotlinCompiler(jvmVersion = 17)
configureDesugaring(this)
sourceSets.getByName("main").manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig.targetSdk = 34
}
// FIXME Android Studio build is looking for a testClasses task but it doesn't exist.
tasks.register("testClasses")
}
}
}
}
7 changes: 6 additions & 1 deletion build-logic/plugins/src/main/kotlin/extensions/Kotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal fun Project.configureKotlinCompiler(jvmVersion: Int) {
this.tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.toVersion(jvmVersion).toString()
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn"
)
jvmTarget = JavaVersion.toVersion(jvmVersion).toString()
}
}
tasks.withType<JavaCompile> {
val javaToolchains = project.extensions.getByType<JavaToolchainService>()
Expand Down
13 changes: 0 additions & 13 deletions build-logic/plugins/src/main/kotlin/extensions/KotlinAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ package extensions
import com.android.build.api.dsl.CommonExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.kotlin.dsl.dependencies
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions

internal fun configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
Expand All @@ -33,20 +31,9 @@ internal fun Project.configureDesugaring(
compileOptions {
isCoreLibraryDesugaringEnabled = true
}

kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn"
)
jvmTarget = JavaVersion.VERSION_17.toString()
}
}

dependencies {
add("coreLibraryDesugaring", "com.android.tools:desugar_jdk_libs:1.1.5")
}
}

private fun CommonExtension<*, *, *, *, *, *>.kotlinOptions(block: KotlinJvmOptions.() -> Unit) {
(this as ExtensionAware).extensions.configure("kotlinOptions", block)
}

0 comments on commit 00032b0

Please sign in to comment.