简体   繁体   English

Gradle 获取所有依赖项的列表

[英]Gradle get a list of all dependencies

I am writing a task for ProGuard and I need to get all the dependencies that are in the project, and then iterate over them.我正在为 ProGuard 编写一个任务,我需要获取项目中的所有依赖项,然后对其进行迭代。 I mean, I need to get subproject dependencies, all transition dependencies, and so on.我的意思是,我需要获取子项目依赖项、所有转换依赖项等等。

As a result, I have to copy all the compileOnly dependencies, at which I need to exclude the dependencies that are already in the compile in transitive dependencies结果,我必须复制所有 compileOnly 依赖项,在此我需要排除传递依赖项中已经在编译中的依赖项

Simply put, I need an array that will contain something like this class.简单地说,我需要一个包含类似 class 的数组。 How i can get it?我怎样才能得到它?

interface Dependency{
    String getArtifact();
    String getGroup();
    String getVersion();
    Scope getScope();
    File getJarFile();
}

If you want to see all dependencies in your project you can use this command line in the Android Studio terminal:如果您想查看项目中的所有依赖项,可以在 Android Studio 终端中使用此命令行:

./gradlew app:dependencies

You will be able to see something like this:您将能够看到如下内容:

在此处输入图像描述

buildSrc buildSrc

import org.gradle.api.artifacts.ResolvedArtifact;

public class DepName {
    public String id;
    public String scope;
    public String group;
    public String artifact;
    public String version;
    public String jarFile;
    public ResolvedArtifact resolvedArtifact;

    public DepName(ResolvedArtifact resolvedArtifact) {
        this.resolvedArtifact = resolvedArtifact;
        this.id = extractId(resolvedArtifact);
        String[] ids = this.id.split(":");
        this.group = ids[0];
        this.artifact = ids[1];
        this.version = ids[2];
        this.scope = "compileOnly";
        this.jarFile = resolvedArtifact.getFile().getAbsolutePath();
    }

    private String extractId(ResolvedArtifact resolvedArtifact) {
        String displayName = resolvedArtifact.getId().getDisplayName();
        return displayName.substring(displayName.indexOf("(")+1, displayName.length() -2);
    }

    @Override
    public String toString() {
        return "DepName{" +
                "id='" + id + '\'' +
                ", sc='" + scope + '\'' +
                ", g='" + group + '\'' +
                ", a='" + artifact + '\'' +
                ", v='" + version + '\'' +
                ", f='" + jarFile + '\'' +
                ", ra=" + resolvedArtifact +
                '}';
    }
}

build.gradle build.gradle

def depNames = new ArrayList<DepName>()
allprojects {
    afterEvaluate {
        configurations.compileOnly.resolvedConfiguration.resolvedArtifacts.each {d ->
            depNames.add(new DepName(d))
        }
    }
}

task compileOnlyList {
    doLast {
        println("The compileOnlyList \n ---------")
        println(depNames)
    }

Sample output样品 output

The compileOnlyList 
 ---------
[DepName{id=':my-jms:1.', sc='compileOnly', g='', a='my-jms', v='1.', f='/Users/projects/lib/my-jms-1.2.jar', ra=my-jms-1.2.jar (:my-jms:1.2)}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM