|
| 1 | +package com.seanproctor.datatable |
| 2 | + |
| 3 | +import com.android.build.api.dsl.CommonExtension |
| 4 | +import org.gradle.api.JavaVersion |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.api.plugins.JavaPluginExtension |
| 7 | +import org.gradle.kotlin.dsl.assign |
| 8 | +import org.gradle.kotlin.dsl.configure |
| 9 | +import org.gradle.kotlin.dsl.dependencies |
| 10 | +import org.gradle.kotlin.dsl.provideDelegate |
| 11 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 12 | +import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension |
| 13 | +import org.jetbrains.kotlin.gradle.dsl.KotlinBaseExtension |
| 14 | +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension |
| 15 | + |
| 16 | +/** |
| 17 | + * Configure base Kotlin with Android options |
| 18 | + */ |
| 19 | +internal fun Project.configureKotlinAndroid( |
| 20 | + commonExtension: CommonExtension<*, *, *, *, *, *>, |
| 21 | +) { |
| 22 | + commonExtension.apply { |
| 23 | + compileSdk = 35 |
| 24 | + |
| 25 | + defaultConfig { |
| 26 | + minSdk = 21 |
| 27 | + } |
| 28 | + |
| 29 | + compileOptions { |
| 30 | + // Up to Java 11 APIs are available through desugaring |
| 31 | + // https://developer.android.com/studio/write/java11-minimal-support-table |
| 32 | + sourceCompatibility = JavaVersion.VERSION_11 |
| 33 | + targetCompatibility = JavaVersion.VERSION_11 |
| 34 | + isCoreLibraryDesugaringEnabled = true |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + configureKotlin<KotlinAndroidProjectExtension>() |
| 39 | + |
| 40 | + dependencies { |
| 41 | + "coreLibraryDesugaring"(libs.findLibrary("android.desugarJdkLibs").get()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Configure base Kotlin options for JVM (non-Android) |
| 47 | + */ |
| 48 | +internal fun Project.configureKotlinJvm() { |
| 49 | + extensions.configure<JavaPluginExtension> { |
| 50 | + // Up to Java 11 APIs are available through desugaring |
| 51 | + // https://developer.android.com/studio/write/java11-minimal-support-table |
| 52 | + sourceCompatibility = JavaVersion.VERSION_11 |
| 53 | + targetCompatibility = JavaVersion.VERSION_11 |
| 54 | + } |
| 55 | + |
| 56 | + configureKotlin<KotlinJvmProjectExtension>() |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Configure base Kotlin options |
| 61 | + */ |
| 62 | +private inline fun <reified T : KotlinBaseExtension> Project.configureKotlin() = configure<T> { |
| 63 | + // Treat all Kotlin warnings as errors (disabled by default) |
| 64 | + // Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties |
| 65 | + val warningsAsErrors: String? by project |
| 66 | + when (this) { |
| 67 | + is KotlinAndroidProjectExtension -> compilerOptions |
| 68 | + is KotlinJvmProjectExtension -> compilerOptions |
| 69 | + else -> TODO("Unsupported project extension $this ${T::class}") |
| 70 | + }.apply { |
| 71 | + jvmTarget = JvmTarget.JVM_11 |
| 72 | + allWarningsAsErrors = warningsAsErrors.toBoolean() |
| 73 | + freeCompilerArgs.add( |
| 74 | + // Enable experimental coroutines APIs, including Flow |
| 75 | + "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi", |
| 76 | + ) |
| 77 | + freeCompilerArgs.add( |
| 78 | + /** |
| 79 | + * Remove this args after Phase 3. |
| 80 | + * https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-consistent-copy-visibility/#deprecation-timeline |
| 81 | + * |
| 82 | + * Deprecation timeline |
| 83 | + * Phase 3. (Supposedly Kotlin 2.2 or Kotlin 2.3). |
| 84 | + * The default changes. |
| 85 | + * Unless ExposedCopyVisibility is used, the generated 'copy' method has the same visibility as the primary constructor. |
| 86 | + * The binary signature changes. The error on the declaration is no longer reported. |
| 87 | + * '-Xconsistent-data-class-copy-visibility' compiler flag and ConsistentCopyVisibility annotation are now unnecessary. |
| 88 | + */ |
| 89 | + "-Xconsistent-data-class-copy-visibility" |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
0 commit comments