之前写过一篇Android gradle统一依赖版本:kotlin+buildSrc的集成使用,

两者的区别可以参照再见吧 buildSrc, 拥抱 Composing builds 提升 Android 编译速度

1.新建一个module(名字任意),删掉不必要的东西,保留以下内容即可:


1.DependencyManager.kt

package com.test.versionplugin/*** <pre>*     desc  : 如果数量少的话,放在一个类里面就可以,如果数量多的话,可以拆分为多个类* </pre>*/object BuildVersions {const val compileSdkVersion = 31const val buildToolsVersion = "30.0.3"const val minSdkVersion = 21const val targetSdkVersion = 31const val versionCode = 1const val versionName = "1.0"
}object Versions {const val kotlin = "1.5.0"const val retrofit = "2.9.0"const val appcompat = "1.1.0"const val material = "1.3.0"const val coreKtx = "1.2.0"const val constraintLayout = "2.0.4"const val recyclerview = "1.0.0"const val glide = "4.9.0"const val junit = "4.13.2"const val junitExt = "1.1.2"const val espressoCore = "3.3.0"
}object AndroidX {const val appcompat = "androidx.appcompat:appcompat:${Versions.appcompat}"const val coreKtx = "androidx.core:core-ktx:${Versions.coreKtx}"const val constraintLayout ="androidx.constraintlayout:constraintlayout:${Versions.constraintLayout}"const val recyclerview = "androidx.recyclerview:recyclerview:${Versions.recyclerview}"}object Kt {const val stdlibJdk = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"const val stdlibJdk8 = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlin}"
}object Retrofit {const val retrofit = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"const val gson = "com.squareup.retrofit2:converter-gson:${Versions.retrofit}"
}object Depend {const val junit = "junit:junit:${Versions.junit}"const val androidTestJunit = "androidx.test.ext:junit:${Versions.junitExt}"const val espressoCore = "androidx.test.espresso:espresso-core:${Versions.espressoCore}"const val material = "com.google.android.material:material:${Versions.material}"// glideconst val glide_runtime = "com.github.bumptech.glide:glide:${Versions.glide}"const val glide_compiler = "com.github.bumptech.glide:compiler:${Versions.glide}"}

2.VersionPlugin

package com.test.versionpluginimport org.gradle.api.Plugin
import org.gradle.api.Project/*** <pre>*     desc  : 如果出现红色警告可以忽略,不会影响项目的编译和运行* </pre>*/
class VersionPlugin : Plugin<Project> {override fun apply(project: Project) {}companion object {}
}

3.build.gradle

buildscript {repositories {jcenter()mavenCentral()}dependencies {// 因为使用的 Kotlin 需要需要添加 Kotlin 插件classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"}
}apply plugin: 'kotlin'
apply plugin: 'java-gradle-plugin'repositories {// 需要添加 jcenter 否则会提示找不到 gradlePluginjcenter()mavenCentral()
}dependencies {implementation gradleApi()implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32"}compileKotlin {kotlinOptions {jvmTarget = "1.8"}
}
compileTestKotlin {kotlinOptions {jvmTarget = "1.8"}
}gradlePlugin {plugins {version {// 在 app 模块需要通过 id 引用这个插件id = 'com.test.versionplugin'// 实现这个插件的类的路径implementationClass = 'com.test.versionplugin.VersionPlugin'}}
}

上面的id 以及implementationClass 要写实际的路径包名,不然找不到。

2.项目根目录下的settings.gradle

需要使用 includeBuild

rootProject.name = "Composing builds demo"
include ':app'
includeBuild ("versionPlugin")

3.根目录下的build.gradle

注意plugins 的位置,需要放到buildscript 后面,前面不能有 apply plugin 这种,不然就报错。

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {ext.kotlin_version = "1.5.0"repositories {google()mavenCentral()}dependencies {classpath "com.android.tools.build:gradle:4.2.1"classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}
}/*** 如果在多项目构建中,可能想把插件应用到子项目中,apply false 来告诉 Gradle 不要应用插件** plugins {} 默认的行为是解析和应用插件*/
plugins {// 这个 id 就是在 versionPlugin 文件夹下 build.gradle 文件内定义的idid "com.test.versionplugin" apply false
}allprojects {repositories {google()mavenCentral()jcenter() // Warning: this repository is going to shut down soon}
}subprojects { subproject ->// 默认应用所有子项目中apply plugin: 'com.test.versionplugin'// 如果想应用到某个子项目中,可以通过 subproject.name 来判断应用在哪个子项目中// subproject.name 是你子项目的名字,示例如下// 官方文档地址:https://guides.gradle.org/creating-multi-project-builds/#add_documentationif (subproject.name == "app") {apply plugin: 'com.android.application'apply plugin: 'kotlin-android'}
}task clean(type: Delete) {delete rootProject.buildDir
}

4.app下的build.gradle使用:

plugins {//    id 'com.android.application'
//    id 'kotlin-android'id 'kotlin-kapt'
}
import com.test.versionplugin.*
android {compileSdkVersion BuildVersions.compileSdkVersionbuildToolsVersion BuildVersions.buildToolsVersiondefaultConfig {applicationId "com.ssq.composingbuildsdemo"minSdkVersion BuildVersions.minSdkVersiontargetSdkVersion BuildVersions.targetSdkVersionversionCode BuildVersions.versionCodeversionName BuildVersions.versionNametestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}kotlinOptions {jvmTarget = '1.8'}
}dependencies {implementation Kt.stdlibJdkimplementation AndroidX.coreKtximplementation AndroidX.appcompatimplementation Depend.materialimplementation AndroidX.constraintLayouttestImplementation Depend.junitandroidTestImplementation Depend.androidTestJunitandroidTestImplementation Depend.espressoCore//glideimplementation Depend.glide_runtimekapt Depend.glide_compiler//retrofitimplementation Retrofit.retrofitimplementation Retrofit.gsonimplementation AndroidX.recyclerview
}

Android gradle统一依赖版本:Composing builds相关推荐

  1. Android Studio统一依赖管理Composing builds

    glideVersion : "4.11.0", okhttpVersion : "3.11.0", retrofitVersion : "2.3.0 ...

  2. Android Studio统一依赖管理Composing builds,2021最新网易Android面经

    支持Android Studio的代码补全,以下演示示例图来自于网络 使用方式可参考:Kotlin + buildSrc for Better Gradle Dependency Management ...

  3. 统一依赖管理Composing builds

    背景 在我们的AS项目中,经常引用多个Module,多人参与项目开发,这种背景下,我们会时常遇到版本冲突问题,出现不同的compileSdkVersion等,导致我们的包体变大,项目运行时间变长,所以 ...

  4. AndroidStudio使用进阶二:搭建自己的maven私服,并使用Gradle统一依赖管理

    前言: 最近我们老大组织了我们软件团队开了一个小会,说了一些存在的问题,平时在技术上的交流还是比较少的,尤其是在不同的项目之间的开发人员,然而经过这次会议我突然发现,我的缺陷不仅是在基础的能力上,还有 ...

  5. 【gradle Composing builds】gradle依赖管理Composing builds之杂症-快速同步

    Composing builds杂症-快速同步 问题描述 作为一个有强迫症的人,在使用Composing builds后一直耿耿于怀,第一次打开项目后,Ctrl+左键点击定义的依赖不能跳转,build ...

  6. Android Gradle排除依赖模块的某个类

    Gradle怎么排除所依赖的模块的某各类 其实我觉得在很多的情况下都会碰到这个问题,比如你的老大写了个BaseModule,里面有很多东西,你就没必要造轮子了,但是其实你这个项目中用不到这么多,那就没 ...

  7. 记录每天学习的新知识:Composing builds

    Composing builds 一.啥是 Composing builds ?? 二.使用 1. 于项目中新建 Moudle,取名 VersionPlugin 2.重写 VersionPlugin ...

  8. Android Studio使用Composing builds统一依赖管理

    Composing builds 一.啥是 Composing builds ?? https://docs.gradle.org/current/userguide/composite_builds ...

  9. 【Android 修炼手册】Gradle 篇 -- Android Gradle Plugin 主要流程分析

    预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin 使用及开发 了解 android gradle plugin 的使用 看完本文可以达到什么程度 了解 andr ...

最新文章

  1. 把mysql部署在局域网的服务器上,远程连接mysql时报错误代码1130 Host ‘***.***.***.***’is not allowed to connect to this MySQL
  2. java:蓝桥杯 矩阵乘法
  3. redis之五大数据类型
  4. java freememory 单位_Runtime类中的freeMemory,totalMemory,maxMemory区别
  5. Pytorch代码函数笔记
  6. C# XML字符串与DataTable相互转换
  7. pymongo的使用
  8. 欠拟合和过拟合以及如何选择模型
  9. 华为云联合HarmonyOS发布智联生活行业加速器
  10. ArcGiS/ArcInfo/ArcEditor/ArcMap/ArcView的区别
  11. 1024程序员节:心疼被段子手黑得最惨的你们
  12. adb命令刷机vivox20_vivo手机锁屏密码忘了怎么办?vivo手机强制解锁的三种方法
  13. 从腾讯外包月薪5K到转岗正式员工月薪15K,这178天的心酸只有自己知道...
  14. 明翰英语教学系列之音标篇V0.2(持续更新)
  15. 降维分析:人类发展指数法(IFI法)
  16. 【百练】1103Hangover宿醉(c语言)
  17. 微信小程序用地理编码做地图标注
  18. oracle 动态声明变量_Oracle11g新特性之动态变量窥视
  19. 为什么要用RSocket
  20. Design Data-Intensive Applications 读书笔记三 查询语句

热门文章

  1. 干货分享 | 代谢组学数据分析,常见图形制作分享-百趣生物
  2. 【⏰亲】今天冬至,早些回家!
  3. 使用QT实现Mjpeg-streamer的客户端,采用单独的线程进行视频图片的获取
  4. uni-app H5打包上线流程
  5. 《MySQL安装流程详解》及《MySQL安装一直失败,重新安装显示已安装》
  6. 源码分析 There is no getter for property named '*' in 'class java.lang.String
  7. matlab实现直方图规定化
  8. 数据结构~07.栈和队列的基本概念
  9. 【网络通信 -- SIP 电话】项目实战记录 -- SIP 服务器 OPENSIPS 搭建测试与 SIP 客户端 PJSIP 编译安装测试
  10. An attempt was made to call the method javax.persistence.PersistenceContext.synchronization()