美文网首页
使用TOML统一组件化SDK版本

使用TOML统一组件化SDK版本

作者: 头秃到底 | 来源:发表于2024-01-07 22:21 被阅读0次

    自从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库最快捷的发布方式就是jitpack了。

    当项目使用了组件化以后,通常我们会定义一个config.gradle,像是这样。

    ext {
        // 是否单独运行某个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数据库
                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"]}",
                // 网络请求
                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",
                // 数据存储
                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"]}",
    
                // 相机
    //            cameraX           : "androidx.camera:camera-camera2:${dependVersion["cameraxVersion"]}",
    
                // 权限处理
                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 是一种旨在成为一个小规模、易于使用的语义化的配置文件格式,它被设计为可以无二义性的转换为一个哈希表。 “TOML”这个名字是“Tom's Obvious, Minimal Language”的首字母略写词。“Tom”指它的作者Tom Preston-Werner。

    toml官网

    我们去它官网瞧一瞧,toml.io/en/。这语法看起来好像蛮简单的。是的,它是一个key-value的配置文件。类似的还有json、ini、properties和yaml配置。

    gradle对toml的支持

    那么我们要怎么用呢?前面都是废话,从这里开始才是干货。

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

    然后在项目根目录创建一个名为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" }
    
    

    好小子,我感觉你在推广自己的库啊,而且我有证据!接下来在模块的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)
    }
    
    

    你有没有发现,横杠符号换成了点?我们命名的时候就可以尽量保证物以类聚。同一类的sdk,如果后面的名称是一致的,我们可以提前。比如lifecycle-livedata-ktx和lifecycle-viewmodel-ktx,再比如dview的那三个dialog库。

    注意事项

    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.

    别名必须由一系列标识符组成,并用破折号(-,推荐)、下划线 (_) 或点 (.) 分隔。 标识符本身必须由 ascii 字符组成,最好是小写,最后跟数字。

    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.

    有些关键字是保留的,因此不能用作别名。 接下来的单词不能用作别名:

    • extensions
    • class
    • convention

    除此之外,接下来的单词不能用作依赖项别名的第一个子组(对于捆绑包、版本和插件,此限制不适用):

    • bundles
    • versions
    • plugins

    例如,对于依赖项,别名 versions-dependency 无效,但 versionsDependency 或 dependency-versions 有效。

    相关文章

      网友评论

          本文标题:使用TOML统一组件化SDK版本

          本文链接:https://www.haomeiwen.com/subject/quknndtx.html