Skip to content

一款检查依赖配置是否冲突的 Gradle 插件 ​

背景介绍 ​

在平时的开发中,大家经常会遇到因为gradle的构建配置错误引起的各种奇奇怪怪的问题,每次梳理依赖关系都需要很长时间,为了更方便的找出项目中的依赖冲突,专门开发了这个插件。

使用方法: ​

在项目中添加插件 ​

  • 在项目根目录的build.gradle中添加插件仓库:

    groovy
    buildscript {
    	repositories {
    		maven { url "https://repo1.maven.org/maven2" }
    	}
    	dependencies {
    		classpath "com.bihe0832:GradleDependenciesCheck:1.0.2"
    	}
    }
  • 引入插件,并添加插件相关配置

    你可以选择在每个project都添加一份,也可以选择参考demo,在根build.gradle的subprojects中添加

    groovy
    subprojects {
    	/************************** 应用插件 Start ***********************/
    	//应用插件
    	apply plugin: 'com.bihe0832.gradleDependenciesCheck'
    	//配置插件相关配置
    	GradleDependenciesCheckConfig {
    		/**
    			* 检查结果提示类型:
    			*  1 以warning形式在命令行提示
    			*  2 直接编辑报错,并提示错误
    			*/
    		showResultType = 2
    	}
    	/************************** 应用插件 End ***********************/
    
    }
  • 将插件添加到其余系统任务中自动执行

    groovy
    subprojects {
    	afterEvaluate { Project project ->
    		project.getTasks().getByName('clean').dependsOn("checkGradleDependencies")
    		project.getTasks().getByName('preBuild').dependsOn("checkGradleDependencies")
    	}
    }

完整的插件使用方式,可以参考项目的事例Sample的根目录的build.gradle

运行插件 ​

  • 在IED右侧的Gradle窗口选择zixie,然后点击checkGradleDependencies

  • 直接在命令行运行./gradlew checkGradleDependencies

  • 直接运行已经添加了依赖的系统task

运行效果 ​

  • 在命令行运行效果如下:

    > Task :app:listGradleDependencies FAILED
    listGradleDependencies start ...
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':app:listGradleDependencies'.
    > [ERROR] org.jetbrains.kotlin:kotlin-stdlib has different version: 
    	1.3.0 found from: 
    			app:org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0
    			app:Sample:sdk:unspecified
    	1.2.71 found from: 
    			app:org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71
    ……
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings
    
    BUILD FAILED in 1s
    1 actionable task: 1 executed
    [ERROR] org.jetbrains.kotlin:kotlin-stdlib has different version: 
    	1.3.0 found from: 
    			app:org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0
    			app:Sample:sdk:unspecified
    	1.2.71 found from: 
    			app:org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71
    
    4:39:17 PM: Task execution finished 'listGradleDependencies'.

项目相关: ​

源码地址 ​

后续规划 ​

  • 检查所有依赖的最新版,然后推荐更新

  • 推荐把support等相关库都切换到了androidx

  • 对于一些需要版本对应的,提示对应。例如:databing compile依赖的版本号与Android Gradle Plugin版本保持一致等

参考及文章 ​