美文网首页
Android 中 Gradle 调用过程

Android 中 Gradle 调用过程

作者: WalkerDenial | 来源:发表于2020-11-01 23:32 被阅读0次

    在 Android 开发中,我们经常接触 gradle 文件,例如:settings.gradle、build.gradle 等等,那么新建一个项目,他们之间的先后顺序是什么呢?

    What?为什么要知道这个呢,直接调用它不香吗?

    香是香,但是 当你需要做一些配置的时候,比如资源配置、预处理等操作的时候,该添加到什么位置呢?只有搞清楚了调用流程,才能在正确的地方添加操作。

    接下来以一个新建项目为例:

    新建一个 Android 项目目录如下:

    ├── app
    │   ├── libs
    │   ├── src
    │   ├── build.gradle
    │   └── proguard-rules.pro
    ├── build
    ├── gradle
    │   └── wrapper
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── config.gradle
    ├── build.gradle
    ├── gradle.properties
    ├── gradlew
    ├── gradlew.bat
    ├── local.properties
    └── settings.gradle
    

    其中 config.gradle 是基础配置,统一管理依赖项,整个项目中,存在 4 个 gradle 文件,它们分别是:

    1. app/build.gradle:对 app 模块所需配置信息进行管理;
    2. config.gradle:对依赖配置版本信息进行统一管理;
    3. build.gradle:项目根配置文件;
    4. settings.gradle:配置、管理项目模块信息;

    对以上 4 个文件进行日志信息输出处理:

    app/build.gradle

    
    android {
        println("app --> build.gradle --> android")
        ...
    
        defaultConfig {
            println("app --> build.gradle --> defaultConfig")
            ...
            println("app --> build.gradle --> defaultConfig out")
        }
    
        buildTypes {
            println("app --> build.gradle --> buildTypes")
            debug {
                println("app --> build.gradle --> debug")
                println("app --> build.gradle --> debug out")
            }
            release {
                println("app --> build.gradle --> release")
                ...
                println("app --> build.gradle --> release out")
            }
            println("app --> build.gradle --> buildTypes out")
        }
        compileOptions {
            println("app --> build.gradle --> compileOptions")
            ...
            println("app --> build.gradle --> compileOptions out")
        }
        kotlinOptions {
            println("app --> build.gradle --> kotlinOptions")
            ...
            println("app --> build.gradle --> kotlinOptions out")
        }
        println("app --> build.gradle --> android out")
    }
    
    dependencies {
        println("app --> build.gradle --> dependencies")
        ...
        println("app --> build.gradle --> dependencies out")
    }
    

    config.gradle

    allprojects {
        println("config.gradle --> allprojects")
        repositories {
            println("config.gradle --> repositories")
            ...
            println("config.gradle --> repositories out")
        }
        println("config.gradle --> allprojects out")
    }
    
    task clean(type: Delete) {
        println("config.gradle --> clean")
        ...
        println("config.gradle --> clean out")
    }
    
    ext.deps = [
            ...
    ]
    

    build.gradle

        buildscript {
        println("build.gradle --> buildscript")
        ...
        repositories {
            println("build.gradle --> repositories")
            ...
            println("build.gradle --> repositories out")
        }
        dependencies {
            println("build.gradle --> dependencies")
            ..
            println("build.gradle --> dependencies out")
        }
        println("build.gradle --> buildscript out")
    }
    

    settings.gradle

        println("settings.gradle --> start")
        ...
        println("settings.gradle --> end")
    

    build 项目之后,日志输出信息如下:

    Starting Gradle Daemon...
    Gradle Daemon started in 953 ms
    settings.gradle --> start
    settings.gradle --> end
    
    > Configure project :
    build.gradle --> buildscript
    build.gradle --> repositories
    build.gradle --> repositories out
    build.gradle --> dependencies
    build.gradle --> dependencies out
    build.gradle --> buildscript out
    config.gradle --> allprojects
    config.gradle --> repositories
    config.gradle --> repositories out
    config.gradle --> allprojects out
    config.gradle --> allprojects
    config.gradle --> repositories
    config.gradle --> repositories out
    config.gradle --> allprojects out
    config.gradle --> clean
    config.gradle --> clean out
    
    > Configure project :app
    app --> build.gradle --> android
    app --> build.gradle --> defaultConfig
    app --> build.gradle --> defaultConfig out
    app --> build.gradle --> buildTypes
    app --> build.gradle --> debug
    app --> build.gradle --> debug out
    app --> build.gradle --> release
    app --> build.gradle --> release out
    app --> build.gradle --> buildTypes out
    app --> build.gradle --> compileOptions
    app --> build.gradle --> compileOptions out
    app --> build.gradle --> kotlinOptions
    app --> build.gradle --> kotlinOptions out
    app --> build.gradle --> android out
    app --> build.gradle --> dependencies
    app --> build.gradle --> dependencies out
    

    由此可知,执行顺序如下:settings.gradle --> build.gradle --> config.gradle --> app/build.gradle ,在配置 config 信息时,要注意其实效性在 build.gradle 之后,可以避免很多不必要的错误。

    最后,项目地址信息 KotlinStudy,欢迎交流学习。

    相关文章

      网友评论

          本文标题:Android 中 Gradle 调用过程

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