1. 程式人生 > >build.gradle檔案詳解

build.gradle檔案詳解

一個普通的android專案工程裡面有兩個build.gradle檔案,一個位於根目錄,一個位於app資料夾內.

一. 位於根目錄的build.gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
// 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 }

當前的studio版本是2.3,這是自動生成的程式碼,其實它是基於Groovy的領域特定語言(DSL),相比之前的構建工具Ant和Maven,更加的簡便,但是許多大神都認為沒有之前的更加有擴充套件性有靈活性,而且studio比較吃記憶體,編譯速度較慢.當然,我這個目前還是喜歡使用studio,因為我不是大神

  1. 兩句英文
    言歸正傳,第一句英文
    Top-level build file where you can add configuration options common to all sub-projects/modules, 這等鳥語,我這個四級都沒過的人,只好求助有道詞典.
    翻譯:頂級構建檔案,您可以新增配置選項常見的所有子專案/模組—->>就是頂級配置檔案的意思吧,大致是這個意思
    還有一句英文:
    NOTE: Do not place your application dependencies here; they belong in the individual module build.gradle files
    翻譯:注意:不要把你的應用程式依賴這裡;他們屬於個別模組build.gradle檔案—->>總有一些刁民,把app的依賴庫寫錯地方,所以給大家一個警告
  2. buildscript和allprojects對比
    buildscript裡面的內容是針對gradle的依賴倉庫(repositories)和依賴元件(dependencies )。allprojects裡面的就是針對咱們android專案工程的依賴倉庫(repositories).
    repositories閉包裡面的jcenter(),其是就是程式碼託管倉庫,很多開源專案都會選擇將程式碼託管到jcenter,宣告之後,就可以引用裡面的專案了。
    dependencies裡面的classpath宣告的一個Gradle的外掛,因為gradle不是專門構建android工程的工具,gradle還可以構建java,c++等專案,com.android.tools.build:gradle:2.3.0就是宣告是構建android專案的gradle工具;
  3. task clean(type: Delete)\
    有沒有很熟悉,其實就是build->clean project\
    話說build .gradle,其實就是gradle build的時候讀取的檔案唄,讀取的時候新增的任務 clean ,這句話之前好像沒有,之前有一個同事遇到過一個尷尬的情況,就是在studio1.X的時候新建的專案,後來studio升級到2.0,make modle時總會報錯,說什麼東西沒刪,現在看看估計是這個問題,1.x的時代裡面沒有這句話,後期需要了.所以說很坑啊\

2. 位於app資料夾內的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.cyy.new2"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary= true
    }
    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:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:support-vector-drawable:25.2.0'
    testCompile 'junit:junit:4.12'
}
  1. apply plugin: ‘com.android.application’
    與之相對應的是apply plugin: ‘com.android.library’,對比就會發現,其實就是application和library的區別,就是宣告這是一個app,還是一個library. 應用哪個外掛,這就是哪種型別,畢竟兩種型別的編譯肯定是有區別的,應用的編譯外掛也不一樣,闢謠:studio是可以在一個project中新建多個app的,但是不建議這麼用,大概適用於,你的專案有兩個客戶端或者多個客戶端,比如有使用者端和商戶端,這樣在一個project裡面其實比較好的,許多程式碼可以共用
  2. android{}閉包
    裡面都是一些android工程的配置檔案
    1). compileSdkVersion 25 buildToolsVersion “25.0.2”
    compileSdkVersion 用於指定專案的編譯版本api水平
    buildToolsVersion 用於指定專案構建工具的版本
    其實還有defaultConfig{}中的minSdkVersion,targetSdkVersion
    minSdkVersion 專案最低相容的Android系統版本
    targetSdkVersion 目標版本,其實也就是說你編寫這個工程相容的最好版本,但是在更高版本的裝置上也可以執行.注意,如果不到23,也就是Android 6.0 ,執行時許可權自動開啟(不是萬事大吉了,使用者可以在設定裡面關閉,你寫程式碼處理蹦蹦蹦),但是大於等於23,預設關閉,需要在程式碼裡處理.
    2). defaultConfig{}
    applicationId 應用id,類似包名,需要和包名一致,甚至哥曾經寫錯過,把我另外一個相同applicationId的app覆蓋掉了
    versionCode 版本號,整數,內部使用
    versionName 版本名,外部顯示字串
    testInstrumentationRunner 不知道什麼鬼,應該是和ui測試和單元測試用的東西,沒用過,沒了解過,不是很清楚,以後學了,再補充
    vectorDrawables.useSupportLibrary= true 這個我知道,對vector向量圖示的支援,設定開啟,好像引用23的一個版本預設是不開啟的.
    3). buildTypes{}
    編譯型別,debug和release,目前只有release{},你需要的話,可以寫debug,之前在這個裡面寫過是否列印log
    release{}
    minifyEnabled false 是否混淆
    proguardFiles 混淆的規則檔案
    4).sourceSets{}

    sourceSets {
    main {
    jniLibs.srcDirs = ['libs']
    }
    }

    曾經,我不懂事,用了百度地圖的sdk,把so包放到libs裡面了,然後,沒有加這句話,那天我加班到深夜,ccccc
    這段程式碼的意思就是 jniLibs資料夾,也就是so檔案儲存的地方,設定等於到libs裡面
  3. dependencies{}
    1). compile
    compile fileTree (dir: ‘libs’, include: *.jar)
    把libs資料夾裡面的字尾.jar都依賴編譯了
    很多人不懂這個道理,可能把這句話刪了,然後告訴我,jar包放到libs了,為什麼無法引用裡面的類???
    當然如果你任性刪了,也可以的,你可以單獨的引用
    compile files(‘libs/xxx.jar’)
    當然這是比較任性的人
    compile ‘com.android.support:appcompat-v7:25.2.0’
    這是當年,忽悠我用as人說的,引用網路jar方便,確實方便,就是這麼簡單的一句程式碼\
    2). androidTestCompile
    測試的東西,我也不懂,沒用過,用過再裝逼.
    3). testCompile
    同上