美文网首页
Gradle依赖树及其简单应用

Gradle依赖树及其简单应用

作者: Parallel_Lines | 来源:发表于2019-04-11 12:16 被阅读0次

    Bolg - 依赖树及简单应用

    通过查看依赖树可以解决使用依赖时的常见问题。

    通常我们在Build.gradle中看到的是直接依赖,不包括传递依赖,想看到所有依赖有如下几种方式。

    Gradle View

    GradleView是一款查看依赖的插件。

    优点:
    可以方便快速的查看项目中的所有依赖。

    缺点:
    依赖不是以树的形式给出,无法得出传递关系。

    集成方式:
    Preferences -- Plugins -- Browser repositories -- 搜索 Gradle View 并重启 AS。

    使用命令

    控制台 Terminal 输入:

    ./gradlew app:dependencies
    

    其中 app 是 module 的名字。建议使用如下命令筛选:

    ./gradlew app:dependencies --configuration compile
    

    常见用途

    我们经常在项目中引入多个依赖库,其中多个依赖库可能又传递依赖了同一个依赖的不同版本。

    这种情况下,通常会默认使用较新的版本,并给出一个警告。

    示例

    项目中如下依赖:

        implementation 'com.xxx.push:core:1.0.0'
        implementation 'com.mcxiaoke.volley:library:1.0.18'
    

    其中 'com.xxx.push:core:1.0.0' 又依赖了 'com.mcxiaoke.volley:library:1.0.19',通过 Gradle View 查看真实依赖:

    img.png

    说明默认使用较新版本。

    通常情况下会有警告,推荐我们使用同一版本,否则会有Crash风险。如果我们想强制项目使用同一指定版本,如使用'com.mcxiaoke.volley:library:1.0.18' ,可以如下在build.gradle中配置:

    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            //统一版本号
            if (requested.group == 'com.mcxiaoke.volley') {
                if (requested.name.startsWith("library")) {
                    details.useVersion '1.0.18'
                }
            }
        }
    }
    

    通过命令行查看依赖树:

    img.png

    可以看到,'com.xxx.push:core:1.0.0' 中的 Volley 依赖被强制指定到了1.0.18版本,项目强制使用了同一个指定版本。

    相关文章

      网友评论

          本文标题:Gradle依赖树及其简单应用

          本文链接:https://www.haomeiwen.com/subject/ffhtwqtx.html