This message was deleted.
# plugin-development
s
This message was deleted.
f
I tried cleaning the project but it didn't help
v
Is there a reason you are using an extension function? I'd just apply the convention plugin in the consumer and have the convention plugin directly do the configuration.
f
you mean instead of calling a function, extract another plugin that is then used by the plugins that currently consume the function?
v
It is hard to talk about this in this abstract way. Can you create a quick MCVE? Would make it much easier to talk about.
(everything works fine in this project)
Let me paste the relevant stuff here: plugin 1:
Copy code
import com.github.fnberta.kotlinbuild.configureAndroid

plugins {
    kotlin("android")
    id("com.android.application")
}

android {
    configureAndroid(this)
    ... more config
}
plugin 2:
Copy code
import com.github.fnberta.kotlinbuild.configureAndroid

plugins {
    kotlin("android")
    id("com.android.library")
}

android {
    configureAndroid(this)
    ... more config
}
The shared function:
Copy code
import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project

internal fun Project.configureAndroid(commonExtension: CommonExtension<*, *, *, *, *>) {
    commonExtension.apply {
           ... apply config
        }
    }
}
Does that help?
So basically extracting a plugin won't work because that plugin would need to apply either
com.android.application
or
com.android.library
to get access to the
android
extension
v
Ah, yeah, AGP fun. You could probably do it like
Copy code
val TARGET_SDK = 34
val MIN_SDK = 26

fun Project.configureAndroidCommon() = configure<CommonExtension<*, *, *, *, *>> {
    compileSdk = TARGET_SDK
    defaultConfig {
        minSdk = MIN_SDK
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

pluginManager.withPlugin("com.android.application") {
    configureAndroidCommon()
}
pluginManager.withPlugin("com.android.library") {
    configureAndroidCommon()
}
but you probably don't win too much by that. As the extension function is only used within your conventionp plugin project, it is probably find and I misunderstood. But then the answer is "no", a working project does not really help to see the error in the non-working project. 😄
f
Well this part of the code is identical between the projects 🤷 I can't reproduce this error in another project. I guess it's some kind of gradle caching issue or something like that
v
Well, some difference must be there 😄
f
pluginManager.withPlugin
didn't know about that, thanks! But I somehow feel this is less explicit than having specific application and library plugins?
yeah there must be 😄 I thought this is maybe a known error people have run into but I guess I need to dig deep and find the difference
v
You would still have them and in them apply that one. The
pluginManager.withPlugin
is the way to react to a plugin being applied
So that you don't introduce ordering constraints for plugins which is bad practice.
f
makes sense 👍
maybe a follow up question to that:
configureAndroidCommon
sets
Copy code
defaultConfig {
        minSdk = MIN_SDK
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
Now in consumers applying this plugin I'd like to extend this and specify for example:
Copy code
defaultConfig {
   someOtherProperty = something
}
Is this possible? Are they getting merged?
Or does the second override the first
v
What das IJ say if you mark the
this
in
android { configureAndroid(this) }
and press
Ctrl+Shift+p
?
Or does the second override the first
Unless the AGP is doing more weird stuff than I know, it should behave as expected, being additive.
Except if you set the same property, then it of course overwrites.
f
nice thanks!
👌 1
IJ says
Ch_srf_android_android_application_gradle
v
Well, you see, even IntelliJ knows that
this
is not the
CommonExtension
in that case, but the script class
Do the same in one of the working projects
Can you maybe share a build
--scan
of the working and non-working cases?
f
yes, will do in a sec. It's funny, when I comment out the
configureAndroid(this)
, I can build, then I uncomment it and do the ctrl shift p thing and it shows the correct type 🤷
that's the working one
that's the failing one
v
It seems the working one is not applying any Android plugin
f
well it's applying them inside pre-compiled script plugins, but none of them are applied in the project
(the same should be true though for the failing one 🤔)
v
The failing one has AGP 8.1.1 applied
f
ok why is that, let me investigate
👌 1
what i'm also seeing now
the failing one prints this error
Copy code
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.8.10` that might work differently than in the requested version `1.9.0`.
I have the exact same versions specified in the working one, but there I don't get it
the top level build.gradle.kts has
alias(libs.plugins.android.library) apply false
but this should only specify a version and not apply it?
v
Yes, this does not apply, but only adds it to the class path
f
ok but looking at the scan now, agp is applied but not in the project with the convention plugins
v
Unless it is in a precompiled script plugin where it should imho produce a warning, but actually just ignores the
apply false
f
if the projects don't depend on each other they shouldn't interfere no?
v
Not exactly sure what you mean, but I'd tend to say "exact"
f
well in the scan, agp is applied but only in projets independent of the one where i'm trying to build the convention plugins
hence this shouldn't be an issue?
v
Probably not, but if you look at the applied plugins, you see that in the working one
kotlin:1.8.10-gradle76
and
kotlin-scripting:1.8.10-gradle76
are applied while in the non-working both are applied with
1.9.0-gradle80
.
f
I see, this is for sure also what's causing the unsupportd kotlin plugin version warning
1
This is probably because the top level
build.gradle.kts
specifies
Copy code
alias(libs.plugins.kotlin.jvm) apply false
    alias(libs.plugins.kotlin.android) apply false
which I can't remove because then I get
Copy code
Error resolving plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.9.0']
> The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.
so I guess I want to override what the top level
build.gradle.kts
specifies for this one specific project that has the convention plugins
🤔
v
You should probably not mix the convention plugins and production build, but have the convention plugins in an included build for example.
f
yeah this is a multi project repo with many libraries. But I see now that build convention plugins should be separate
👌 1
Let me extract this thing! Thank you so much for your help, greatly appreciated!
👌 1