【问题标题】:Get dependencies from resolve configuration on Gradle 4.1从 Gradle 4.1 上的解析配置中获取依赖项
【发布时间】:2017-12-01 10:39:36
【问题描述】:

在 Gradle 的早期版本的 android 插件上,我可以通过我自己的任务,使用以下方式获取 jar 依赖项的路径:

android.libraryVariants.all { variant ->
    task "copyDependencies${variant.name.capitalize()}"(type: Copy) {
        configurations.compile.files().each { dependency ->
            from dependency.path
        }
        into project.projectDir.path + "/build/libs/${variant.name}"
    }
}

但是在这个插件的最新版本中,compile pass to deprecated 并且他们引入了apiimplementation 配置,所以当我尝试使用之前的代码时,gradle 说:

不允许直接解析配置'api'

对引入的这一新变化有何建议?


更新

我有一个依赖列表和过滤器用于执行此操作的配置:

android.libraryVariants.all { variant ->

    task "copyDependencies${variant.name.capitalize()}"(type: Copy) {
        from {
            variant.getCompileConfiguration().files().each { dependency ->
                configurations.api.getDependencies().each { configDep ->
                    if (dependency.name.contains(configDep.name)) {
                        from dependency.path
                    }
                }
            }
        }
        into project.projectDir.path + "/build/libs/${variant.name}"
    }
}

但是这个解决方案仍然存在问题,当项目 B 依赖于项目 A 时,就会上瘾。定义了这个任务后,Gradle 都不会构建。

【问题讨论】:

    标签: android gradle


    【解决方案1】:

    最后,我找到了从配置中复制依赖的解决方案:

    android.libraryVariants.all { variant ->
        task "copyDependencies${variant.name.capitalize()}"() {
            outputs.upToDateWhen { false }
            doLast {
                println "Executing from in copyDependencies${variant.name.capitalize()}"
                variant.getCompileConfiguration().getAllDependencies().each { dependency ->
                    configurations.api.getDependencies().each { configDep ->
                        if (dependency.name.contains(configDep.name)) {
                            println "Dependency detected: " + dependency.name
                            variant.getCompileClasspath().each { fileDependency ->
                                if (fileDependency.absolutePath.contains(dependency.name)) {
                                    println fileDependency.absolutePath
                                    copy {
                                        from fileDependency.absolutePath
                                        into project.projectDir.path + "/build/intermediates/bundles/${variant.name}/libs"
                                        exclude '**/classes.jar'
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      请参阅用户指南的chapter 48.4 以查找所有可用配置以及要使用和/或解决的配置。

      【讨论】:

      • 我知道我不能用这个配置解决它,我想知道的是如何获取每个标有'api'配置的依赖项的路径。我需要这项任务继续工作。
      • 你有没有看我的链接?我认为apiElements 是你所追求的,那里有描述。
      猜你喜欢
      • 2021-03-21
      • 2016-10-09
      • 1970-01-01
      • 2020-11-10
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多