欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android統(tǒng)一依賴管理的三種方式總結(jié)

 更新時(shí)間:2022年01月27日 08:51:58   作者:Arrom  
為了項(xiàng)目的管理,依賴包的紡一管理是必要的,下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一依賴管理的三種方式,文中通過實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下

簡述

每個(gè)項(xiàng)目從新建開始我們或多或少都會(huì)導(dǎo)入各種依賴庫,如果項(xiàng)目中只有一個(gè)module的話,對于依賴庫版本的管理很容易,但是多個(gè)module的話,稍加不注意,很容易導(dǎo)入多個(gè)版本的庫甚至產(chǎn)生版本沖突,更新修改依賴庫也需要多處操作,所以我們需要對依賴庫版本進(jìn)行統(tǒng)一管理。

傳統(tǒng)apply from的方式(也是我以前項(xiàng)目中使用)

為了對模塊進(jìn)行統(tǒng)一管理,會(huì)在根目錄新建一個(gè)config.gradle文件或者在根目錄的build.gradle定義一些變量

ext {
    android = [
            compileSdkVersion   : 29,
            buildToolsVersion   : "30.0.2",
            applicationId       : "com.xionggouba.bearseller",
            minSdkVersion       : 19,
            targetSdkVersion    : 30,
            versionCode         : 27,
            versionName         : "3.9.1",
            defaultPublishConfig: 'release',
            publishNonDefault   : true,
            multiDexEnabled     : true,
            mapKey              : 'c7e1ee468aa1bf8a6739',
            pushKey             : '65aae199a0059eb1dbe7',
            pushChannel         : 'developer-default',
    ]
    appid = [
            app           : "com.xionggouba.bearseller",
            login         : "com.huitao.login",
            home          : "com.huitao.home",
            webview       : "com.huitao.webview",
            main          : "com.huitao.main",
            productManager: "com.huitao.productmanager",
            personal      : "com.huitao.personalcenter",
            map           : "com.huitao.map",
            bluetooth     : "com.huitao.bluetooth",
            push          : "com.huitao.push",
            markketing    : "con.huitao.marketing",
            printer       : "com.huitao.printer"
    ]
    versions = [
            "lifecycle_version": "2.2.0",
            "arch_version"     : "2.1.0",
            "retrofit_version" : "2.6.2",
            "dialog_version"   : "3.3.0",
            "glide_version"    : "4.9.0",
            "hilt"             : "2.28-alpha",
            "kotlin_version"   : "1.4.10",
            "fragment_version" : "1.2.5",
            "room_version"     : "2.2.6"
    ]
    architecture = [
            "viewmodel"          : "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions['lifecycle_version']}",
            "livedata"           : "androidx.lifecycle:lifecycle-livedata-ktx:${versions['lifecycle_version']}",
            "lifecycleruntime"   : "androidx.lifecycle:lifecycle-runtime-ktx:${versions['lifecycle_version']}",
            "savedstate"         : "androidx.lifecycle:lifecycle-viewmodel-savedstate:${versions['lifecycle_version']}",
            // alternately - if using Java8, use the following instead of lifecycle-compiler
            "lifecyclecommon"    : "androidx.lifecycle:lifecycle-common-java8:${versions['lifecycle_version']}",
            // Saved state module for ViewModel
            "viewmodelsavedstate": "androidx.lifecycle:lifecycle-viewmodel-savedstate:${versions['lifecycle_version']}",
            "lifecycleextentions": "androidx.lifecycle:lifecycle-extensions:${versions['lifecycle_version']}",
            "retrofit2"          : "com.squareup.retrofit2:retrofit:${versions['retrofit_version']}",
            "gson"               : "com.squareup.retrofit2:converter-gson:${versions['retrofit_version']}",
            "persistentcookiejar": "com.github.franmontiel:PersistentCookieJar:v1.0.1",
            "glide"              : "com.github.bumptech.glide:glide:${versions['glide_version']}",
            "glidecompiler"      : "com.github.bumptech.glide:compiler:${versions['glide_version']}",
            "oss"                : "com.aliyun.dpa:oss-android-sdk:2.9.1",
            "luban"              : "top.zibin:Luban:1.1.8"
    ]
]

在工程的根目錄build.gradle添加

apply from"config.gradle"

找一個(gè)東西就得靠搜索,逐一查找。

buildSrc方式

什么是buildSrc

當(dāng)運(yùn)行 Gradle 時(shí)會(huì)檢查項(xiàng)目中是否存在一個(gè)名為 buildSrc 的目錄。然后 Gradle 會(huì)自動(dòng)編譯并測試這段代碼,并將其放入構(gòu)建腳本的類路徑中, 對于多項(xiàng)目構(gòu)建,只能有一個(gè) buildSrc 目錄,該目錄必須位于根項(xiàng)目目錄中, buildSrc 是 Gradle 項(xiàng)目根目錄下的一個(gè)目錄,它可以包含我們的構(gòu)建邏輯,與腳本插件相比,buildSrc 應(yīng)該是首選,因?yàn)樗子诰S護(hù)、重構(gòu)和測試代碼

小結(jié)

buildSrc在近幾年時(shí)非常流行的,因?yàn)樗蚕?buildSrc 庫工件的引用,全局只有一個(gè)地方可以修改它,支持自動(dòng)補(bǔ)全(這個(gè)很爽),支持跳轉(zhuǎn)。 但是他也有一個(gè)缺點(diǎn),依賴更新將重新構(gòu)建整個(gè)項(xiàng)目,這個(gè)不是很好。

Composing builds

什么是Composing builds

復(fù)合構(gòu)建只是包含其他構(gòu)建的構(gòu)建. 在許多方面,復(fù)合構(gòu)建類似于 Gradle 多項(xiàng)目構(gòu)建,不同之處在于,它包括完整的 builds ,而不是包含單個(gè) projects

  • 組合通常獨(dú)立開發(fā)的構(gòu)建,例如,在應(yīng)用程序使用的庫中嘗試錯(cuò)誤修復(fù)時(shí)
  • 將大型的多項(xiàng)目構(gòu)建分解為更小,更孤立的塊,可以根據(jù)需要獨(dú)立或一起工作

小結(jié)

這種方式擁有buildSrc的優(yōu)點(diǎn),同時(shí)依賴更新不用重新構(gòu)建整個(gè)項(xiàng)目。

Composing builds項(xiàng)目實(shí)戰(zhàn)

在項(xiàng)目中新建一個(gè)model,用于管理配置信息

新建一個(gè)ConfigPlugin的類,不用具體的實(shí)現(xiàn)

class ConfigPlugin: Plugin<Project> {
    override fun apply(p0: Project) {
    }

    companion object{
    }
}

編輯configPluginmodel中的build.gradle文件

我的文件大致內(nèi)容如下

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // 因?yàn)槭褂玫?Kotlin 需要需要添加 Kotlin 插件
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
    }
}

apply plugin: 'kotlin'
apply plugin: 'java-gradle-plugin'

repositories {
    // 需要添加 jcenter 否則會(huì)提示找不到 gradlePlugin
    jcenter()
    google()
}

dependencies {
    implementation gradleApi()
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

gradlePlugin {
    plugins {
        version {
            // 在 app 模塊需要通過 id 引用這個(gè)插件
            id = 'com.umeshop.configplugin'
            // 實(shí)現(xiàn)這個(gè)插件的類的路徑
            implementationClass = 'com.umeshop.configplugin.ConfigPlugin'
        }
    }
}

修改根目錄的settings.gradle文件

項(xiàng)目目錄引入插件

在configPluginmodel中定義一個(gè)DependencyManager文件來管理一些第三方的依賴

在使用目錄導(dǎo)入

大致流程介紹完成

需要注意的地方

根目錄的settings.gradle的配置

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "jetpackDemo"
include ':app'

有些版本是這樣的,建議刪除dependencyResolutionManagement的配置,在build.gradle中配置。大致是這樣的

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

還有一個(gè)就是依賴的修改

includeBuild("configPlugin")

是includeBuild標(biāo)簽,不是include

總結(jié)

到此這篇關(guān)于Android統(tǒng)一依賴管理的三種方式的文章就介紹到這了,更多相關(guān)Android統(tǒng)一依賴管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論