I'm trying to setup common configuration between all of my modules (which all have the multiplatform...
e
I'm trying to setup common configuration between all of my modules (which all have the multiplatform plugin applied) in the root
build.gradle.kts
but I can't figure out how to do that. Any help?
Copy code
allprojects {
  repositories {
    google()
    mavenCentral()
    maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
    maven("<https://s01.oss.sonatype.org/content/repositories/snapshots>")
  }

  // trying to do something like the following
  kotlin.sourceSets.all {
    languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
  }
}
Also I'd like to just set the baseline. For modules that need additional or different config, I'd like to be able to merge/override this in the module
build.gradle.kts
e
not going to work quite like that, Kotlin plugin isn't applied yet at this point and even if it were, type-safe won't be generated
the right solution would likely be to develop your own convention plugin
you might be able to get away with some combination of
gradle.afterProject { findPlugin()…
but it'll be in the wrong order for overriding
e
Thanks! With convention plugins, how would I get a reference to the multiplatform plugin extension?
Actually I was able to get it to work (without overriding) by doing:
Copy code
plugins.whenPluginAdded {
  if(this is KotlinBasePluginWrapper) {
    val kotlin = extensions.getByName("kotlin") as KotlinMultiplatformExtension
    kotlin.sourceSets.all {
      languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
    }
  }
}
Not sure if that is the "right" way or not, but it's a start, and I'll look more into convention plugins.
j
Use
plugins.withId('org.jetbrains.kotlin.multiplatform') { .. }
👍 1
e
Is there a better way to get a reference to the extension?
Actually seems like this is working:
Copy code
val kotlin = extensions.getByType<KotlinMultiplatformExtension>()
👍 1
l
the<KotlinMultiplatformExtension>()
will also work
e
that works as long as the root has the kotlin plugin in buildscript classpath and subprojects all apply the kotlin plugin, and will cause errors otherwise. I suppose if that's the case, it'll work... but that does not seem ideal to me
c
Are there any tips for migrating from doing
subprojects {}
in the root build.gradle to a convention plugin? I’ve had luck with convention plugins for java and maven, but the kotlin multiplatform plugin is giving me trouble. I specifically get import issues with the
org.jetbrains.kotlin
namespace.
Copy code
plugins.withId("org.jetbrains.kotlin.multiplatform") {
    extensions.findByType<org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension>()?.apply {
        targets.all {
            compilations.all {
                kotlinOptions {
                    allWarningsAsErrors = run {
                        val kotlinWarningsAsErrors: String by project
                        kotlinWarningsAsErrors.toBoolean()
                    }
                    freeCompilerArgs = freeCompilerArgs + "-Xopt-in=kotlin.RequiresOptIn"
                }
            }
        }
    }
}
I’ve fiddled with the plugins and repositories under both build-conventions/build.gradle.kts and build-conventions/settings.gradle.kts without much luck. If someone has a working example of applying multiplatform conventions, that would really help.