Hey everyone I’m creating a custom Gradle Conventi...
# gradle
v
Hey everyone I’m creating a custom Gradle Convention Plugin How can I get the name of the exact gradle module the plugin is applied For Example
Copy code
:shared
  | -> :data
         | -> :network
         | -> :local
  | -> :domain
I have the plugin applied in every module I want to get the current module name i.e. for network it should be network I tried the
Project.name
but it gives me
"shared"
all the time
My function
Copy code
internal fun Project.configureKotlinCocoapods(
    extension: CocoapodsExtension
) = extension.apply {
    summary = "Some description for the Shared Module"
    homepage = "Link to the Shared Module homepage"
    version = "1.0"
    ios.deploymentTarget = "14.1"
    framework {
        isStatic = true
        baseName = <mailto:this@configureKotlinCocoapods.name|this@configureKotlinCocoapods.name>

        println("Module Name = " + <mailto:this@configureKotlinCocoapods.name|this@configureKotlinCocoapods.name>)
    }
}
j
you are applying the plugin only on the shared module, if not, name should show the value you are looking for
v
@Javier im adding the plugin on every module
j
how many names are printed in the terminal
v
I dont remember but there were multiple
shared was printed multiple times
j
try printing the project path
v
trying that
j
if you see the same path multiple times, you are not getting the correct project
v
:shared
is printed 6 times, but i have around 15 modules
Copy code
class KotlinMultiplatformPlugin: Plugin<Project> {

    override fun apply(target: Project):Unit = with(target){
        with(pluginManager){
            apply(libs.findPlugin("kotlinMultiplatform").get().get().pluginId)
            apply(libs.findPlugin("kotlinCocoapods").get().get().pluginId)
            apply(libs.findPlugin("androidLibrary").get().get().pluginId)
            apply(libs.findPlugin("kotlin.serialization").get().get().pluginId)
        }

        extensions.configure<KotlinMultiplatformExtension>(::configureKotlinMultiplatform)
        extensions.configure<LibraryExtension>(::configureKotlinAndroid)
    }
}

internal fun Project.configureKotlinMultiplatform(
    extension: KotlinMultiplatformExtension
) = extension.apply {
    jvmToolchain(17)
    androidTarget()
    iosArm64()
    iosX64()
    iosSimulatorArm64()

    applyDefaultHierarchyTemplate()

    sourceSets.apply {
        commonMain {
            dependencies {
                implementation(libs.findLibrary("koin.core").get())
                implementation(libs.findLibrary("coroutines.core").get())
                implementation(libs.findLibrary("kotlinx-dateTime").get())
                implementation(libs.findLibrary("napier").get())
                implementation(libs.findLibrary("kotlinx-serialization").get())
            }
        }

        androidMain {
            dependencies {
                implementation(libs.findLibrary("koin.android").get() )
            }
        }
    }

    (this as ExtensionAware).extensions.configure<CocoapodsExtension>(::configureKotlinCocoapods)
}
j
you are not getting the correct project instance, it is not possible to have more than one project with the same path
v
Anything wrong in my code?
j
I don’t know where you are applying that convention plugin
v
In every child module of shared
j
try at the start to print the path
v
okay
j
println(target.path)
v
:shared is printed only once
j
and the other projects?
v
No, its printing all modules
:shared sharedcore sharedui …..
j
the name you were getting was not from project.name
it would be some name from other configuration, maybe the cocoapods extension or whatever
v
i was using the correct
this
to get the name
j
change the path in the println with name and you will see it is working so the this is breaking that
v
checking
It was working, just printing 6 times each I thought its giving shared all the time
It prints all module name, each 6 times though
Copy code
> Configure project :shared:core
Module Name = core
Module Name = core
Module Name = core
Module Name = core
Module Name = core
Module Name = core

> Configure project :shared:data
Module Name = data
Module Name = data
Module Name = data
Module Name = data
Module Name = data
Module Name = data
using the original code
So, I’m getting the module name correctly It getting printed 6 times, is that an issue?
j
i haven’t used that extension so i don’t know what it does
v
No issues, Thanks a lot for helping
v
The function you give to
framework
is called once for every pod framework. So if you have 6 of them, you get 6 times the output.
v
@Vampire Thanks, got it Ive got 6 pod dependencies 3 in core module 3 in shared
Ive also got an
export(project(":shared:core"))
in my shared module
build.gradle