使用TOML統(tǒng)一組件化SDK版本

自從Gradle改變了Android的依賴管理方式后,要想集成第三方SDK就變得很容易。

比如我要集成RecyclerView,只需要這么一行代碼。

implementation("androidx.recyclerview:recyclerview:1.3.2")

如果是groovy,也只需要這么一行代碼。

implementation 'androidx.recyclerview:recyclerview:1.3.2'

如果要集成github上面的第三方SDK,也只需要在settings.kts中再多指定一個依賴管理倉庫的地址。

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

github上面的android庫最快捷的發(fā)布方式就是jitpack了。

當(dāng)項(xiàng)目使用了組件化以后,通常我們會定義一個config.gradle,像是這樣。

ext {
    // 是否單獨(dú)運(yùn)行某個module
    isModule = false
    signingConfig = [
            storePassword: 'xxx',
            keyAlias     : 'xxx',
            keyPassword  : 'xxx'
    ]

    android = [
            applicationId    : "com.sum.tea",
            // 使用exoPlayer編譯版本需要到33,而33中有bug,xml寫代碼沒有代碼提示
            compileSdk       : 33,
            buildToolsVersion: '32.0.0',
            minSdk           : 21,
            targetSdk        : 32,

            versionCode      : 1,
            versionName      : "1.0.1",
    ]

    dependVersion = [
            multidexVersion       : '2.0.1',
            retrofitVersion       : '2.9.0',
            retrofitGsonVersion   : '2.4.0',
            okhttp3LogVersion     : '3.11.0',
            gsonVersion           : '2.10.1',
            mmkvVersion           : '1.2.15',
            refreshVersion        : '2.0.5',
            glideVersion          : '4.15.0',
            flexboxVersion        : '3.0.0',
            aroutreApiVersion     : '1.5.2',
            arouterCompilerVersion: '1.5.2',
            tbssdkVersion         : '44132',
            exoPlayerVersion      : '2.18.5',
            permissionsVersion    : '0.12',
            roomVersion           : '2.5.0',
            rxjavaVersion         : '3.1.6',
            rxandroidVersion      : '3.0.2',
            cameraxVersion        : '1.3.0-alpha04',
    ]

    depsLibs = [
            coreKtx           : 'androidx.core:core-ktx:1.7.0',
            appcompat         : 'androidx.appcompat:appcompat:1.3.0',
            material          : 'com.google.android.material:material:1.4.0',
            constraintlayout  : 'androidx.constraintlayout:constraintlayout:2.0.4',
            liveDataKtx       : 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1',
            viewModelKtx      : 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1',
            navigationFragment: 'androidx.navigation:navigation-fragment-ktx:2.3.5',
            navigationUI      : 'androidx.navigation:navigation-ui-ktx:2.3.5',
            lifecycleCommon   : 'androidx.lifecycle:lifecycle-common:2.5.0-beta01',

            // room數(shù)據(jù)庫
            roomKtx           : "androidx.room:room-ktx:${dependVersion["roomVersion"]}",
            roomCompiler      : "androidx.room:room-compiler:${dependVersion["roomVersion"]}",

            junit             : 'junit:junit:4.13.2',
            extJunit          : 'androidx.test.ext:junit:1.1.3',
            espressoCore      : 'androidx.test.espresso:espresso-core:3.4.0',

            multidex          : "androidx.multidex:multidex:${dependVersion["multidexVersion"]}",
            // 網(wǎng)絡(luò)請求
            retrofit2         : "com.squareup.retrofit2:retrofit:${dependVersion["retrofitVersion"]}",
            retrofit2Gson     : "com.squareup.retrofit2:converter-gson:${dependVersion["retrofitGsonVersion"]}",
            // 日志打印
            loggingInterceptor: "com.squareup.okhttp3:logging-interceptor:${dependVersion["okhttp3LogVersion"]}",

            gson              : "com.google.code.gson:gson:${dependVersion["gsonVersion"]}",
//            xlog              : "com.tencent.mars:mars-xlog:1.2.5",
            // 數(shù)據(jù)存儲
            mmkv              : "com.tencent:mmkv:${dependVersion["mmkvVersion"]}",

            // 下拉刷新
            refreshLayout     : "io.github.scwang90:refresh-layout-kernel:${dependVersion["refreshVersion"]}",
            refreshHeader     : "io.github.scwang90:refresh-header-classics:${dependVersion["refreshVersion"]}",
            refreshFooter     : "io.github.scwang90:refresh-footer-classics:${dependVersion["refreshVersion"]}",

            // 圖片加載
            glide             : "com.github.bumptech.glide:glide:${dependVersion["glideVersion"]}",
            glideCompiler     : "com.github.bumptech.glide:compiler:${dependVersion["glideVersion"]}",

            // 流式布局
            flexbox           : "com.google.android.flexbox:flexbox:${dependVersion["flexboxVersion"]}",
//
            // 路由
            aroutreApi        : "com.alibaba:arouter-api:${dependVersion["aroutreApiVersion"]}",
            arouterCompiler   : "com.alibaba:arouter-compiler:${dependVersion["arouterCompilerVersion"]}",

            // 騰訊X5
            tbssdk            : "com.tencent.tbs:tbssdk:${dependVersion["tbssdkVersion"]}",

            // ExoPlayer播放器
            exoPlayer         : "com.google.android.exoplayer:exoplayer:${dependVersion["exoPlayerVersion"]}",

            // 相機(jī)
//            cameraX           : "androidx.camera:camera-camera2:${dependVersion["cameraxVersion"]}",

            // 權(quán)限處理
            rxPermission      : "com.github.tbruyelle:rxpermissions:${dependVersion["permissionsVersion"]}",
            rxjava            : "io.reactivex.rxjava3:rxjava:${dependVersion["rxjavaVersion"]}",
            rxandroid         : "io.reactivex.rxjava3:rxandroid:${dependVersion["rxandroidVersion"]}",
    ]
}

今天我們不用config.gradle的這種方式。那么toml就鐺鐺鐺鐺,閃亮登場了。

toml是什么

TOML 是一種旨在成為一個小規(guī)模、易于使用的語義化的配置文件格式,它被設(shè)計(jì)為可以無二義性的轉(zhuǎn)換為一個哈希表。 “TOML”這個名字是“Tom's Obvious, Minimal Language”的首字母略寫詞?!癟om”指它的作者Tom Preston-Werner。

toml官網(wǎng)

我們?nèi)ニ倬W(wǎng)瞧一瞧,toml.io/en/。這語法看起來好像蠻簡單的。是的,它是一個key-value的配置文件。類似的還有json、ini、properties和yaml配置。

gradle對toml的支持

那么我們要怎么用呢?前面都是廢話,從這里開始才是干貨。

dependencyResolutionManagement {
    // ...
    versionCatalogs {
        create("commonLibs") { from(files("${rootDir.path}/dep.toml")) }
    }
}

然后在項(xiàng)目根目錄創(chuàng)建一個名為dep.toml的文件,文件名不一定要跟我一樣。

[libraries]
dora = { module = "com.github.dora4:dora", version = "1.1.40"}
dora-apollo = { module = "com.github.dora4:dora-apollo-support", version = "1.3"}
apollo-compiler = { module = "com.github.lsxiao.Apollo:processor", version = "1.0.2"}
dora-glide = { module = "com.github.dora4:dora-glide-support", version = "1.3"}
dora-firebase = { module = "com.github.dora4:dora-firebase-support", version = "1.7"}
dora-pgyer = { module = "com.github.dora4:dora-pgyer-support", version = "1.0"}
dora-arouter = { module = "com.github.dora4:dora-arouter-support", version = "1.6"}
arouter-compiler = { module = "com.alibaba:arouter-compiler", version = "1.5.2"}
dcache = { module = "com.github.dora4:dcache-android", version = "2.0.12"}
dview-titlebar = { module = "com.github.dora4:dview-titlebar", version = "1.27"}
dview-colors = { module = "com.github.dora4:dview-colors", version = "1.0"}
dview-clearedittext = { module = "com.github.dora4:dview-clear-edittext", version = "1.0"}
dview-dialog-bottom = { module = "com.github.dora4:dview-bottom-dialog", version = "1.11"}
dview-dialog-loading = { module = "com.github.dora4:dview-loading-dialog", version = "1.4"}
dview-dialog-alert = { module = "com.github.dora4:dview-alert-dialog", version = "1.3"}

refresh-layout = { module = "io.github.scwang90:refresh-layout-kernel", version = "2.0.6"}
refresh-header = { module = "io.github.scwang90:refresh-header-classics", version = "2.0.6"}
refresh-footer = { module = "io.github.scwang90:refresh-footer-classics", version = "2.0.6"}
ktx-livedata =  { module = "androidx.lifecycle:lifecycle-livedata-ktx", version = "2.6.2" }
ktx-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version = "2.6.2" }

好小子,我感覺你在推廣自己的庫啊,而且我有證據(jù)!接下來在模塊的dependencies中的寫法就簡潔了,而且有自動代碼提示。

dependencies {
    implementation(commonLibs.dora)
    implementation(commonLibs.dora.apollo)
    kapt(commonLibs.apollo.compiler)
    implementation(commonLibs.dora.glide)
    implementation(commonLibs.dora.firebase)
    implementation(commonLibs.dora.pgyer)
    implementation(commonLibs.dora.arouter)
    kapt(commonLibs.arouter.compiler)
    implementation(commonLibs.dcache)
    implementation(commonLibs.dview.titlebar)
    implementation(commonLibs.dview.colors)
    implementation(commonLibs.dview.clearedittext)
    implementation(commonLibs.dview.dialog.bottom)
    implementation(commonLibs.dview.dialog.alert)
    implementation(commonLibs.dview.dialog.loading)
    implementation(commonLibs.refresh.layout)
    implementation(commonLibs.refresh.header)
    implementation(commonLibs.refresh.footer)
    implementation(commonLibs.ktx.viewmodel)
    implementation(commonLibs.ktx.livedata)
}

你有沒有發(fā)現(xiàn),橫杠符號換成了點(diǎn)?我們命名的時候就可以盡量保證物以類聚。同一類的sdk,如果后面的名稱是一致的,我們可以提前。比如lifecycle-livedata-ktx和lifecycle-viewmodel-ktx,再比如dview的那三個dialog庫。

注意事項(xiàng)

Aliases must consist of a series of identifiers separated by a dash (-, recommended), an underscore (_) or a dot (.). Identifiers themselves must consist of ascii characters, preferably lowercase, eventually followed by numbers.

別名必須由一系列標(biāo)識符組成,并用破折號(-,推薦)、下劃線 (_) 或點(diǎn) (.) 分隔。 標(biāo)識符本身必須由 ascii 字符組成,最好是小寫,最后跟數(shù)字。

Some keywords are reserved, so they cannot be used as an alias. Next words cannot be used as an alias:

extensions

class

convention

Additional to that next words cannot be used as a first subgroup of an alias for dependencies (for bundles, versions and plugins this restriction doesn’t apply):

bundles

versions

plugins

So for example for dependencies an alias versions-dependency is not valid, but versionsDependency or dependency-versions are valid.

有些關(guān)鍵字是保留的,因此不能用作別名。 接下來的單詞不能用作別名:

  • extensions
  • class
  • convention

除此之外,接下來的單詞不能用作依賴項(xiàng)別名的第一個子組(對于捆綁包、版本和插件,此限制不適用):

  • bundles
  • versions
  • plugins

例如,對于依賴項(xiàng),別名 versions-dependency 無效,但 versionsDependency 或 dependency-versions 有效。

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

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

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