I want to define a Gradle Plugin inside my `build-...
# gradle
k
I want to define a Gradle Plugin inside my
build-logic
that applies the Jetbrains Compose libraries. But trying it this way results in an error. There is no
KotlinMultiplatformExtension#compose
property available.
Copy code
class KmpComposePlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                apply("kmp")
                apply("org.jetbrains.compose")
            }

            val extension = extensions.getByType<KotlinMultiplatformExtension>()
            with(extension) {
                jvm()

                sourceSets.commonMain.dependencies {
                    implementation(this@with.compose.runtime)
                }
            }
        }
    }
}
1
c
you need to
configure
the extenstion. at this point in time where you are trying to access it, its not “applied”.
Copy code
import org.gradle.kotlin.dsl.configure

extensions.configure<KotlinMultiplatformExtension>{}
👍 1
also, please don’t crosspost without reference. the answers might be useful for others.
k
> you need to
configure
the extenstion. at this point in time where you are trying to access it, its not “applied”. I changed the code to this but it still doesn't detect it. Is that what you meant by configuring? The compose property is still not accessible within my custom plugin.
Copy code
extensions.configure<KotlinMultiplatformExtension> {
   jvm()

   sourceSets.commonMain.dependencies {
     implementation(compose.runtime)
   }
}
h
Are you sure the kmp plugin exists?
👍 1
c
You
apply(“kmp”)
That’s not the kotlin Multiplattform plug-in 🤔
👍 1
h
And if you use precompiled script plugins you can easily write your convention plugins like regular gradle build kts files using the generated extension accessors.
v
You can access the dependencies from here
Copy code
val composeDeps = extensions.getByType<ComposeExtension>().dependencies
And then you can apply it like
Copy code
implementation(composeDeps.runtime)
1
k
Thank you! That solved it. I didn't notice it was part of the
ComposeExtension
. For people stumbling across this:
kmp
is the ID of my custom plugin above.
b
hmm, it did not recognized in my case. Should I add specific dependency to be able to import it inside build-logic/convention plugin gradle.kts. here current state of the gradle.
plugins *{*
``kotlin-dsl``
}
_group_ = "uz.uzkassa.buildlogic"
_java_ *{*
_sourceCompatibility_ = JavaVersion._VERSION_1_8_
targetCompatibility
= JavaVersion._VERSION_1_8_
}
_dependencies_ *{*
_compileOnly_(_libs_._android_._gradlePlugin_)
_compileOnly_(_libs_._kotlin_._gradlePlugin_)
}
_gradlePlugin_ *{*
plugins *{*
register("androidApplicationCompose")*{*
_id_ = "horeca.compose"
_implementationClass_ = "ComposeMultiplatformConventionPlugin"
}
register("androidApplication")*{*
_id_ = "horeca.feature"
_implementationClass_ = "FeatureMultiplatformConventionPlugin"
}
register("androidLibrary") *{*
_id_ = "horeca.android.library"
_implementationClass_ = "TEST_AndroidLibraryConventionPlugin"
}
}
}
k
This is how my custom plugin registers the compose dependencies
Copy code
val compose = extensions.getByType<ComposeExtension>().dependencies
            extensions.configure<KotlinMultiplatformExtension> {
                jvm()

                sourceSets.commonMain.dependencies {
                    implementation(compose.material3)
                    implementation(compose.materialIconsExtended)
                    implementation(compose.runtime)
                    implementation(compose.ui)
                }
            }
v
@Bekzod you need to add this as a
compileOnly
dependency
Copy code
compose-gradlePlugin = { module = "org.jetbrains.compose:org.jetbrains.compose.gradle.plugin", version.ref = "compose" }
b
@Vaibhav Jaiswal I added it here:
build-logic->convention->build.gradle.kts plugins{alias(_libs_._plugins_._jetbrainsCompose)_}
but it throw exception by asking other compose related configuration. I'm new to KMM, so struggling to set up.
v
@Bekzod Not in plugins but in the
dependencies
block as
compileOnly(libs.plugins.jetbrainsCompose
1
👍 1
149 Views