Android - Unified Dependency Management (config.gradle)

insert image description here

foreword

This article belongs to the extended article of "A Rapid Application Development Framework Based on MVP Architecture, Kotlin Version" (Note: This article is still being updated, you can take a look first, so stay tuned!), and introduces in detail the use of the LeoFastDevMvpKotlin rapid development framework. Time, the method of project dependency management.

introduce

Android relies on unified management So far, bloggers know that there are three methods, namely:

  1. The traditional apply from method (also a method I want to talk about in this article): Create a new "config.gradle" file, and then write all the dependencies in the project in it. The update only needs to modify the content of the "config.gradle" file, which affects all module.
  2. buildSrc method: When running Gradle, it will check whether there is a directory named buildSrc in the project. Gradle will then automatically compile and test this code and put it into the classpath of the build script. For multi-project builds, there can only be one buildSrc directory, which must be located in the root project directory. buildSrc is the Gradle project root directory under a directory.
  3. Composing builds approach: Composite builds are just builds that include other builds. In many ways, a composite build is similar to a Gradle multi-project build, except that it includes complete builds rather than individual projects. In general, he has The advantage of the buildSrc approach is that simultaneous updates don't require rebuilding the entire project.

Each of the three methods has its own advantages, and the most perfect one should be the third implementation. But this method is not conducive to the use of the framework, because it belongs to a new module. If the project depends on the framework remotely, this module is also included by default. So the blogger chose the first way. The following article also focuses on the first method.

Method to realize

To achieve this unified dependency management, there are three steps in total, namely:

  • Step 1: Create a "config.gradle" file
  • Step 2: Introduce "config.gradle" into the project
  • Step 3: Add dependencies in "build.gradle" of all modules
  • Step 1: Create a "config.gradle" file

    First change the Android format of the Aandroid Studio directory to Project, and then create a "config.gradle" file

    insert image description here
    Then we edit the content in the article, here is the code of the framework directly (the length is too long, part of the code is omitted):

    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",
            
            ......
            ]
    }
    

    A simple understanding is to save all dependencies in two arrays of version number and dependency name, all of which are managed and managed in this file. Use ext to wrap three arrays: the first is "build.gradle" in Android , the second is the version number, and the third is the name of the dependency. The dependency version number in the dependent name array refers to the version number in the version array through the $ keyword

  • Step 2: Introduce "config.gradle" into the project

    Import the " config.gradle " file into the project, and add the following code to the "build.gradle" file in the root directory of the project (that is, in the same directory as the newly created "config.gradle"):

      apply from:"config.gradle"
    

    It should be noted that if you are AndroidStudio 4.0+ then you will see a "build.gradle" file like this

    // 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"
    

    Instead, if you are AndroidStudio 4.0- then you will see a "build.gradle" file like this

    
    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
    }
    
    

    But it's just that the content in the two files is inconsistent, the location of this file is the same, and the import code we added is also the same. It can be said that this is just a passing mention, and it does not actually affect our way of achieving unified dependency management.

  • Step 3: Add dependencies in "build.gradle" of all modules

    This step is the most important. After we have completed the above two steps, we are just ready. Now we need to point the dependencies in the "build.gradle" file in each of our modules to the "config.gradle" file. That is, the two "build.gradle" files circled in the figure below.

    insert image description here

    Because we have introduced "config.gradle" in the root directory in the second step, so we can point to "config.gradle" in "build.gradle". For example:

    implementation rootProject.ext.dependencies.coreKtx

    This line refers to the content of coreKtx in the dependencies array in our "config.gradle" file. A complete example is as follows:

    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
        
    }
    
    

    It should be noted that when we write code, there is no automatic code completion. So be careful, it must be consistent with the name in the "config.gradle" file.

Precautions

  • First of all, when coding in this way, there is no code completion (only the input will prompt), we need to ensure that our names are consistent
  • When we add dependencies, after adding them in "config.gradle", remember to add the corresponding pointing code in "build.gradle" in the corresponding module.

Summarize

The above is the whole content of this article. In summary, there are not many steps, only three steps. But it's the details that require attention. It is necessary to keep the written dependencies consistent with the "config.gradle" file, and there will be no automatic code completion for unwritten words.

In addition, this article is an extended article of "A Rapid Application Development Framework Based on MVP Architecture, Kotlin Version" , so it will be explained step by step in more detail. You can pick the key points and skip reading.

Head up picture

Guess you like

Origin blog.csdn.net/weixin_43683367/article/details/130268171