Skip to content

Commit

Permalink
Add jvm library plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Jul 9, 2024
1 parent 57771eb commit c263109
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
13 changes: 9 additions & 4 deletions build-logic/structure/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`kotlin-dsl`
Expand All @@ -10,9 +10,10 @@ java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

Expand Down Expand Up @@ -53,5 +54,9 @@ gradlePlugin {
id = "looker.android.library"
implementationClass = "AndroidLibraryPlugin"
}
register("jvmLibraryPlugin") {
id = "looker.jvm.library"
implementationClass = "JvmLibraryPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import com.android.build.api.dsl.ApplicationExtension
import com.looker.droidify.configureKotlinAndroid
import com.looker.droidify.kotlin2
import com.looker.droidify.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.embeddedKotlin

class AndroidApplicationPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -25,8 +26,8 @@ class AndroidApplicationPlugin : Plugin<Project> {
}
}
dependencies {
add("implementation", embeddedKotlin("stdlib"))
add("implementation", embeddedKotlin("reflect"))
add("implementation", kotlin2("stdlib", libs))
add("implementation", kotlin2("reflect", libs))
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.looker.droidify.configureKotlinAndroid
import com.looker.droidify.kotlin2
import com.looker.droidify.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.embeddedKotlin

class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand Down Expand Up @@ -39,10 +40,10 @@ class AndroidLibraryPlugin : Plugin<Project> {
}
}
dependencies {
add("implementation", embeddedKotlin("stdlib"))
add("implementation", embeddedKotlin("reflect"))
add("testImplementation", embeddedKotlin("test"))
add("androidTestImplementation", embeddedKotlin("test"))
add("implementation", kotlin2("stdlib", libs))
add("implementation", kotlin2("reflect", libs))
add("testImplementation", kotlin2("test", libs))
add("androidTestImplementation", kotlin2("test", libs))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.looker.droidify

import org.gradle.api.Plugin
import org.gradle.api.Project

class JvmLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("org.jetbrains.kotlin.jvm")
apply("looker.lint")
}
configureKotlinJvm()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import DefaultConfig
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.assign
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
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

// Taken from NIA sample app by Google

Expand All @@ -34,33 +39,41 @@ internal fun Project.configureKotlinAndroid(
}
}

configureKotlin()
configureKotlin<KotlinAndroidProjectExtension>()

dependencies {
add("coreLibraryDesugaring", libs.getLibrary("android.desugarJdkLibs"))
}
}

internal fun Project.configureKotlinJvm() {
extensions.configure<JavaPluginExtension> {
// Up to Java 11 APIs are available through desugaring
// https://developer.android.com/studio/write/java11-minimal-support-table
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

configureKotlin<KotlinJvmProjectExtension>()
}

/**
* Configure base Kotlin options
*/
private fun Project.configureKotlin() {
// Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// Set JVM target to 11
jvmTarget = JavaVersion.VERSION_11.toString()
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
val warningsAsErrors: String? by project
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn",
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.FlowPreview",
"-Xcontext-receivers"
)
}
private inline fun <reified T : KotlinTopLevelExtension> Project.configureKotlin() = configure<T> {
when (this) {
is KotlinAndroidProjectExtension -> compilerOptions
is KotlinJvmProjectExtension -> compilerOptions
else -> TODO("Unsupported project extension $this ${T::class}")
}.apply {
apiVersion = KotlinVersion.KOTLIN_2_0
jvmTarget = JvmTarget.JVM_11
freeCompilerArgs = listOf(
"-opt-in=kotlin.RequiresOptIn",
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.FlowPreview",
"-Xcontext-receivers"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.getByType
import org.gradle.plugin.use.PluginDependency
import org.gradle.kotlin.dsl.kotlin

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

fun VersionCatalog.getLibrary(alias: String): Provider<MinimalExternalModuleDependency> =
findLibrary(alias).get()

fun VersionCatalog.getPlugin(alias: String): Provider<PluginDependency> =
findPlugin(alias).get()
fun DependencyHandler.kotlin2(module: String, catalog: VersionCatalog): Any =
kotlin(module, version = catalog.findVersion("kotlin").get().strictVersion)
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
# Plugins defined by this project
looker-android-application = { id = "looker.android.application", version = "unspecified" }
looker-android-library = { id = "looker.android.library", version = "unspecified" }
looker-jvm-library = { id = "looker.jvm.library", version = "unspecified" }
looker-hilt = { id = "looker.hilt", version = "unspecified" }
looker-hilt-work = { id = "looker.hilt.work", version = "unspecified" }
looker-lint = { id = "looker.lint", version = "unspecified" }
Expand Down

0 comments on commit c263109

Please sign in to comment.