Dependency resolution and accessing maven classifier

Until now I was using this to get list of all dependencies

runtimeConfiguration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.forEach { dependency: ResolvedDependency ->
                dependency.moduleArtifacts.forEach { artifact: ResolvedArtifact ->
                    val newDep = MavenCoordinates(
                        dependency.moduleGroup, dependency.moduleName,
                        dependency.moduleVersion, artifact.type, artifact.classifier
                    )

But it started to causing this Error. I found that old way is now deprecated,

Caused by: org.gradle.internal.component.AmbiguousVariantSelectionException: The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr
' with value 'debug', attribute 'basic' with value 'demo', attribute 'com.android.build.gradle.internal.dependency.AndroidTypeAttr' with value 'Aar', attribute 'org.jetbrains.kotlin.platform.type' with value
 'androidJvm'. However we cannot choose between the following variants of project :sample-library:
  - Configuration ':sample-library:debugRuntimeElements' variant android-assets declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', at
tribute 'com.android.build.gradle.internal.dependency.AndroidTypeAttr' with value 'Aar', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
      - Unmatched attributes:
          - Provides attribute 'artifactType' with value 'android-assets' but the consumer didn't ask for it
          - Doesn't say anything about basic (required 'demo')
          - Provides attribute 'com.android.build.api.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it

So I started using this

    runtimeConfiguration.incoming.artifactView {
        isLenient = true
    }.artifacts.forEach {
        val artifact =
            it.id.componentIdentifier as org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier

        val newDep = MavenCoordinates(
            artifact.group,
            artifact.module,
            artifact.version,
            it.variant.attributes.getAttribute(Attribute.of("artifactType", String::class.java))!!,
            null
        )

It also failing, but only when isLenient is false, but with true it works.

But I can access maven classifier from this API please?

I found that most of dependency ComponentArtifactIdentifier are type of org.gradle.internal.component.external.model.DefaultModuleComponentArtif actIdentifier where is possible to this

name.classifier

Something like this

    }.artifacts.forEach {
        if (it.id is ModuleComponentArtifactIdentifier) {
            val identifier = it.id as ModuleComponentArtifactIdentifier

            val newDep = MavenCoordinates(
                identifier.componentIdentifier.group,
                identifier.componentIdentifier.module,
                identifier.componentIdentifier.version,
                if (identifier is DefaultModuleComponentArtifactIdentifier) identifier.name.type else "?",
                if (identifier is DefaultModuleComponentArtifactIdentifier) identifier.name.classifier else null
            )

But some of dependencies are org.gradle.internal.component.external.model.ModuleComponentFileArtifactIdentifier

I really don’t understand why so many types, everything is just maven dependency.

And also is weird that all of these classes are internal. There is really not a public API?

1 Like

@Tomas_Prochazka were you ever able to resolve this?