12

我想在 gradle 任务中获取所有依赖项(包括传递的依赖项)的信息。

我试过代码:

class MyGradlePlugin implements Plugin<Project> {

  void apply(Project project) {
    project.afterEvaluate {
      println "  Project:" + project.name
      project.configurations.each { conf ->
        println "    Configuration: ${conf.name}"
        conf.allDependencies.each { dep ->
          println "      ${dep.group}:${dep.name}:${dep.version}"
        }
      }
    }
  }

}

但它只打印声明的,没有传递的。

这意味着,如果我dependencies是:

dependencies {
    compile "com.google.guava:guava:18.0"
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile 'junit:junit:4.11'
}

它只打印这 3 个依赖项,但不显示org.hamcrest:hamcrest-core:1.3传递依赖项。junit:junit:4.11

如何修改代码以使其也显示org.hamcrest:hamcrest-core:1.3

PS:我知道gradle dependencies任务会显示我想要的一切,但我需要手动获取依赖项信息并以我自己的格式打印。

4

3 回答 3

8

最后,我通过跟随任务弄清楚了

class Dep {
String group
String name
String version
String extention
String classifier

Dep(String group, String name, String version, String extension, String classifier) {
    this.group = group
    this.name = name
    this.version = version
    this.extention = extension
    this.classifier = classifier
}

}

任务 collectAllDeps {

def deps = []
configurations.each {
    conf ->
        if (conf.isCanBeResolved()) {
            conf.getResolvedConfiguration().getResolvedArtifacts().each {
                at ->
                    def dep = at.getModuleVersion().getId()
                    println at.getFile().getAbsolutePath()
                    //          dep = dep1.getComponentIdentifier()
                    println "$dep.group:$dep.name:$dep.version"
                    deps.add(new Dep(dep.group, dep.name, dep.version, at.extension, at.classifier))
            }
        }
}


def json = groovy.json.JsonOutput.toJson(deps)
json = groovy.json.JsonOutput.prettyPrint(json)

new File("deps.json") << json

}

于 2016-05-11T07:45:28.847 回答
0

安卓工作室 3.5

classpath 'com.android.tools.build:gradle:3.5.0'
  1. 创建一个演示 android 应用程序。

  2. 在 build.gradle 添加代码。

  3. 终端:./gradlew showAllDependenciesDebug

    //任务名称:showAllDependencies[Flavor][BuildType]

结果显示所有 sdk 信息,
包括通过以下方式导入的信息:'implementation fileTree(dir: 'libs', include: [' .jar', ' .aar'])'

import com.android.build.gradle.internal.ide.dependencies.ArtifactUtils
import com.android.build.gradle.internal.ide.dependencies.BuildMappingUtils
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.build.gradle.internal.publishing.AndroidArtifacts
import com.android.build.gradle.internal.scope.VariantScopeImpl


project.afterEvaluate {
    project.android.applicationVariants.each {variant ->
        project.tasks.create(name : "showAllDependencies${variant.name.capitalize()}", group : "help", description: "Print all sdk(s) infomation. author:sodino") {
            doLast {
                showAllDeps(project, variant)
            }
        }
    }
}

def showAllDeps(def project, def variant) {
        def android = project.android
        def globalScope = android.globalScope
        def gradle = project.gradle
        def set = ArtifactUtils.getAllArtifacts(
             new VariantScopeImpl(globalScope,
                new TransformManager(project,
                                    null,
                                    null),
                                    variant.variantData),
             AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH,
             null,
             BuildMappingUtils.computeBuildMapping(gradle)
        )
        println "${variant.name.capitalize()} all dependencies.size=${set.size()}" 
        set.eachWithIndex {artifact, idx->
                    def componentIdentifier = artifact.componentIdentifier
                    if (componentIdentifier.displayName.contains(':')) {
                        def modulerIdentifier = componentIdentifier.moduleIdentifier
                        def group = modulerIdentifier.group
                        def name = modulerIdentifier.name
                        def version = componentIdentifier.version
                        println "${idx} : ${componentIdentifier.displayName}"
                    } else {
                          println "${idx} -> ${artifact.artifactFile}"
                    }
        }
}

结果:

sodino Macbook-pro: MyApplication  sodino$: ./gradlew showAllDependenciesDebug

> Configure project :app

> Task :app:showAllDependenciesDebug
Debug all dependencies.size=65
0 -> /Users/sodino/AndroidStudioProjects/MyApplication3/app/libs/sodino.xiamen.FJ.aar
1 : org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.41
2 : org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
3 : androidx.navigation:navigation-ui-ktx:2.1.0
4 : androidx.navigation:navigation-ui:2.1.0
5 : com.google.android.material:material:1.0.0
6 : androidx.appcompat:appcompat:1.1.0
7 : androidx.core:core-ktx:1.1.0
8 : androidx.legacy:legacy-support-v4:1.0.0
9 : androidx.constraintlayout:constraintlayout:1.1.3
10 : androidx.navigation:navigation-fragment-ktx:2.1.0
11 : androidx.navigation:navigation-fragment:2.1.0
12 : androidx.lifecycle:lifecycle-extensions:2.1.0
13 : androidx.navigation:navigation-runtime-ktx:2.1.0
14 : androidx.fragment:fragment-ktx:1.1.0
15 : androidx.activity:activity-ktx:1.0.0
16 : androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0
17 : androidx.navigation:navigation-common-ktx:2.1.0
18 : org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1
19 : org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1
20 : org.jetbrains.kotlin:kotlin-stdlib:1.3.41
21 : androidx.fragment:fragment:1.1.0
22 : androidx.appcompat:appcompat-resources:1.1.0
23 : androidx.recyclerview:recyclerview:1.0.0
24 : androidx.legacy:legacy-support-core-ui:1.0.0
25 : androidx.drawerlayout:drawerlayout:1.0.0
26 : androidx.media:media:1.0.0
27 : androidx.legacy:legacy-support-core-utils:1.0.0
28 : androidx.transition:transition:1.0.1
29 : androidx.viewpager:viewpager:1.0.0
30 : androidx.loader:loader:1.0.0
31 : androidx.navigation:navigation-runtime:2.1.0
32 : androidx.activity:activity:1.0.0
33 : androidx.vectordrawable:vectordrawable-animated:1.1.0
34 : androidx.vectordrawable:vectordrawable:1.1.0
35 : androidx.coordinatorlayout:coordinatorlayout:1.0.0
36 : androidx.slidingpanelayout:slidingpanelayout:1.0.0
37 : androidx.customview:customview:1.0.0
38 : androidx.swiperefreshlayout:swiperefreshlayout:1.0.0
39 : androidx.asynclayoutinflater:asynclayoutinflater:1.0.0
40 : androidx.navigation:navigation-common:2.1.0
41 : androidx.core:core:1.1.0
42 : androidx.cursoradapter:cursoradapter:1.0.0
43 : androidx.cardview:cardview:1.0.0
44 : androidx.lifecycle:lifecycle-process:2.1.0
45 : androidx.lifecycle:lifecycle-service:2.1.0
46 : androidx.lifecycle:lifecycle-runtime:2.1.0
47 : androidx.lifecycle:lifecycle-livedata:2.1.0
48 : androidx.lifecycle:lifecycle-livedata-core:2.1.0
49 : androidx.arch.core:core-runtime:2.1.0
50 : androidx.savedstate:savedstate:1.0.0
51 : androidx.arch.core:core-common:2.1.0
52 : androidx.lifecycle:lifecycle-common:2.1.0
53 : androidx.lifecycle:lifecycle-viewmodel:2.1.0
54 : androidx.versionedparcelable:versionedparcelable:1.1.0
55 : androidx.collection:collection:1.1.0
56 : androidx.documentfile:documentfile:1.0.0
57 : androidx.localbroadcastmanager:localbroadcastmanager:1.0.0
58 : androidx.print:print:1.0.0
59 : androidx.interpolator:interpolator:1.0.0
60 : androidx.annotation:annotation:1.1.0
61 : androidx.constraintlayout:constraintlayout-solver:1.1.3
62 : org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.1
63 : org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
64 : org.jetbrains:annotations:13.0

BUILD SUCCESSFUL in 0s
于 2019-11-30T13:13:07.613 回答
-1

这是一篇旧文章,但我认为值得在单/多 gradle 项目中展示如何做到这一点:

如果您不想与 Gradle 服务器共享敏感信息,您可以使用Gradle 扫描或使用内置的dependencyReport 任务。

在单个项目中,可以直接使用:

./gradlew dependencyReport

在多个项目中,您可以对每个子项目使用相同的调用:

allprojects {
   ...
   tasks.register("allDeps",DependencyReportTask) {}
   ...
}
于 2019-12-19T18:55:52.307 回答