Gradle配置文件详解

     

 

目录

1.Gradle属性文件

1.1 gradle.properties

1.2 local.properties

2. setting.gradle

3.顶级配置文件

3.1 buildscript{ }

3.1.1 repositories{ }

3.2 allprojects{ }

4. 模块配置文件

5. Gradle-Wrapper配置


      Android Studio使用Gradle构建系统,能过简化编译、打包、测试过程; Gradle构建配置文件有3个Gradle的主要配置文件,为项目或模块的构建进行配置,分别是:setting.gradle、顶级配置文件(根目录下build.gradle)、模块配置文件(模块目录下的build.gradle),还有2个是配置Gradle的属性文件为gradle.properties和local.properties;

1.Gradle属性文件

1.1 gradle.properties

gradle.properties位于项目根目录下,主要设置Gradle后台进程JVM内存大小、日记级别等等;

Gradle属性配置参考:Gradle属性配置

1.2 local.properties

为Gradle配置环境变量,例如sdk、ndk路径;

2. setting.gradle

此配置文件位于根目录下,用于指示Gradle在构建应用时应将哪些模块包含在内;

多个模块之间用空格隔开

include ':app', ':lib'

3.顶级配置文件

此配置文件位于项目根目录下,用于定义项目所有模块的配置文件,

/**
 * The buildscript {} block is where you configure the repositories and
 * dependencies for Gradle itself--meaning, you should not include dependencies
 * for your modules here. For example, this block includes the Android plugin for
 * Gradle as a dependency because it provides the additional instructions Gradle
 * needs to build Android app modules.
 */

buildscript {

    /**
     * The repositories {} block configures the repositories Gradle uses to
     * search or download the dependencies. Gradle pre-configures support for remote
     * repositories such as JCenter, Maven Central, and Ivy. You can also use local
     * repositories or define your own remote repositories. The code below defines
     * JCenter as the repository Gradle should use to look for its dependencies.
     */

    repositories {
        google()
        jcenter()
    }

    /**
     * The dependencies {} block configures the dependencies Gradle needs to use
     * to build your project. The following line adds Android Plugin for Gradle
     * version 3.1.0 as a classpath dependency.
     */

    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
    }
}

/**
 * The allprojects {} block is where you configure the repositories and
 * dependencies used by all modules in your project, such as third-party plugins
 * or libraries. Dependencies that are not required by all the modules in the
 * project should be configured in module-level build.gradle files. For new
 * projects, Android Studio configures JCenter as the default repository, but it
 * does not configure any dependencies.
 */

allprojects {
   repositories {
       google()
       jcenter()
   }
}

3.1 buildscript{ }

主要配置Gradle代码托管库和依赖项;

3.1.1 repositories{ }

Gradle引用代码托管库,Gradle预先配置了远程支持JCenter、Maven、Central和lvy等存储区,当然也可以指定本地自定义的远程存储库,eg:google();

JCenter():就可以使用JCenJCenter上托管的开源项目;

1.2 dependencies{ }

项目依赖Gradle的插件版本路径;

3.2 allprojects{ }

配置所有模块中使用的依赖项,例如第三方插件或libraries;

4. 模块配置文件

参考:模块配置文件-build.gradle

5. Gradle-Wrapper配置

参考:Gradle-Wrapper详解

猜你喜欢

转载自blog.csdn.net/niuba123456/article/details/81073639