Is it possible to add imports in a custom gradle p...
# gradle
b
Is it possible to add imports in a custom gradle plugin automatically to the build.gradle.kts file, e.g
Copy code
class MyPlugin : Plugin<Project> {
   fun apply(project: Project) {
      project.addImport("import com.foo.Foo") // i need the command for this
   }
}
so when i use it build.gradle i can directly access it
Copy code
import com.foo.Foo   // i want to get rid of this import

plugins {
   id("MyPlugin")
}

dependencies {
   // use Foo here directly without the need to add the import on top of every build.gradle.kts file when adding the "MyPlugin" plugin
}
k
Not that I know of ... Different build context....
b
ok thanks for the quick response
n
A good way around this is to use lambdas with implicit receivers
Copy code
fun myPluginConfig(config: Action<in Foo>) {...}

// in pure Kotlin this is:
fun myPluginConfig(config: Foo.() -> Unit) {...}
or have your plugin project declare
Foo
at the top level, so it is not part of any package and does not need any imports
b
Top Level classes sound great thanks