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