I’m near the end of creating a KMP gradle conventi...
# multiplatform
b
I’m near the end of creating a KMP gradle convention plugin and ran into some issues that I think may be because I’m configuring things too late (generated sources aren’t being picked up). Android Gradle Plugin has a hook that makes it easy to configure at the right time. Is there something similar to the AGP version but for KMP? Android has the
finalizeDsl
hook.
Copy code
plugins.withId("com.android.application") {
  val androidComponents = extensions.getByType(ApplicationAndroidComponentsExtension::class.java)
  androidComponents.finalizeDsl {
    it.configureAndroid(extension)
  }
}
It looks like
KotlinGradlePluginExtensionPoint
might be the way forward but there’s not a lot of documentation
a
Is there something similar to the AGP version but for KMP
no we don't have things like that. And generally we thrive to reduce any "late configurations" as much as possible. i.e. make kotlin gradle plugin dsl look more declarative. however there are still many things to do. to achieve that. Can you explain your case a bit more in detail? You've mentioned that
generated sources aren't being picked up
I can say that it should be doable in KMP to attache source generation task to the source set. This code should do the trick:
Copy code
abstract class SourceGenerator : DefaultTask() {
    @get:OutputDirectory
    abstract val outputDirectory: DirectoryProperty

    @TaskAction
    fun generate() {
        outputDirectory.file("Generated.kt").get().asFile.writeText("fun main() = println(\"Generated!\")")
    }
}
val sourceGenerator by tasks.registering(SourceGenerator::class) {
    outputDirectory.set(project.layout.buildDirectory.dir("generated"))
}

kotlin {
   // ... targets
  sourceSets {
      commonMain {
          kotlin.srcDir(sourceGenerator.map { it.outputDirectory })
      }
  }
}
b
I have a convention plugin with an extension. I need to wait for the extension to be evaluated before I can add some dependencies. It seems like the dependencies that are being added aren’t actually being picked up so nothing is getting generated. I have a setup where I’m doing a preconfigure and then a post-configure. The preconfigure sets up my targets (Android/iOS),
Copy code
kotlin {
  androidTarget()
  listOf(
    iosX64(),
    iosArm64(),
    iosSimulatorArm64()
  )
}
then so far the post-configure waits for the buildscript to evaluate so I can add whatever plugins and dependencies my extension wants. The issue I’ve been seeing is with adding ksp dependencies to my KMP project. This requires the preconfigure because it needs to know which targets to use. So in one spot I’m adding the ksp dependencies like usual
Copy code
val kspTargets = kotlin.targets.names.map { it.capitalizeUS() }

dependencies {
  for (target in kspTargets) {
    val targetConfigSuffix = if (target == "Metadata") "CommonMainMetadata" else target
    add("ksp${targetConfigSuffix}", myDependency)
  }
}
Then in another spot I’m adding another dependency
Copy code
kotlin {
  sourceSets {
    commonMain.dependencies {
      // Some more dependencies
    }
  }
}
This is all being set within the
KotlinMultiplatformExtension
rather than the dsl