手把手教你搭建android模塊化項(xiàng)目框架(一)選擇篇

本系列旨在幫助android新人搭建屬于自己的項(xiàng)目框架。由于篇幅較長(zhǎng),本人時(shí)間有限,因此更新系列可能周期較長(zhǎng)。
本篇搭建框架適用于中大型項(xiàng)目。

經(jīng)典框架選取

  • 總模式:mvvm-databinding
  • 圖片加載:glide
  • 事件總線:kotlin.flow或eventbus,本篇選用kotlin.flow擼出自己的事件總線
  • 線程調(diào)度:kotlin.CoroutineScope
  • 網(wǎng)絡(luò)請(qǐng)求:retrofit + kotlin.flow
  • 權(quán)限請(qǐng)求:后續(xù)文章教大家擼出自己的權(quán)限工具
  • 數(shù)據(jù)庫(kù):room
  • 分頁庫(kù):paging
  • 下載工具:aira or okdownload
  • json解析:Gson
  • 圖片壓縮:luban
  • 模塊化路由:arouter or WMRouter ,本篇為了讓大家更加理解模塊化路由工作方式,因此會(huì)使用path簡(jiǎn)單封裝一個(gè)自己的router。
  • 動(dòng)畫等:lottie or svga or pag不過本篇不教大家如何使用該框架功能。因?yàn)閱我坏墓ぞ卟⒎强蚣苤攸c(diǎn)功能。
  • 其他:core-ktx,lifecycle,androidx系列庫(kù)等。
  • 依賴注入:hilt(可選),依賴注入是否需要使用,取決于預(yù)估的項(xiàng)目規(guī)格大小,本篇拋磚引玉,會(huì)帶入一點(diǎn)hilt的使用,不過并非重點(diǎn),可酌情選取使用。
  • 基礎(chǔ)存儲(chǔ):mmkv(可選),由于room有一定的性能問題,所以性能優(yōu)化后的部分常用鍵值對(duì)可以選用mmkv使用。
  • bug采集:bugly 本篇不接入,正式項(xiàng)目可選取合適自己的采集工具。
  • 埋點(diǎn)采集:同上。
    上述基本描寫了目前大多數(shù)項(xiàng)目使用的三方及jetpack框架,后續(xù)可能補(bǔ)充其他選擇。如:lifecycle等,不過由于是一方庫(kù),所以不過多描述。

下面開始引入:
創(chuàng)建android項(xiàng)目這一步不多說,但是為了引入項(xiàng)目的一致性,我們創(chuàng)建deps.gradle文件,將項(xiàng)目中所有引入的庫(kù)都放在該文件下,避免各個(gè)模塊引入的庫(kù)版本不一致。

rootProject.ext.compileSdkVersion = 33
rootProject.ext.targetSdkVersion = 33

rootProject.ext.minSdkVersion = 21

rootProject.ext.GLIDE_VERSION = "4.13.2"
rootProject.ext.lifecycle_version = "2.5.1"
rootProject.ext.roomVersion = '2.5.2'

rootProject.ext.deps = [

        /////////////////////////// Android官方庫(kù)  //////////////////////////////////////////////////
        // android
        androidCoreKtx          : 'androidx.core:core-ktx:1.9.0',
        androidAnnotations      : 'androidx.annotation:annotation:1.6.0',
        androidAppCompat        : 'androidx.appcompat:appcompat:1.6.1',
        androidFragmentKtx      : 'androidx.fragment:fragment-ktx:1.5.5',
        androidActivityKtx      : "androidx.activity:activity-ktx:1.6.1",

        androidLifecycleViewModel : "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.ext.lifecycle_version",
        androidLifecycleRuntime   : "androidx.lifecycle:lifecycle-runtime-ktx:$rootProject.ext.lifecycle_version",


        // widget
        androidCardView         : 'androidx.cardview:cardview:1.0.0',
        androidRecyclerView     : 'androidx.recyclerview:recyclerview:1.2.1',
        androidPercent          : 'androidx.percentlayout:percentlayout:1.0.0',
        androidGridLayout       : 'androidx.gridlayout:gridlayout:1.0.0',
        androidConstraintLayout : 'androidx.constraintlayout:constraintlayout:2.0.4',
        androidPalette          : 'androidx.palette:palette-ktx:1.0.0',
        androidSwipe            : 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0',

        androidDesignLibrary    : 'com.google.android.material:material:1.4.0',
        androidFlexbox          : "com.google.android.flexbox:flexbox:3.0.0",

        // emoji2
        emoji2                  : "androidx.emoji2:emoji2:1.1.0",
        emoji2_views            : "androidx.emoji2:emoji2-views:1.1.0",
        emoji2_helper           : "androidx.emoji2:emoji2-views-helper:1.1.0",

        /////////////////////////// 第三方開源庫(kù)  ///////////////////////////////////////////////////
        // di 依賴注入
        dagger                  : "com.google.dagger:dagger:2.44.2",
        daggerCompiler          : "com.google.dagger:dagger-compiler:2.44.2",

        // http
        okhttp                  : "com.squareup.okhttp3:okhttp:3.12.13",
        okio                    : "com.squareup.okio:okio:1.17.4",
        okhttpLoggingInterceptor: "com.squareup.okhttp3:logging-interceptor:3.12.13",
        networkConnectionClass  : "com.facebook.network.connectionclass:connectionclass:1.0.1",
        retrofit2AdapterRxJava  : "com.squareup.retrofit2:adapter-rxjava2:2.6.4",
        retrofit                : "com.squareup.retrofit2:retrofit:2.6.4",
        converter_gson          : "com.squareup.retrofit2:converter-gson:2.6.4",

        //json
        gson                    : "com.google.code.gson:gson:2.8.0",

        // glide
        glide                   : "com.github.bumptech.glide:glide:$rootProject.ext.GLIDE_VERSION",
        glideCompiler           : "com.github.bumptech.glide:compiler:$rootProject.ext.GLIDE_VERSION",

        // kv store
        tencentMmkv             : "com.tencent:mmkv-static:1.2.16",
]

由于是粘貼至其他項(xiàng)目的,版本可能不是最新的。
然后將該文件引入至gradle入口處,這樣就可以在其他模塊引用了。文件如下圖


新建 BMP 圖像.png

插入代碼

plugins {
id 'com.android.application' version '8.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
//引入該文件
apply from: "$rootDir/deps.gradle"

至此,就可以在其他模塊中引入了,例如app.gradle中引入

dependencies {
    implementation rootProject.ext.deps.androidCoreKtx
    implementation rootProject.ext.deps.androidAppCompat
    implementation rootProject.ext.deps.androidDesignLibrary
}

本篇至此就結(jié)束了,至于gradle的更高級(jí)配置會(huì)在后續(xù)逐步說明。
項(xiàng)目地址
項(xiàng)目持續(xù)更新,每個(gè)步驟請(qǐng)查詢tag

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容