MPP projects now also have experimental project le...
# eap
t
MPP projects now also have experimental project level and target level DSL for compiler options:
Copy code
kotlin {
     compilerOptions {
          // Project-level common compiler options that are used as defaults for all targets
          allWarningsAsErrors.set(true)
     }

     // Target-level compiler options
     jvm {
          compilerOptions {
               // JVM compiler options that are used as defaults for all compilations in this target
               jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
          }
     }

    js {
        compilerOptions {
              // JS compiler options that are used as defaults for all compilations in this target
              moduleKind.set(org.jetbrains.kotlin.gradle.dsl.JsModuleKind.AMD)
        }
    }

    linuxX64 {
        compilerOptions {
             // Native compiler options that are used as defaults for all compilations in this target
             languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0) // Overrides default version ‘1.9’
        }
    }

    iosArm64 {
       compilerOptions {
            // Native compiler options that are used as defaults for all compilations in this target
            allWarningsAsErrors.set(false) // Overrides project-level configuration
       }
    }
}
And related
languageSettings
values are also synced with related compilation task
compilerOptions
. Please give it a try and provide feedback 🙂
🤩 5
thank you color 4
🚀 10
m
Question:
compilerOption
is not available on
KotlinProjectExtension
, only on
KotlinJvmProjectExtension
and
KotlinMultiplatformExtension
so to configure the common options in a convention plugin, I have to do this:
Copy code
val kotlin = project.kotlinExtension
when (kotlin) {
    is KotlinJvmProjectExtension -> kotlin.compilerOptions {
      configureCommonOptions()
    }
    is KotlinMultiplatformExtension -> kotlin.compilerOptions {
      configureCommonOptions()
    }
  }
Or is there a way to do it in one go in a project that applies both the KMP and JVM plugins?
Copy code
project.kotlinExtension.compilerOptions.configureCommonOptions()
?
t
at the moment - there is no common interface. Somewhat related YT issue: https://youtrack.jetbrains.com/issue/KT-58956/Offer-a-shared-interface-for-JVM-and-Android-compilerOptions-in-Project-extension Generally we need to properly expose extensions in KGP-API - currently they are missing there except
KotlinTopLevelExtension
interface
👍 1
1
m
Mmm KMP is slightly different than Android though because of multiple targets so I wrote this:
Copy code
private fun KotlinProjectExtension.forEachCompilerOptions(block: KotlinCommonCompilerOptions.() -> Unit) {
  when (this) {
    is KotlinJvmProjectExtension -> compilerOptions.block()
    is KotlinMultiplatformExtension -> {
      targets.all {
        compilerOptions.block()
      }
    }
    else -> error("Unknown kotlin extension $this")
  }
}
But now I realize this is probably wrong as well? Should I go into the
compilations
compilerOptions? Or does
target.compilerOption
is going to be applied to each
compilation
?
t
you initial approach should work as you are configuring common compiler options. Generally idea is following:
kotlin.compilerOptions
used as a convention values for all targets,
target.compilerOptions
used a convention value for all compilations in this target and
kotlinCompilation.compilerOptions === kotlinCompileTask.compilerOptions
thank you color 1
kotlin.compilerOptions
should also configure Android target as well
👍 1
👍🏼 1