In Gradle, if I wanted to iterate over all the dep...
# multiplatform
s
In Gradle, if I wanted to iterate over all the dependencies in a multiplatform project for a specific platform, how would I do that?
j
Declared set? Or the resolved set?
You can look at cashapp/licensee for the resolved set if that's what you're after
j
I don't see any usage of the KGP APIs to get the target-specific dependencies in that file.
The gist of it is finding the runtime classpath name for a target's main compilation (i.e., not tests) and then looking it up https://github.com/cashapp/licensee/blob/trunk/src/main/kotlin/app/cash/licensee/plugin.kt#L176-L178
s
I've been hacking at it for the past 20 minutes. I found an approach that I think gets me there:
Copy code
val relevantConfigurations = mutableListOf<String>()
        kotlin.targets
            .filter { it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.js }
            .forEach {
                if (it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.js) {
                    val main = it.compilations["main"]
                    main.allKotlinSourceSets.forEach { sourceSet ->
                        relevantConfigurations += sourceSet.apiConfigurationName 
                        relevantConfigurations += sourceSet.implementationConfigurationName 
                    }
                }
            }
        
        for (configName in relevantConfigurations) {
            val configuration = configurations[configName]
            configuration.dependencies
        }
c
It’s a different usecase for this plugin. I create a separate configuration where I “copy” the dependencies and let gradle resolve the dependenciesto download the
.pom
definition.
Copy code
pomConfiguration.resolvedConfiguration.lenientConfiguration.artifacts
is the part where I get the resolved dependencies.
j
Yes, that's what licensee also does but it does it per target so we don't miss out on dependencies which only exist on a single target
s
Thank you - I will look over the licensee code and see if I can make this better
it feels very messy
c
@jw that done on plugin level.
configureMultiplatformProject
j
I see it now. Didn't realize you were overriding the collectDependencies API. KGP and AGP have APIs to get at all those configurations without relying on their current string form. Those can and have changed in the past and probably will in the future
c
Nice. thanks for the hint! 👍