Android— 统一依赖管理(config.gradle)

介绍

Android 依赖统一管理距目前为止,博主一共知道有三种方法,分别是:

  1. 传统apply from的方式(也是本文想讲的一种方式):新建一个 「config.gradle」 文件,然后将项目中所有依赖写在里面,更新只需修改 「config.gradle」 文件内容,作用于所有module。
  2. buildSrc 方式:当运行 Gradle 时会检查项目中是否存在一个名为 buildSrc 的目录。然后 Gradle 会自动编译并测试这段代码,并将其放入构建脚本的类路径中, 对于多项目构建,只能有一个 buildSrc 目录,该目录必须位于根项目目录中, buildSrc 是 Gradle 项目根目录下的一个目录。
  3. Composing builds 方式:复合构建只是包含其他构建的构建. 在许多方面,复合构建类似于 Gradle 多项目构建,不同之处在于,它包括完整的 builds ,而不是包含单个 projects,总的来说,他有 buildSrc 方式的优点,同时更新不需要重新构建整个项目。

三种方式各有各的好,目前最完美的应该是第三种实现。但是这种方式不利于框架使用,因为它属于的是新建一个module,如果项目远程依赖了框架,默认也包含了这个 module。所以博主选择了第一种方式。以下文章也是围绕第一种方式进行讲解。

实现方式

实现这个统一依赖管理,拢共分三步,分别是:

  • 第一步:创建「config.gradle」 文件
  • 第二步:项目当中引入「config.gradle」
  • 第三步:在所有module的「build.gradle」当中添加依赖
  • 第一步:创建 「config.gradle」 文件

    首先将 Aandroid Studio 目录的Android格式修改为Project,然后再创建一个「config.gradle」的文件

    在这里插入图片描述

    然后我们编辑文章里面的内容,这里直接给出框架的代码出来(篇幅太长,省略部分代码):

    ext {
    /**
     * 基础配置 对应 build.gradle 当中 android 括号里面的值
     */
    android = [
            compileSdk               : 32,
            minSdk                   : 21,
            targetSdk                : 32,
            versionCode              : 1,
            versionName              : "1.0.0",
            testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner",
            consumerProguardFiles    : "consumer-rules.pro"
            
            ......
    ]
    
    /**
     * 版本号 包含每一个依赖的版本号,仅仅作用于下面的 dependencies
     */
    version = [
            coreKtx              : "1.7.0",
            appcompat            : "1.6.1",
            material             : "1.8.0",
            constraintLayout     : "2.1.3",
            navigationFragmentKtx: "2.3.5",
            navigationUiKtx      : "2.3.5",
            junit                : "4.13.2",
            testJunit            : "1.1.5",
            espresso             : "3.4.0",
            
            ......
    ]
    
    /**
     * 项目依赖 可根据项目增加删除,但是可不删除本文件里的,在 build.gradle 不写依赖即可
     * 因为MVP框架默认依赖的也在次文件中,建议只添加,不要删除
     */
    dependencies = [
    
            coreKtx                    : "androidx.core:core-ktx:$version.coreKtx",
            appcompat                  : "androidx.appcompat:appcompat:$version.appcompat",
            material                   : "com.google.android.material:material:$version.material",
            constraintLayout           : "androidx.constraintlayout:constraintlayout:$version.constraintLayout",
            navigationFragmentKtx      : "androidx.navigation:navigation-fragment-ktx:$version.navigationFragmentKtx",
            navigationUiKtx            : "androidx.navigation:navigation-ui-ktx:$version.navigationUiKtx",
            junit                      : "junit:junit:$version.junit",
            testJunit                  : "androidx.test.ext:junit:$version.testJunit",
            espresso                   : "androidx.test.espresso:espresso-core:$version.espresso",
            
            ......
            ]
    }
    

    简单理解就是将所有的依赖,分成版本号以及依赖名两个数组的方式保存,所有都在这个文件统一管管理。用 ext 包裹三个数组:第一个是「build.gradle」Android 里面的,第二个是版本号,第三个是依赖的名字。依赖名字数组里面的依赖版本号通过 $ 关键字指代 version 数组里面的版本号

  • 第二步:项目当中引入 「config.gradle」

    将「config.gradle」文件引入项目当中,在项目的根目录的「build.gradle」文件(也就是刚刚新建的 「config.gradle」同目录下的),添加如下代码:

    apply from:"config.gradle"
    

    需要注意的的是,如果你是 AndroidStudio 4.0+ 那么你将看到这样的「build.gradle」文件

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        id 'com.android.application' version '7.2.2' apply false
        id 'com.android.library' version '7.2.2' apply false
        id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
    }
    
    apply from:"config.gradle"
    

    相反,如果你是 AndroidStudio 4.0- 那么你将会看到这样的「build.gradle」文件

    
    apply from: "config.gradle"
    
    buildscript {
        ext.kotlin_version="1.7.10"
        repositories {
            maven { url "https://jitpack.io" }
            mavenCentral()
            google()
        }
        dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
            mavenCentral()
            google()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    不过仅仅是两个文件里面的内容不一致,这个文件的位置是一样的,而且我们添加的引入代码也是一样的。可以说,这只是顺带提一嘴,实际上不影响我们实现统一依赖管理这个方式。

  • 第三步:在所有module的「build.gradle」当中添加依赖

    这一步是最重要的,我们完成了上面两步之后,只是做好了准备,现在我们需要将我们每一个module里面「build.gradle」文件里面的依赖指向「config.gradle」文件。也就是下图圈起来的 那两个「build.gradle」文件。

    在这里插入图片描述

    因为我们第二步的时候已经在根目录引入了「config.gradle」,所以我们在「build.gradle」就可以指向「config.gradle」例如:

    implementation rootProject.ext.dependencies.coreKtx

    这一行,就指代了我们「config.gradle」文件里面的 dependencies 数组里面的 coreKtx 的内容。完整示例如下:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
    }
    android {
        namespace 'leo.dev.mvp.kt'
    //    compileSdk 32
        compileSdk rootProject.ext.android.compileSdk
    
        defaultConfig {
            applicationId "leo.dev.mvp.kt"
    //        minSdk 21
    //        targetSdk 32
    //        versionCode 1
    //        versionName "1.0"
    //
    //        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    
            minSdk rootProject.ext.android.minSdk
            targetSdk rootProject.ext.android.targetSdk
            versionCode rootProject.ext.android.versionCode
            versionName rootProject.ext.android.versionName
    
            testInstrumentationRunner rootProject.ext.android.testInstrumentationRunner
    
        }
        
        ......
    }
    
    dependencies {
    
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        
    //    implementation 'androidx.core:core-ktx:1.7.0'
    //    implementation 'androidx.appcompat:appcompat:1.6.1'
    //    implementation
    //
    //    testImplementation 'junit:junit:4.13.2'
    //    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    //    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    
        implementation rootProject.ext.dependencies.coreKtx
        implementation rootProject.ext.dependencies.appcompat
        implementation rootProject.ext.dependencies.material
    
        testImplementation rootProject.ext.dependencies.junit
        androidTestImplementation rootProject.ext.dependencies.testJunit
        androidTestImplementation rootProject.ext.dependencies.espresso
        
    }
    

    需要注意的是,我们在编写代码的时候,是没有代码自动补全的。所以得小心翼翼,必须要和「config.gradle」文件里面的名字向一致。

注意事项

  • 首先就是这种方式在coding的时候,是没有代码补全的(只有输入过的,才会有提示),我们需要确保我们的名字一致
  • 我们在增加依赖的时候,在「config.gradle」里面添加完之后,记得在对应的module里面的「build.gradle」里面添加对应的指向代码。

总结

以上就是本篇文章的全部内容,总结起来其实步骤不多,也就三步。但是需要注意的是细节。需要保持写入的依赖与「config.gradle」文件一致,并且未写过的词,是不会有代码自动补全的。

更多Android进阶学习资料 请点击免费领取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值