1. 程式人生 > >Gradle相關的配置檔案

Gradle相關的配置檔案

  • Gradle 是一種構建工具。
  • 那麼什麼又是構建工具呢?

    Android Gradle 構建工具是一個執行時用於處理 module 下的 build.gradle 檔案的,在這個檔案傳遞到 Gradle 去做進一步操作之前進行的。
    
  • 以NotePad 專案為例,來稍微介紹下一個完整的 Android 專案包含的基本 Gradle 相關的配置檔案:

這裡寫圖片描述

  • 首先來看一下app目錄下的NotePad/app/src/build.gradle檔案中的內容:

    • compileSdkVersion 25,說明要執行該原始碼,你必選已經安裝了android API 25。

    • buildToolsVersion 25.0.2 ,說明要執行該原始碼,你必須已經安裝了 android sdk build-tools21.1.2。

    • minSdkVerison 表示向下低至android API 19,即androd 5.0和5.0以上的版本都可以執行該工程。
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId 'online.xingge.notepad'
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { } } dependencies { compile fileTree(include
: ['*.jar'], dir: 'libs') 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.1.1' testCompile 'junit:junit:4.12' }
  • 再來看一下NotePad/gradle中的內容,可以看到裡面聲明瞭 gradle 的目錄與下載路徑以及當前專案使用的 gradle 版本,這些預設的路徑一般不用更改的,這個檔案裡指明的 gradle 版本不對也是很多導包不成功的原因之一。
#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 
  • NotePad/settings.gradle

這個檔案是全域性的專案配置檔案,裡面主要宣告一些需要加入 gradle 的 module。

  • 那麼如何正確匯入下載的開源專案呢?

    1. 先開啟每個 module下的 gradle 檔案,即 app 目錄下的build.gradle 以及各個 library 下的 build.gradle。    
    
    2. 首先檢視 compileSdkVersion 和buildToolsVersion,因為有些時候你本地的版本和下載的版本不一致,那麼就會導致失敗。 
    
    3. 然後就是檢查 gradle-wrapper ,Google 有些時候要求不同的 Adroid Studio 支援不同的 gradle 版本。手動更新下android gradle plugin 的版本,然後重新同步下。
    
  • 這樣基本就可以匯入執行啦,如果還有錯誤,那就是本身環境問題或其他原因,可以嘗試解除安裝Android Studio重灌,親測有用。

作者:趙磊:原文地址