我正在尝试编写gradle任务(下面的代码),以聚合来自多项目构建中ResolvedArtifact
对象的信息。但是,当我使用gradle 4.8.1在示例android项目(https://github.com/gmetal/sample-dependency-check-app)上运行任务时,出现以下错误:
07:29 $ gradle clean resolveArtifacts
> Task :app:resolveArtifacts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:resolveArtifacts'.
> Could not resolve all dependencies for configuration ':app:debugAndroidTestCompileClasspath'.
> More than one variant of project :mylibrary matches the consumer attributes:
- Configuration ':mylibrary:debugApiElements' variant android-aidl:
- Found artifactType 'android-aidl' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-classes:
- Found artifactType 'android-classes' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-manifest:
- Found artifactType 'android-manifest' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant android-renderscript:
- Found artifactType 'android-renderscript' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':mylibrary:debugApiElements' variant jar:
- Found artifactType 'jar' but wasn't required.
- Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
resolveArtifacts任务的代码如下:
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.tasks.TaskAction
import static org.gradle.api.specs.Specs.SATISFIES_ALL
class ResolveArtifacts extends DefaultTask {
ResolveArtifacts() {
group = 'Sample Task'
description = 'Attempts to resolve artifacts.'
}
@TaskAction
analyze() {
project.allprojects.collectMany { Project currentProject ->
currentProject.configurations.findAll {
canBeResolved(it)
}.each { Configuration configuration ->
configuration.resolvedConfiguration.lenientConfiguration.getArtifacts(SATISFIES_ALL).each { ResolvedArtifact artifact ->
logger.lifecycle("------ resolved ${artifact}")
}
}
}
}
def canBeResolved(configuration) {
configuration.metaClass.respondsTo(configuration, "isCanBeResolved") ?
configuration.isCanBeResolved() : true
}
}
关于如何解决上述错误的任何建议?原始问题已发布到github上的dependency-check-gradle。
答案 0 :(得分:1)
解决方案是使用configuration.incoming.artifactView并仅过滤所需的工件类型:
def artifactType = Attribute.of('artifactType', String)
def types = ['jar', 'aar']
types.each { type ->
configuration.incoming.artifactView {
attributes {
it.attribute(artifactType, type)
}
}.artifacts.each {
println "artifact file: ${it.file}"
}
}
见我的github issue与gradle团队。