前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 重构 | 统一管理 Gradle 依赖版本

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

作者头像
贺biubiu
发布2020-05-22 15:50:07
1K0
发布2020-05-22 15:50:07
举报
文章被收录于专栏:HLQ_StruggleHLQ_Struggle
第85次推文

LZ-Says

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

前言

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

  • 产品不死,重构不止。

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

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

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

Pro

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

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

拆分 build,统一管理

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

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

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

So,此处抽取信息如下:

代码语言:javascript
复制
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 添加依赖

代码语言:javascript
复制
// 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 原有使用方式

代码语言:javascript
复制
// ...

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 依赖 提取到单独文件中
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 贺biubiu 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档