https://kotlinlang.org logo
#gradle
Title
# gradle
c

Ciaran Sloan

11/07/2023, 10:21 AM
Im trying to set some compiler args in my KMP project, which I previously used to do via a
kotlinOptions
block, but since updating kotlin and AGP this seems to no longer resolve. Im reading the docs and following whats specified here, but getting the error
Task with name 'compileKotlin' not found in project
. I'm declaring this in the
build.gradle.kts
of a kmp module. Has anyone any suggestions where I might be going wrong?
when I print the list of tasks available, I am seeing the following tasks among that list:
compileKotlinIosArm64, compileKotlinIosSimulatorArm64, compileKotlinIosX64, compileKotlinMetadata, compileLint, compileTestKotlinIosArm64, compileTestKotlinIosSimulatorArm64, compileTestKotlinIosX64
Curious as to how best to declare these compiler options without having to write this for each of these compile kotlin tasks?
some further context - I have a multi module project that has a mix of kmp and android modules. I'm using some
OptIn
APIs across these modules and I want to opt into these via compiler args. I'm trying to write this once in the project
v

Vampire

11/07/2023, 10:42 AM
That example does not match the describing text, you should open a documentation bug to the Kotlin project. The text says the example is to configure all kotlin compilation tasks, but only shows how to configure the specific
compileKotlin
task. The correct example should be something like
Copy code
tasks.withType<KotlinCompilationTask<*>>().configureEach {
    ...
}
👍 1
c

Ciaran Sloan

11/07/2023, 10:46 AM
ahh okay, this makes a lot of sense now! Thanks for the follow up, ill open a bug report on the docs 🙂
m

mbonnin

11/07/2023, 10:46 AM
FWIW, you can also do that:
Copy code
kotlin {
  targets.all {
    compilations.all {
      compilerOptions.configure {
      }
    }
  }
}
c

Ciaran Sloan

11/07/2023, 10:48 AM
right, but I need context for the
kotlin
lambda right? So how can I do this in a multi module setup?
m

mbonnin

11/07/2023, 10:48 AM
kotlin
is the top level extension for KGP
extensions.findByName("kotlin") as KotlinMultiplatformExtension
(but usually you should just be able to write
kotlin {}
in a build script
c

Ciaran Sloan

11/07/2023, 10:50 AM
I mean, yeah I'm already doing that of sorts, I have the following in a Convention plugin that im using in both android and kmp modules:
Copy code
extensions.findByType<KotlinAndroidProjectExtension>()?.apply {
    jvmToolchain(17)
}
extensions.findByType<KotlinMultiplatformExtension>()?.apply {
    jvmToolchain(17)
}
👍 1
but I wasnt sure how I can set compiler options in this KotlinMultiplatformExtension
but I see how your doing it above, thats helpful!
👍 1
heres what I've settled with, for future if anyone happens to peek here with similar questions:
Copy code
class KotlinConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            tasks.withType<KotlinCompilationTask<*>>().configureEach {
                compilerOptions.optIn.addAll(OPT_IN_FLAGS)
            }
            extensions.findByType<KotlinAndroidProjectExtension>()?.apply {
                jvmToolchain(17)
            }
            extensions.findByType<KotlinMultiplatformExtension>()?.apply {
                jvmToolchain(17)
            }
        }
    }
}
m

mbonnin

11/07/2023, 10:54 AM
If it's OptIn, you need, you can also do this 😅
Copy code
kotlin {
  targets.all {
    sourceSets.all {
      languageSettings.optIn("org.mylibrary.OptInAnnotation")
    }
  }
}
c

Ciaran Sloan

11/07/2023, 10:55 AM
right, but thats only specifically for KMP projects?
m

mbonnin

11/07/2023, 10:55 AM
Yes
c

Ciaran Sloan

11/07/2023, 10:55 AM
my use case has a mix of android and kmp projects
m

mbonnin

11/07/2023, 10:56 AM
I'm not 100% sure why optIn would be a sourceSet vs compilation thing but I'm thinking there is a future where source sets are compiled separately
my use case has a mix of android and kmp projects
The docs has separate instructions for both, I'm not sure why TBH
👍🏼 1
c

Ciaran Sloan

11/07/2023, 10:57 AM
yeah, I guess I could write this twice for separate android and kmp targets, but feels a bit simpler to define this once
💯 1
3 Views