Application comparison of four dependency management methods in Android

Android application development involves a large number of dependent libraries and third-party components, so it is crucial to effectively manage these dependencies. This article will introduce the four main Android dependency management methods, analyze their advantages, disadvantages, and best practices.

introduction

In Android application development, dependency management is a critical task. Dependency management includes not only the introduction of libraries and components, but also version control, sharing and maintenance. In order to meet the needs of different projects and teams, the Android development community has proposed various dependency management methods.

traditional dependency approach

The traditional dependency management method is build.gradleto add dependencies directly in the project's files, which is one of the most common methods. The sample code is as follows:

dependencies {
    
    
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.firebase:firebase-core:20.0.0'
    // 添加更多依赖...
}

Advantages :

  • Simple and easy to understand, suitable for small projects or rapid prototyping.

Disadvantages :

  • As dependencies increase, build.gradlethe files can become large and difficult to maintain.
  • Dependency versions are not easily shared, which may lead to version conflicts.

Best Practice : Suitable for small projects or prototype development where you need to keep things simple and flexible.

Kotlin buildSrc

Kotlin buildSrc is an improved approach to dependency management that moves dependency definitions into independent Kotlin modules for better organization and sharing of dependencies. Proceed as follows:

  1. Create a buildSrcsubproject named.
  2. buildSrcCreate a Kotlin file in, for example , Dependencies.ktand define dependencies in it.
// buildSrc/src/main/java/Dependencies.kt

object Dependencies {
    
    
    const val appCompat = "com.android.support:appcompat-v7:28.0.0"
    const val firebaseCore = "com.google.firebase:firebase-core:20.0.0"
    // 添加更多依赖...
}

  1. build.gradleUse these dependencies in the main project :
dependencies {
    
    
    implementation Dependencies.appCompat
    implementation Dependencies.firebaseCore
    // 添加更多依赖...
}

Advantages :

  • Better organization and shared dependencies.
  • Reduced build.gradlefile complexity.

Disadvantages :

  • Additional buildSrcsubprojects need to be created.

Best Practice : Suitable for medium-sized projects where better organization and shared dependencies are required.

Composing builds

Composing builds is a new feature in the Android Gradle plugin that allows the build logic to be split into multiple independent building modules. Proceed as follows:

  1. Create a new module named composeBuilds and create build.gradle.ktthe file
...

gradlePlugin {
    
    
    plugins {
    
    
        version {
    
    
            // 在 app 模块需要通过 id 引用这个插件
            id = 'com.xxx.xxx'
            // 实现这个插件的类的路径
            implementationClass = 'com.xx.Dependencies'
        }
    }
}

  1. Add a new file in module Dependencies.ktand add the following content
class Dependencies : Plugin<Project> {
    
    
    override fun apply(project: Project) {
    
    
    }

    companion object {
    
    
        val appcompat = "com.android.support:appcompat-v7:28.0.0"
    }
}

  1. settings.gradleDefine the building blocks in the main project's files:
includeBuild('path/to/composeBuilds')

  1. Create a file in the build module build.gradle.ktsand define dependencies in it.
// path/to/composeBuilds/build.gradle.kts

dependencies {
    
    
    implementation("com.android.support:appcompat-v7:28.0.0")
    implementation("com.google.firebase:firebase-core:20.0.0")
    // 添加更多依赖...
}

  1. Apply the building module in the main project build.gradle:
plugins{
    
    
    // 这个id就是在composeBuilds文件夹下build.gradle文件内定义的id
    id "com.xxx.xxx"
}

dependencies {
    
    
    implementation Dependencies.appCompat
}

Advantages :

  • Build logic is broken down into modules, making it easier to manage and maintain.
  • Building blocks can be shared into multiple projects.

Disadvantages :

  • Additional building blocks need to be created.

Best practice : Suitable for large projects where build logic needs to be modularized and shared.

Version Catalogs

Version Catalogs is a new way of managing dependencies, one of which is to .tomldefine all dependencies and version information through files. One advantage of this method is that it can centrally manage all dependent versions, reducing the possibility of version conflicts. Proceed as follows:

  1. dependencies.tomlCreate a file called in the root directory of your project .toml, defining the dependencies.
# dependencies.toml

[dependencies]
appCompat = "com.android.support:appcompat-v7:28.0.0"
firebaseCore = "com.google.firebase:firebase-core:20.0.0"
# 添加更多依赖...

  1. In the main project's settings.gradle.ktsfile, specify the location of the Version Catalogs:
// settings.gradle.kts

dependencyResolutionManagement {
    
    
    // 指定Version Catalogs的位置
    versionCatalogs {
    
    
        create("dependencies") {
    
    
            from("${rootProject.projectDir}/dependencies.toml")
        }
    }
}

  1. Reference the Version Catalogs in the main project's build.gradle.ktsfiles and use the dependencies from them:
// build.gradle.kts

dependencies {
    
    
    // 使用Version Catalogs中的依赖项
    implementation dependencies.appCompat
    implementation dependencies.firebaseCore
    // 添加更多依赖...
}

Advantages :

  • Centrally manage dependent versions to reduce version conflicts.
  • Version information can be easily shared among multiple projects.

Disadvantages :

  • .tomlFile formats need to be learned and used .

Best practice : Suitable for complex projects involving large teams that require stricter version management and sharing of version information.

in conclusion

Different Android projects may require different dependency management methods, and the choice should be based on the size, complexity and needs of the team. The traditional dependency method is suitable for small projects and prototype development, while Kotlin buildSrc, Composing builds, and Version Catalogs are suitable for larger and complex projects. Choosing the most appropriate method according to your needs will contribute to the successful development and maintenance of the project.

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, you must be able to select and expand, and improve your programming thinking. In addition, good career planning is also very important, and learning habits are important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here is a set of "Advanced Notes on the Eight Major Modules of Android" written by a senior architect at Alibaba to help you systematically organize messy, scattered, and fragmented knowledge, so that you can systematically and efficiently Master various knowledge points of Android development.
img
Compared with the fragmented content we usually read, the knowledge points in this note are more systematic, easier to understand and remember, and are strictly arranged according to the knowledge system.

Welcome everyone to support with one click and three links. If you need the information in the article, just scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓ (There is also a small bonus of ChatGPT robot at the end of the article, don’t miss it)

PS: There is also a ChatGPT robot in the group, which can answer everyone’s work or technical questions.

picture

Guess you like

Origin blog.csdn.net/datian1234/article/details/133045366