获取Gradle依赖项的程序化方法

huangapple go评论61阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年5月25日 11:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定