Android 重構 | 統一管理 Gradle 依賴版本

一生清貧怎敢入繁華,倆袖清風怎敢誤佳人。

在這裏插入圖片描述

前言

重構書中,有這麼一句話:

  • 產品不死,重構不止。

好代碼,總是要經歷多個階段,從匆忙趕工上線,到慢慢細緻打磨,折騰的過程,美好的結果。

經歷過的項目,大部分都是一個 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,爽了很多。

在這裏插入圖片描述
點滴積累,跟着雞老大~

萬一某天優秀了呢~

哈哈哈

參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章