I’m trying to make a convention plugin that enable...
# ksp
b
I’m trying to make a convention plugin that enables ksp with a custom extension. However it seems like whenever I’ve been trying to add these dependencies when the values are available that it doesn’t generate code. So I’m thinking that adding them in
afterEvaluate
isn’t working. I’m been looking for hooks to do it before that point and couldn’t find any. My workaround has been to creating a new convention plugin and just apply that in my plugins to enable ksp and whatever dependencies I want in it. Does anyone have any ideas of why it might not generate but if I add the same dependencies in a regular buildscript it will work?
Copy code
myextension {
  ksp = true
  // OR
  ksp {
    android(someDependency)
    common(someDependency)
  }
}
Here is how I’ve been adding the dependencies in
afterEvaluate
Copy code
plugins.withId(libs.plugins.ksp.get().pluginId) {
  val kotlin = extensions.getByName<KotlinMultiplatformExtension>("kotlin")
  val kspTargets = kotlin.targets.names.map { it.capitalizeUS() }
    .map {
      val name = if (it == "Metadata") "CommonMainMetadata" else it
      name.prefixIfNot("ksp")
    }

  configurations.filter { it.name in kspTargets }.forEach { configuration ->
    configuration.dependencies.addAll(
      buildList<Dependency> {
        if (anvil.get()) {
          add(libs.kotlin.inject.compiler.get())
          add(libs.anvil.compiler.get())
        }
        kspConfig.allDependencies.get().forEach { dependency ->
          add(dependency)
        }

        if (name.contains("android", ignoreCase = true)) {
          addAll(kspConfig.androidDependencies.get())
        }

        if (name.contains("ios", ignoreCase = true)) {
          addAll(kspConfig.iosDependencies.get())
        }
      }
    )
  }
}
It seems like this runs into the same issue as here https://github.com/google/ksp/issues/1524
j
Why do you need the
afterEvaluate
? you can do something like:
Copy code
ksp(enabled = true) // enabled = true by default

ksp {
   ...
}
And run the configuration just after checking
enabled
in the lambda without the need of
afterEvaluate
b
I just didn't want to apply KSP if I didn't need to
j
you don't need to apply it if
enabled = false
b
I'm not able to see that the enabled is evaluated until after the build script runs with my custom extension. A workaround is using a function to enable or disable it but that just looks silly compared to all the properties I have for everything else
j
Copy code
fun ksp(enabled: Boolean = true, action: Action<YourKspExtension>) {
    if (enabled) {
        action.execute(...)
        applyAndConfigureKsp()
    }
}
If you want to use
Property
, you will need a callback hell and/or custom ordering by defining priorities or using
afterEvaluate
.
I don't recommend trying it with
Property
, you will become mad. I would go with the function approach. The real problem is you cannot workaround this on Declarative Gradle, probably if they fix this issue (check the comment but not the OP message), it would fix your problem too.
b
I think for KSP I will have to use a function to do it. I've been searching all over and haven't seen anyone who's been able to do it any other way. Although I think doing it this way kind of clogs up my extension unfortunately