is there an API I can use in kotlin to check for e...
# gradle
m
is there an API I can use in kotlin to check for existence of a specific dependency/artifact in a module? I'm looking for something like
pluginManager.withPlugin()
/
hasPlugin()
but for dependencies.
v
Mind elaborating on your use-case?
m
I'm making a convention plugin and based on the existence of some dependencies, I should act differently otherwise I would get some warning messages like these:
androidx.compose.material3.ExperimentalMaterial3Api is unresolved. Please make sure it's present in the module dependencies
it is not a major issue as a I said it is just a warning but I wanted to ask if there is something I can use for this.
Copy code
val configurationName = "implementation"
val dependencyGroupId = "com.example"
val dependencyArtifactId = "my-library"

val matchingConfiguration = project.configurations.findByName(configurationName)

if (matchingConfiguration != null) {
  val foundDependency = matchingConfiguration.artifacts.find {
    it.group == dependencyGroupId && it.name == dependencyArtifactId
  }
  if (foundDependency != null) {
    println("Dependency '${dependencyGroupId}:${dependencyArtifactId}' exists in configuration '${configurationName}'")
  } else {
    println("Dependency '${dependencyGroupId}:${dependencyArtifactId}' not found in configuration '${configurationName}'")
  }
} else {
  println("Configuration '${configurationName}' not found")
}
Is something like this is fine? haven't tested it though
v
No, not really.
Do you only care about dependencies declared explicitly or also if it comes in transitively?
m
dependencies declared explicitly
v
Then
Configuration#withDependencies
is your friend
It is a hook that is executed right before resolution to add last-minute dependencies for example based on other declared dependencies
But not on
implementation
but on resolvable configurations, for example
runtimeClasspath
or
compileClasspath
m
Ah, is there anything equivalent for non-resolvable configurations (e.g
implementation
) because I'm interested in those? or there can't be because I'm misunderstanding something?
v
Wait, I'm talking non-sense, you cannot declare on the resolvable configurations.
So yes, do it on
implementation
m
Thanks for your help I will try it.
👌 1