網站首頁 編程語言 正文
簡述
每個項目從新建開始我們或多或少都會導入各種依賴庫,如果項目中只有一個module的話,對于依賴庫版本的管理很容易,但是多個module的話,稍加不注意,很容易導入多個版本的庫甚至產生版本沖突,更新修改依賴庫也需要多處操作,所以我們需要對依賴庫版本進行統一管理。
傳統apply from的方式(也是我以前項目中使用)
為了對模塊進行統一管理,會在根目錄新建一個config.gradle文件或者在根目錄的build.gradle定義一些變量
ext { android = [ compileSdkVersion : 29, buildToolsVersion : "30.0.2", applicationId : "com.xionggouba.bearseller", minSdkVersion : 19, targetSdkVersion : 30, versionCode : 27, versionName : "3.9.1", defaultPublishConfig: 'release', publishNonDefault : true, multiDexEnabled : true, mapKey : 'c7e1ee468aa1bf8a6739', pushKey : '65aae199a0059eb1dbe7', pushChannel : 'developer-default', ] appid = [ app : "com.xionggouba.bearseller", login : "com.huitao.login", home : "com.huitao.home", webview : "com.huitao.webview", main : "com.huitao.main", productManager: "com.huitao.productmanager", personal : "com.huitao.personalcenter", map : "com.huitao.map", bluetooth : "com.huitao.bluetooth", push : "com.huitao.push", markketing : "con.huitao.marketing", printer : "com.huitao.printer" ] versions = [ "lifecycle_version": "2.2.0", "arch_version" : "2.1.0", "retrofit_version" : "2.6.2", "dialog_version" : "3.3.0", "glide_version" : "4.9.0", "hilt" : "2.28-alpha", "kotlin_version" : "1.4.10", "fragment_version" : "1.2.5", "room_version" : "2.2.6" ] architecture = [ "viewmodel" : "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions['lifecycle_version']}", "livedata" : "androidx.lifecycle:lifecycle-livedata-ktx:${versions['lifecycle_version']}", "lifecycleruntime" : "androidx.lifecycle:lifecycle-runtime-ktx:${versions['lifecycle_version']}", "savedstate" : "androidx.lifecycle:lifecycle-viewmodel-savedstate:${versions['lifecycle_version']}", // alternately - if using Java8, use the following instead of lifecycle-compiler "lifecyclecommon" : "androidx.lifecycle:lifecycle-common-java8:${versions['lifecycle_version']}", // Saved state module for ViewModel "viewmodelsavedstate": "androidx.lifecycle:lifecycle-viewmodel-savedstate:${versions['lifecycle_version']}", "lifecycleextentions": "androidx.lifecycle:lifecycle-extensions:${versions['lifecycle_version']}", "retrofit2" : "com.squareup.retrofit2:retrofit:${versions['retrofit_version']}", "gson" : "com.squareup.retrofit2:converter-gson:${versions['retrofit_version']}", "persistentcookiejar": "com.github.franmontiel:PersistentCookieJar:v1.0.1", "glide" : "com.github.bumptech.glide:glide:${versions['glide_version']}", "glidecompiler" : "com.github.bumptech.glide:compiler:${versions['glide_version']}", "oss" : "com.aliyun.dpa:oss-android-sdk:2.9.1", "luban" : "top.zibin:Luban:1.1.8" ] ]
在工程的根目錄build.gradle添加
apply from"config.gradle"
找一個東西就得靠搜索,逐一查找。
buildSrc方式
什么是buildSrc
當運行 Gradle 時會檢查項目中是否存在一個名為 buildSrc 的目錄。然后 Gradle 會自動編譯并測試這段代碼,并將其放入構建腳本的類路徑中, 對于多項目構建,只能有一個 buildSrc 目錄,該目錄必須位于根項目目錄中, buildSrc 是 Gradle 項目根目錄下的一個目錄,它可以包含我們的構建邏輯,與腳本插件相比,buildSrc 應該是首選,因為它更易于維護、重構和測試代碼
小結
buildSrc在近幾年時非常流行的,因為它共享 buildSrc 庫工件的引用,全局只有一個地方可以修改它,支持自動補全(這個很爽),支持跳轉。 但是他也有一個缺點,依賴更新將重新構建整個項目,這個不是很好。
Composing builds
什么是Composing builds
復合構建只是包含其他構建的構建. 在許多方面,復合構建類似于 Gradle 多項目構建,不同之處在于,它包括完整的 builds ,而不是包含單個 projects
- 組合通常獨立開發的構建,例如,在應用程序使用的庫中嘗試錯誤修復時
- 將大型的多項目構建分解為更小,更孤立的塊,可以根據需要獨立或一起工作
小結
這種方式擁有buildSrc的優點,同時依賴更新不用重新構建整個項目。
Composing builds項目實戰
在項目中新建一個model,用于管理配置信息
新建一個ConfigPlugin的類,不用具體的實現
class ConfigPlugin: Plugin<Project> { override fun apply(p0: Project) { } companion object{ } }
編輯configPluginmodel中的build.gradle文件
我的文件大致內容如下
buildscript { repositories { jcenter() } dependencies { // 因為使用的 Kotlin 需要需要添加 Kotlin 插件 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" } } apply plugin: 'kotlin' apply plugin: 'java-gradle-plugin' repositories { // 需要添加 jcenter 否則會提示找不到 gradlePlugin jcenter() google() } dependencies { implementation gradleApi() implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" } compileKotlin { kotlinOptions { jvmTarget = "1.8" } } compileTestKotlin { kotlinOptions { jvmTarget = "1.8" } } gradlePlugin { plugins { version { // 在 app 模塊需要通過 id 引用這個插件 id = 'com.umeshop.configplugin' // 實現這個插件的類的路徑 implementationClass = 'com.umeshop.configplugin.ConfigPlugin' } } }
修改根目錄的settings.gradle文件
項目目錄引入插件
在configPluginmodel中定義一個DependencyManager文件來管理一些第三方的依賴
在使用目錄導入
大致流程介紹完成
需要注意的地方
根目錄的settings.gradle的配置
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } rootProject.name = "jetpackDemo" include ':app'
有些版本是這樣的,建議刪除dependencyResolutionManagement的配置,在build.gradle中配置。大致是這樣的
allprojects { repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } }
還有一個就是依賴的修改
includeBuild("configPlugin")
是includeBuild標簽,不是include
總結
原文鏈接:https://juejin.cn/post/7049342239423594533
相關推薦
- 2023-02-14 Python利用Prim算法生成迷宮_python
- 2022-09-10 Python并發編程多進程,多線程及GIL全局解釋器鎖_python
- 2024-03-28 SpringBoot項目中的500錯誤
- 2023-03-29 PyTorch中grid_sample的使用及說明_python
- 2022-01-15 Es6中用Set去重
- 2022-04-25 老生常談C語言中指針的使用_C 語言
- 2022-04-08 深入理解Golang的反射reflect示例_Golang
- 2022-05-26 Flutter開發實現底部留言板_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支