我正在使用一個大型的多模塊 Android 應用程式,并且我正在嘗試定義一個 Gradle 任務來收集所有運行時依賴項的 jar。我正在嘗試這樣的事情app/build.gradle:
task collectDeps {
doLast {
configurations.releaseRuntimeClasspath.resolvedConfiguration.resolvedArtifacts.each {
// do stuff
}
}
}
我過去在其他 Java 專案中使用過這個片段,所以我知道它在概念上是有效的;這只是我第一次在具有多種構建型別和/或變體的專案上嘗試它。
在 Android 專案上運行時,執行此任務會引發變體決議錯誤:
Execution failed for task ':app:collectDeps'.
> Could not resolve all dependencies for configuration ':app:releaseRuntimeClasspath'.
> The consumer was configured to find a runtime of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'. However we cannot choose between the following variants of project :myModule:
- Configuration ':myModule:releaseRuntimeElements' variant android-aar-metadata declares a runtime of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
- Unmatched attributes:
- Provides attribute 'artifactType' with value 'android-aar-metadata' but the consumer didn't ask for it
- Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'release' but the consumer didn't ask for it
- Provides a library but the consumer didn't ask for it
- Configuration ':myModule:releaseRuntimeElements' variant android-art-profile declares a runtime of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
- Unmatched attributes:
- Provides attribute 'artifactType' with value 'android-art-profile' but the consumer didn't ask for it
- Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'release' but the consumer didn't ask for it
- Provides a library but the consumer didn't ask for it
為簡潔起見,我已經洗掉了錯誤;總共有大約 20 個變體。請注意,這myModule是頂級應用程式的專案依賴項;如果我洗掉該依賴項,則錯誤是相同的,但來自不同的模塊。
我還應該在這里指出,所有其他構建目標都可以正常作業;該應用程式非常成熟,我所做的唯一更改是將這個新任務添加到app/build.gradle. 所以我假設我解決 Gradle 不喜歡的依賴項的方式有些問題,但我正在努力弄清楚是什么,或者如何解決它。
谷歌搜索這個錯誤不是很有幫助;Gradle 檔案對于如何解決變體的具體方法非常模糊,并且提供的解決方案似乎側重于更改將依賴項添加到專案中的方式;但我不一定想這樣做,因為該構建適用于所有其他用例。
理想情況下,我希望能夠在我的collectDeps任務中專門為解析度強制一個變體(實際上,理想情況下,collectDeps將在插件中定義)。這可能嗎?
如果重要,構建使用 Android Gradle 插件的 Gradle 7.2 和 v7.1.1
uj5u.com熱心網友回復:
可能有更好的方法來處理這個問題,但我最終通過從Sonatype 的開源 Nexus 掃描插件中獲得靈感來解決我的問題。代碼看起來像(這是在 Kotlin 中,但可以毫不費力地修改為 Groovy):
project.allprojects.forEach { project ->
val cfg = project.configurations.releaseRuntimeClasspath
try {
cfg.resolvedConfiguration.resolvedArtifacts.forEach {
// do stuff
}
} catch(e: Exception) {
when(e) {
is ResolveException, is AmbiguousVariantSelectionException -> {
val copyConfiguration = createCopyConfiguration(project)
cfg.allDependencies.forEach {
if(it is ProjectDependency) {
project.evaluationDependsOn(it.dependencyProject.path)
} else {
copyConfiguration.dependencies.add(it)
}
}
copyConfiguration.resolvedConfiguration.resolvedArtifacts.forEach {
// do stuff
}
}
else -> throw(e)
}
}
}
private fun createCopyConfiguration(project: Project): Configuration {
var configurationName = "myCopyConfiguration"
var i = 0
while(project.configurations.findByName(configurationName) != null) {
configurationName = i
i
}
val copyConfiguration = project.configurations.create(configurationName)
copyConfiguration.attributes {
val factory = project.objects
this.attribute(Usage.USAGE_ATTRIBUTE, factory.named(Usage::class.java, Usage.JAVA_RUNTIME))
}
return copyConfiguration
}
基本思想是,如果由于變數選擇不明確而無法決議配置,我將創建并注入一個新的父配置來指定屬性org.gradle.usage='java-runtime';這足以消除變體的歧義。
請注意,我沒有使用任何其他屬性對此進行測驗,因此它可能可以通過設定例如artifactType屬性來作業;但我的用例更具體地與運行時類路徑相關,所以這對我有用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485124.html
