This message was deleted.
# community-support
s
This message was deleted.
t
Fwiw, you would now use
project.extensions.getByType(SourceSetContainer::class.java)
(or
project.the<SourceSetContainer>()
or similar) for Java projects.
For Android projects, you need to first get the Android extension, something like
project.the<CommonExtension>().sourceSets
(or use
ApplicationExtension
if you know you'd in an Android application).
j
@Thomas Broyer the difference between
the
and
extensions
is it is lazy so you don't need
afterEvaluate
?
t
r
Thx, which dependency do I need for that
the
extension function?
v
kotlinDsl()
iirc
t
gradleKotlinDsl()
, which you get automatically when applying the ``kotlin-dsl`` plugin (which also applies ``embedded-kotlin``, itself applying
kotlin("jvm") version embeddedKotlinVersion
)
👍 1
r
Great I got now the import working, next stupid question: how can I import that
CommonExtension
or
ApplicationExtension
classes? They are not in my class path by default
t
You need to add a dependency on the AGP (either
compileOnly
if your plugin could work without it,
implementation
if it's required, this declaration will then set a minimum version, or you could use
compileOnly
anyway and let the user explicitly add the plugin to their buildscript classpath), I'd use the
gradle-api
artifact then, that only exposes public APIs.
r
Good so I'm on the right track. I just idenfified this dependency name: com.android.tools.build:gradle
t
I'd suggest rather using
com.android.tools.build:gradle-api
then 😉
👍 1
r
good point
I get now a
org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin 'my.plugin.name'
Extension of type 'ApplicationExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension]
My plugin is now basically:
Copy code
open class MyPlugin : Plugin<Project> {
    override fun apply(project: Project): Unit = with(project) {
        val androidSourceSets = project.the<ApplicationExtension>().sourceSets
        // crash in line above
ah Javi provided the solution already I need to use
afterEvaluate
v
Don't use
afterEvaluate
. If your plugin cannot work without that plugin applied, then apply the plugin before configuring the extension.
If you just want to react if the plugin is applied, use
pluginManager.withId("...") { ... }
💯 1