@@ -21,19 +21,22 @@ import com.android.build.api.dsl.LibraryExtension
2121import org.gradle.api.JavaVersion
2222import org.gradle.api.Plugin
2323import org.gradle.api.Project
24+ import org.gradle.api.plugins.AppliedPlugin
2425import org.gradle.api.tasks.compile.JavaCompile
26+ import org.gradle.api.tasks.testing.Test
2527import org.gradle.api.tasks.testing.logging.TestExceptionFormat
26- import org.gradle.kotlin.dsl.configure
28+ import org.gradle.kotlin.dsl.getByType
2729import org.gradle.kotlin.dsl.withType
2830import org.jetbrains.kotlin.gradle.dsl.JvmTarget
29- import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
31+ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3032
3133class AndroidApplicationConventionPlugin : Plugin <Project > {
3234 override fun apply (target : Project ) {
3335 with (target) {
3436 pluginManager.apply (" com.android.application" )
3537
3638 configureAndroid<ApplicationExtension >()
39+ configureKotlin()
3740 }
3841 }
3942}
@@ -44,6 +47,19 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
4447 pluginManager.apply (" com.android.library" )
4548
4649 configureAndroid<LibraryExtension >()
50+ configureKotlin()
51+ }
52+ }
53+ }
54+
55+ class JavaLibraryConventionPlugin : Plugin <Project > {
56+ override fun apply (target : Project ) {
57+ with (target) {
58+ pluginManager.apply (" java-library" )
59+ pluginManager.apply (" org.jetbrains.kotlin.jvm" )
60+
61+ configureJava()
62+ configureKotlin()
4763 }
4864 }
4965}
@@ -52,7 +68,7 @@ private val javaVersion = JavaVersion.VERSION_11
5268private val jvmTargetVersion = JvmTarget .JVM_11
5369
5470private inline fun <reified Ext : CommonExtension <* , * , * , * , * , * >> Project.configureAndroid () {
55- val commonExtension = extensions.getByType( Ext :: class .java )
71+ val commonExtension = extensions.getByType< Ext >( )
5672
5773 commonExtension.apply {
5874 compileOptions {
@@ -64,15 +80,7 @@ private inline fun <reified Ext : CommonExtension<*, *, *, *, *, *>> Project.con
6480 unitTests {
6581 isIncludeAndroidResources = true
6682 isReturnDefaultValues = true
67- all {
68- it.testLogging {
69- events(" failed" )
70- showExceptions = true
71- showCauses = true
72- showStackTraces = true
73- exceptionFormat = TestExceptionFormat .FULL
74- }
75- }
83+ all(Test ::configureTestLogging)
7684 }
7785 }
7886 }
@@ -81,11 +89,34 @@ private inline fun <reified Ext : CommonExtension<*, *, *, *, *, *>> Project.con
8189 sourceCompatibility = javaVersion.toString()
8290 targetCompatibility = javaVersion.toString()
8391 }
92+ }
8493
85- // Configure the Kotlin plugin if it is applied
86- pluginManager.withPlugin(" org.jetbrains.kotlin.android" ) {
87- extensions.configure<KotlinAndroidProjectExtension > {
88- compilerOptions { jvmTarget.set(jvmTargetVersion) }
89- }
94+ private fun Project.configureJava () {
95+ tasks.withType<JavaCompile >().configureEach {
96+ sourceCompatibility = javaVersion.toString()
97+ targetCompatibility = javaVersion.toString()
9098 }
99+
100+ tasks.withType<Test >().configureEach(Test ::configureTestLogging)
101+ }
102+
103+ private fun Test.configureTestLogging () = testLogging {
104+ events(" failed" )
105+ showExceptions = true
106+ showCauses = true
107+ showStackTraces = true
108+ exceptionFormat = TestExceptionFormat .FULL
109+ }
110+
111+ private fun Project.configureKotlin () {
112+ val configure =
113+ fun (_: AppliedPlugin ) {
114+ tasks.withType<KotlinCompile >().configureEach {
115+ compilerOptions { jvmTarget.set(jvmTargetVersion) }
116+ }
117+ }
118+
119+ // Configure the Kotlin plugin that is applied, if any
120+ pluginManager.withPlugin(" org.jetbrains.kotlin.android" , configure)
121+ pluginManager.withPlugin(" org.jetbrains.kotlin.jvm" , configure)
91122}
0 commit comments