英文:
Get gradle dependencies programmatically
问题
我想在Android项目中通过Gradle以编程方式获取依赖项。
我尝试过(在项目和模块级别的gradle.build中):
task dumpDependencies {
doLast {
def resolved = configurations.compileClasspath.resolvedConfiguration
println "Files: $resolved.files"
}
}
但是我收到了这个错误:
> Could not get unknown property 'compileClasspath' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
我正在使用Gradle版本7.5
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
我漏掉了什么?
英文:
I want to get the dependencies programmatically within Gradle in an Android project.
I tried (in both project as well as module level gradle.build):
task dumpDependencies {
doLast {
def resolved = configurations.compileClasspath.resolvedConfiguration
println "Files: $resolved.files"
}
}
But I get this error:
> Could not get unknown property 'compileClasspath' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
I am using Gradle version 7.5
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
What am I missing?
答案1
得分: 1
以下是翻译后的内容:
compileClasspath
配置由 java
插件提供,更多信息请参阅 https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management。
要访问 compileClasspath
配置,需要插件 Java 声明
plugins {
id 'java'
}
对于默认使用 kotlin
插件的 Android 项目,没有 compileClasspath
配置。我可以考虑这个替代解决方案:
configurations.implementation.setCanBeResolved(true)
def implementationResolved = configurations.implementation.resolvedConfiguration
println "Implementation files: $implementationResolved.files"
configurations.compileOnly.setCanBeResolved(true)
def compileResolved = configurations.compileOnly.resolvedConfiguration
println "Compile only files: $compileResolved.files"
def files = implementationResolved.files + compileResolved.files
println "Files: $files"
英文:
The compileClasspath
configuration is provided by the java
plugin, see more at https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management.
So to access the compileClasspath
configuration, plugin Java declaration is required
plugins {
id 'java'
}
For Android projects, which by default use kotlin
plugin instead, there are no compileClasspath
configurations. I could think of this alternative solution:
configurations.implementation.setCanBeResolved(true)
def implementationResolved = configurations.implementation.resolvedConfiguration
println "Implementation files: $implementationResolved.files"
configurations.compileOnly.setCanBeResolved(true)
def compileResolved = configurations.compileOnly.resolvedConfiguration
println "Compile only files: $compileResolved.files"
def files = implementationResolved.files + compileResolved.files
println "Files: $files"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论