Android從零擼美團(一) – 統一管理 Gradle 依賴 提取到單獨檔案中

solocoder發表於2018-11-03

前言

從今天開始帶大家一起從零開始擼一個美團Android版App。
【從零擼美團】這個專題將持續更新,用以詳細記錄分享開發過程,歡迎關注。

原始碼地址:github.com/cachecats/L…

Android從零擼美團(二) – 仿美團下拉重新整理自定義動畫

Android從零擼美團(三) – Android多標籤tab滑動切換 – 自定義View快速實現高度定製封裝

Android從零擼美團(四) – 美團首頁佈局解析及實現 – Banner+自定義View+SmartRefreshLayout下拉重新整理上拉載入更多


專題的第一篇文章本來想按慣例講專案介紹、整體架構、程式碼規範之類的。但今天有點躁動,不想講那麼正經深奧的東西,定的最低計劃又是週三和週日每週兩更,所以就暫且講構建專案依賴的小技巧吧。專案架構稍後會補上~

正文

一、為什麼要把專案依賴抽取單獨管理

如果專案比較小,架構簡單隻有一個 build.gradle 檔案那完全不必將依賴抽離出來,因為整個系統構建好還是要費一些精力的。不是一直喊避免過度設計嗎?

但是如果按照著名的 Android-CleanArchitecture (github.com/android10/A…) 介紹的架構對專案進行分層,整個專案會有好幾個 module

在這裡插入圖片描述

先貼出 Clean architecture 的架構圖感受下~

基於這個分層思想,我的專案結構是這樣的:

在這裡插入圖片描述

專案一共分為四層
common: 整個依賴的最底層,抽取出其他層共有的程式碼。比如 RxJava的封裝,工具類的封裝等。
data: 資料層,網路資料、資料庫中資料處理層。
domin: 這層應該是純 Java 程式碼,從 data 層出來的資料要經過 domin 轉換一下,UI層 app 引用的實體都是 domin 層的。用這種方法來隔離網路和資料庫資料變化對程式碼的影響。
app: 應該叫 presentation 表現層,覺得這名字太長還是 app 看著舒服~ 這層就是正常的 Android 程式碼。

這裡對 Clean architecture 做了一定的修改,在實踐中感覺這樣分層開發更順手。
具體的架構解析及每一層具體該怎麼寫請關注後面的文章,以後都會詳細講解。

重回剛才的問題,為什麼要抽取依賴統一管理?

有四個 module 就有四個 build.gradle,並且依賴很多都是重複的,分開寫在四個不同的檔案不利於後期維護升級。
想想如果要更改某個依賴的版本,得開啟四個檔案挨個改,多麻煩。
所以將可變的依賴抽離出來統一管理是很有必要的。

二、提取方法

下面就是具體的操作方法。
把大象裝進冰箱需要幾步?

三步走:

1、建立管理依賴的檔案

本專案中在與 app , data 同級的根目錄下建立了資料夾 buildsystem,然後在這個資料夾下建立檔案 dependences.gradle。目錄結構如下圖:

在這裡插入圖片描述

dependences.gradle 程式碼:

ext {

    //Android
    androidBuildToolsVersion = "27.0.3"
    androidMinSdkVersion = 18
    androidTargetSdkVersion = 27
    androidCompileSdkVersion = 27

    //Libraries
    recyclerViewVersion = "27.0.2"
    rxjava2Version = "2.1.8"
    rxandroidVersion = "2.0.1"
    daggerVersion = "2.14.1"
    glideVersion = "4.5.0"
    butterKnifeVersion = "8.8.1"
    bannerVersion = "1.4.10"
    loggerVersion = "2.1.1"
    baseRecyclerViewAdapterHelperVersion = "2.9.30"
    dbflowVersion = "4.2.4"

    app = [
            recyclerView                 : "com.android.support:recyclerview-v7:${recyclerViewVersion}",
            rxjava                       : "io.reactivex.rxjava2:rxjava:${rxjava2Version}",
            rxandroid                    : "io.reactivex.rxjava2:rxandroid:${rxandroidVersion}",
            dagger                       : "com.google.dagger:dagger:${daggerVersion}",
            daggerCompiler               : "com.google.dagger:dagger-compiler:${daggerVersion}",
            glide                        : "com.github.bumptech.glide:glide:${glideVersion}",
            glideCompiler                : "com.github.bumptech.glide:compiler:${glideVersion}",
            buterKnife                   : "com.jakewharton:butterknife:${butterKnifeVersion}",
            butterKnifeCompiler          : "com.jakewharton:butterknife-compiler:${butterKnifeVersion}",
            banner                       : "com.youth.banner:banner:${bannerVersion}",
            logger                       : "com.orhanobut:logger:${loggerVersion}",
            baseRecyclerViewAdapterHelper: "com.github.CymChad:BaseRecyclerViewAdapterHelper:${baseRecyclerViewAdapterHelperVersion}",
            dbflowProcessor              : "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflowVersion}",
            dbflowCore                   : "com.github.Raizlabs.DBFlow:dbflow-core:${dbflowVersion}",
            dbflow                       : "com.github.Raizlabs.DBFlow:dbflow:${dbflowVersion}",
            dbflowRx2                    : "com.github.Raizlabs.DBFlow:dbflow-rx2:${dbflowVersion}",
    ]

}
複製程式碼

版本號和具體依賴地址也分開了,更加便於管理。
其中除了依賴的地址,所有的變數名都是隨便取的。app 對應於專案module app 裡的依賴,當然還有 datadomincommon 三個 module 的依賴,宣告方式跟 app 一樣,只是我還沒用到所以沒寫。

2、宣告建立的檔案

在專案的 build.gradle 第一行加上這行程式碼:
apply from: "buildsystem/dependences.gradle"

在這裡插入圖片描述

宣告之後即可在各個 module 中的 build.gradle 中愉快的引用啦。

3、在具體位置引用

開啟 app 下的 build.gradle ,在需要用到的地方先宣告一個變數:

def appDependence = rootProject.ext.app
複製程式碼

rootProject 是什麼意思我還不太明白,猜測應該是取到專案的根目錄。
還記得 dependences.gradle 檔案最外層是 ext 節點嗎? rootProject.ext 相當於取到了我們提取檔案的根節點,rootProject.ext.app 即取到了 app 節點宣告的陣列。然後再將它賦值給 appDependence 變數。

接下來就可以放心使用啦

implementation appDependence.recyclerView
implementation appDependence.rxjava
複製程式碼

貼出 app/build.gradle 程式碼:

apply plugin: `com.android.application`

android {
    compileSdkVersion rootProject.ext.androidCompileSdkVersion
    buildToolsVersion rootProject.ext.androidBuildToolsVersion
    defaultConfig {
        applicationId "com.cachecats.meituan"
        minSdkVersion rootProject.ext.androidMinSdkVersion
        targetSdkVersion rootProject.ext.androidTargetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(`proguard-android.txt`), `proguard-rules.pro`
        }
    }

    lintOptions {
        abortOnError false
    }

    //支援Java8特性
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {

    def appDependence = rootProject.ext.app

    implementation project(`:common`)
    implementation project(`:data`)
    implementation project(`:domin`)
    implementation fileTree(include: [`*.jar`], dir: `libs`)
    implementation `com.android.support:appcompat-v7:27.0.2`
    implementation `com.android.support.constraint:constraint-layout:1.0.2`
    testImplementation `junit:junit:4.12`
    androidTestImplementation `com.android.support.test:runner:1.0.1`
    androidTestImplementation `com.android.support.test.espresso:espresso-core:3.0.1`

    //denpendence
    implementation appDependence.recyclerView
    implementation appDependence.rxjava
    implementation appDependence.rxandroid
    implementation appDependence.dagger
    annotationProcessor appDependence.daggerCompiler
    implementation appDependence.glide
    annotationProcessor appDependence.glideCompiler
    implementation appDependence.buterKnife
    annotationProcessor appDependence.butterKnifeCompiler
    implementation appDependence.banner
    implementation appDependence.logger
    implementation appDependence.baseRecyclerViewAdapterHelper
    implementation appDependence.dbflowCore
    implementation appDependence.dbflowRx2
    implementation appDependence.dbflow
    annotationProcessor appDependence.dbflowProcessor

}

複製程式碼

好啦,以上就是如何統一管理 Android 專案的 Gradle 依賴。
請忽略配圖的解析度和本地專案地址不一樣,這篇文章是在三臺電腦上完成的……


整個專案的原始碼都在 Github 上,可以隨時檢視下載。
原始碼地址:github.com/cachecats/L…
注:由於專案會一直更新,後期可能會對之前的程式碼進行重構。如發現原始碼中程式碼跟文章中不一致,以原始碼為準。重構之後我會盡量及時修改部落格中的相關內容,與原始碼保持一致。

如果覺得專案不錯, 順手在 Github 上點個 star 鼓勵一下唄~

相關文章