0
点赞
收藏
分享

微信扫一扫

Android 重构 | 统一管理 Gradle 依赖版本


文章目录

  • ​​前言​​
  • ​​拆分 build,统一管理​​
  • ​​The end​​
  • ​​参考资料​​

一生清贫怎敢入繁华,俩袖清风怎敢误佳人。

Android 重构 | 统一管理 Gradle 依赖版本_androidd

前言

重构书中,有这么一句话:

  • 产品不死,重构不止。

好代码,总是要经历多个阶段,从匆忙赶工上线,到慢慢细致打磨,折腾的过程,美好的结果。

经历过的项目,大部分都是一个 app 包下包罗万象,而今借此机会,从单一模块要逐渐演变,第一步,模块化搞起~

经过瞎折腾后,目前结构如下:

  • Pro
  • app:主 module
  • helper:帮助类(针对系统级别)以及工具类
  • weight:自定义 View 相关

经过一番折腾之后,的确比之前顺眼了许多,随之而来带来的问题是,每个 module 下都有对应的 build 文件,每个 build 文件都有一些基本的依赖库,想想日后还要分离各种 module,相关的管理怎么做?

拆分 build,统一管理

Step 1:项目根目录下创建 config.gradle

在此处,首先要明确共有依赖都有哪儿些:

  • Android 基本信息,例如编译 SDK 版本、版本信息等;
  • 基础依赖版本,例如 support 等;
  • 常用的一些依赖

So,此处抽取信息如下:

ext {

/**
* Android 基本配置项
*/
android = [
// 编译 SDK 版本
compileSdkVersion: 29,
// Gradle 编译项目工具版本
buildToolsVersion: "29.0.3",
// 最低兼容 Android 版本
minSdkVersion : 23,
// 最高兼容 Android 版本
targetSdkVersion : 29,
// 当前版本编号
versionCode : 1,
// 当前版本信息
versionName : "1.0"
]

/**
* 基础依赖版本 - 类似 support 库等
*/
def dependVersion = [
appcompat : "1.1.0",
constraintlayout: "1.1.3"
]

/**
* 常用依赖
*/
dependencies = [
// basic
"kotlinStdlibJdk7": "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${kotlin_version}",
"appcompat" : "androidx.appcompat:appcompat:${dependVersion.appcompat}",
"coreKtx" : 'androidx.core:core-ktx:1.2.0',
"constraintlayout": "androidx.constraintlayout:constraintlayout:${dependVersion.constraintlayout}",
// test
"junit" : 'junit:junit:4.12',
"testJunit" : 'androidx.test.ext:junit:1.1.1',
"testEspressoCore": 'androidx.test.espresso:espresso-core:3.2.0'
]
}

Step 2:为项目根目录下 build 添加依赖

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle"

buildscript {
// ...
}
// ...

Step 3:调整 module 中 build.gradle 原有使用方式

// ...

android {

def androidRoot = rootProject.ext.android

compileSdkVersion androidRoot.compileSdkVersion
buildToolsVersion androidRoot.buildToolsVersion

defaultConfig {
applicationId "your package name"
minSdkVersion androidRoot.minSdkVersion
targetSdkVersion androidRoot.targetSdkVersion
versionCode androidRoot.versionCode
versionName androidRoot.versionName

// ...
}
// ...
}

/**
* implementation:不会向下传递,仅在当前 module 生效; api:向下传递,所依赖的 module 均可使用
*/
dependencies {
def androidDependencies = rootProject.ext.dependencies

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation androidDependencies.kotlinStdlibJdk7
implementation androidDependencies.appcompat
implementation androidDependencies.coreKtx
implementation androidDependencies.constraintlayout
testImplementation androidDependencies.junit
androidTestImplementation androidDependencies.testJunit
androidTestImplementation androidDependencies.testEspressoCore

// 模块化部分导入部分

// helper
implementation project(path: ':helper')
// weight
implementation project(path: ':weight')

// 常用三方依赖导入部分
// ...
}

The end

ummm,爽了很多。

Android 重构 | 统一管理 Gradle 依赖版本_gradle_02


点滴积累,跟着鸡老大~

万一某天优秀了呢~

哈哈哈

参考资料

  • ​​添加构建依赖项​​
  • ​​Android从零撸美团(一) - 统一管理 Gradle 依赖 提取到单独文件中​​


举报

相关推荐

0 条评论