Android---gradle全局配置config.build


转载: https://blog.csdn.net/T_double/article/details/105557178

 

概念
Gradle的官方定义:

Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。

个人通俗的理解为:

Gradle是一个构建工具,是用来帮我们执行编译、打包APP等过程的,并且我们可以自己配置构建规则,Gradle便可以根据我们的命令为我们自动构建出apk包

以下内容基于Android Studio编译器进行初建的APP工程进行示例说明

全局配置文件
随着APP项目的模块化开发、组件化开发以及插件化开发等模式的出现,为了能方便在不同的module中进行统一管理公共配置信息,全局的gradle配置文件显得有必要,其中下列方式只是其中的一种配置方式
在项目的根目录下创建config.gradle文件,用来统一定义并管理公共配置信息部分
 

 统一定义并管理的config.gradle内容

/**
 * ext:扩展内容,固定格式(即只能用ext{}),并且是一个闭包的内容
 */
ext {
    //appcompatV7Version:自定义的属性,下同
    appcompatV7Version = "26.+"
    junitVersion = "4.12"
    //appId:自定义的一个map映射集合,key-value
    appId = [
            "app": "app的包名"
    ]
    android = [
            compileSdkVersion: 26,
            buildToolsVersion: "26.0.2",
            minSdkVersion    : 19,
            targetSdkVersion : 26,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    dependencies = [
            //shell语言语法:${变量名}
            "appcompat-v7": "com.android.support:appcompat-v7:${appcompatV7Version}",
            "junit"       : "junit:junit:${junitVersion}"
    ]
}

config.gradle文件的使用

 其实上图的写法与以下内容的写法是一样的,区别只是做了两个配置文件的整合而已

// Top-level build file where you can add configuration options common to all sub-projects/modules. /** * ext:扩展内容,固定格式(即只能用ext{}),并且是一个闭包的内容 */ ext { //appcompatV7Version:自定义的属性,下同 appcompatV7Version = "26.+" junitVersion = "4.12"
    //appId:自定义的一个map映射集合,key-value appId = [ "app": "app的包名" ] android = [ compileSdkVersion: 26, buildToolsVersion: "26.0.2", minSdkVersion : 19, targetSdkVersion : 26, versionCode : 1, versionName : "1.0" ] dependencies = [ //shell语言语法:${变量名} "appcompat-v7": "com.android.support:appcompat-v7:${appcompatV7Version}", "junit"       : "junit:junit:${junitVersion}" ] } buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }

全局配置的使用

以app模块为例(参照配置中的注释进行理解)

 

apply plugin: 'com.android.application'

//赋值与引用,def 类型 相当于java中的Object类型,如config是一个map集合对象 def config = rootProject.ext.android def appId = rootProject.ext.appId println("config内容:${config}")//输出内容-》config内容:[compileSdkVersion:26, buildToolsVersion:26.0.2, minSdkVersion:19, targetSdkVersion:26] println("appId内容:${appId}")//输出内容-》appId内容:[app:com.itsdf07.appnotes] android { compileSdkVersion config.compileSdkVersion //map集合取值方式一 buildToolsVersion config.buildToolsVersion defaultConfig { applicationId appId["app"]//map集合取值方式二 minSdkVersion config.minSdkVersion targetSdkVersion config.targetSdkVersion versionCode config.versionCode versionName config.versionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }

其中上面内容使用到了println打印就相当于java中的system.out.println一样,打印结果与查看内容位置如下图展示

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM