gradle中仓库repositories 与查看项目依赖dependency,Ctrl+鼠标左键跳转gradle源码

查看gradle源码(Ctrl+鼠标左键可跳转)

一、先将all改成bin

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

改为:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-bin.zip

二、打开一个build.gradle文件,
Android stuido 会提示
You can configure Gradle wrapper to use distribution with sources.
It will provide IDE with Gradle API/DSL documentation.

三、选择是apply即可。

gradle中仓库:

    repositories {
        google()
        maven { url "https://dl.google.com/dl/android/maven2/" }
        //
        maven { url 'https://maven.google.com' }

        
        jcenter()
        maven { url "https://jcenter.bintray.com/" }

        
        mavenCentral()
        maven { url "https://repo.maven.apache.org/maven2/" }
        //
        maven { url "https://repo1.maven.org/maven2/" }
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
        
        
        maven { url "https://jitpack.io" }

        
        mavenLocal()
    }

一、命令行直接查看gradle依赖:

注意:如果是 Linux / Mac 以下命令请直接用 ./ 前缀。

//命令格式: 
//gradlew 	模块名:dependencies

gradlew 	:app:dependencies 

gradlew -q  :app:dependencies

//只看compile相关的依赖
gradlew -q  :app:dependencies --configuration compile

//只看implementation相关的依赖
gradlew -q  :app:dependencies --configuration implementation

//将结果输出到当前目录下的log.txt文件
gradlew -q  :app:dependencies >log.txt

看结果其中:

//依赖项被忽略(前面列出)
(*) - dependencies omitted (listed previously)
//意味着前边已经有了,这里就不再使用(有可能冲突)


//配置未使用,不考虑
(n) - Not resolved (configuration is not meant to be resolved)

二、命令行利用网页查看gradle依赖:

gradlew build --scan
BUILD SUCCESSFUL in 4m 21s
56 actionable tasks: 56 executed

Publishing a build scan to scans.gradle.com requires 
accepting the Gradle Terms of Service defined at 
https://gradle.com/terms-of-service. 
Do you accept these terms? [yes, no]
yes	//输入yes

Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/xxxxxxx

打开链接会发送邮件查看扫描结果即可。

三、gradle view插件

在这里插入图片描述

测试一

retrofit2、okhttp3-integration中都含有OKHTTP的依赖,

implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.0.0'
implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.6.0'
implementation "com.github.bumptech.glide:okhttp3-integration:4.8.0"

gradle view的hierarchy:
在这里插入图片描述
命令行输出到日志:

gradlew -q :app:dependencies >log.txt

在这里插入图片描述

测试二

implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.0.0'
implementation(group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.6.0') {
    exclude group: "com.squareup.okhttp3"
}
implementation("com.github.bumptech.glide:okhttp3-integration:4.8.0") {
    exclude group: "com.squareup.okhttp3"
}

命令行输出到日志:

gradlew -q :app:dependencies >log.txt

在这里插入图片描述

Gradle检查和避免依赖冲突可以通过以下几种方法实现: 1. 使用Gradle依赖报告:Gradle提供了命令行工具来查看项目依赖树。通过运行`gradle dependencies`命令,你可以看到项目所有依赖项及其层次结构,这有助于你识别潜在的冲突。冲突通常会显示为“conflicts with”信息。 2. 使用依赖分析插件:可以使用如`gradle-dependency-analyzer`这样的第三方插件来更详细地分析项目依赖关系。这类插件提供了图形界面或报告,帮助开发者理解依赖的层次结构和潜在冲突。 3. 配置依赖冲突解决策略:在Gradle的构建脚本,可以通过声明冲突解决策略来指定如何处理特定的依赖冲突。例如,你可以在`build.gradle`文件使用`configurations`块来设置特定的冲突解决规则: ```groovy configurations { all*.exclude group: 'org.example', module: 'incompatible-library' } ``` 这段代码会从所有依赖排除`org.example`组下的`incompatible-library`模块。 4. 使用`resolutionStrategy`:对于Maven或Ivy依赖,可以在构建脚本的`repositories`或`dependencies`部分使用`resolutionStrategy`来定义冲突解决规则: ```groovy dependencies { implementation 'com.example:library:1.0' implementation 'com.example:another-library:1.0' resolutionStrategy { force 'com.example:library:1.0@jar' } } ``` 这会强制使用特定版本的`library`依赖,即使它与其他依赖有冲突。 5. 使用`dependencyInsight`任务:Gradle的`dependencyInsight`任务可以帮助你深入理解特定依赖项为何被选以及冲突的具体情况。这个任务可以提供关键信息,帮助你解决复杂的依赖冲突问题。 6. 使用Gradle Wrapper:确保使用相同版本的Gradle可以减少因为Gradle版本差异带来的依赖解析问题。 通过上述方法,你可以有效地检查和避免Gradle项目依赖冲突问题。不过,需要注意的是依赖管理是一个复杂的问题,可能需要综合运用多种策略才能达到最佳效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值