gradle

 摘自:http://www.javashuo.com/article/p-duknctxq-he.htmlandroid

buildscript {
    repositories {
        jcenter()
    }

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

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.1.0"
}

 buildscript { ... } 配置了用於驅動構建的代碼。上述代碼聲明瞭項目使用 jCenter 倉庫,而且聲明瞭一個 jCenter 文件的 classpath。該文件聲明瞭項目的 Android Gradle 插件版本爲 1.3.1。segmentfault

接着,使用了 com.android.application 插件。該插件用於編譯 Android 應用app

android { ... } 配置了全部 android 構建所需的參數,這也是 Android DSL 的入口點。默認狀況下,只有compileSdkVersion 和 buildtoolsVersion 這兩個屬性是必須的。ide

android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }

        jnidebug {
            initWith(buildTypes.debug)
            packageNameSuffix ".jnidebug"
            jnidebugBuild true
        }
    }
}
  • 配置默認的 debug Build Type:
    • 設置包名爲 <app appliationId>.debug,以便可以在同一個設備上安裝 debug 和 release 版的 apk
  • 建立了名爲 jnidebug 的新 BuildType,而且以 debug 的配置做爲默認配置。
  • 繼續配置 jnidebug,開啓了 JNI 組件的 debug 功能,並添加了包名後綴。
android {
    signingConfigs {
        debug {
            storeFile file("debug.keystore")
        }

        myConfig {
            storeFile file("other.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }

    buildTypes {
        foo {
            signingConfig signingConfigs.myConfig
        }
    }
}

以上代碼片斷指定了 debug keystore 的路徑在項目根目錄下。其餘使用了相同配置的 Build Types 亦會受影響,在該例子中爲 debug Build Type。該代碼片斷同時還建立了新的 Signing Config 及使用該簽名配置的Build Type測試

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

本地包依賴gradle

  • compile 編譯主 moudle
  • androidTestCompile 編譯主 moudle 的測試
  • debugCompile debug Build Type 的編譯
  • releaseCompile release Build Type 的編譯
repositories {
    jcenter()
}


dependencies {
    compile 'com.google.guava:guava:18.0'
}

遠程包依賴ui

Gradle 支持從 Maven 或 Ivy 倉庫中拉取依賴文件。首先必須將倉庫添加到列表中,而後必須在 dependencies 中添加 Maven 或 Ivy 聲明的包。google

include ':app', ':libraries:lib1', ':libraries:lib2'

其中 setting.gradle 的內容很是簡單。該文件定義了各 Gradle 項目的位置spa

productFlavors {
        flavor1 {
            ...
        }

        flavor2 {
            ...
        }
    }

每個 Build Type 都會生成新的 APK。Product Flavors 一樣也會作這些事情:項目的輸出將會組合全部的 Build Types 和 Product Flavors(若是有定義 Flavor)。每一種組合(包含 Build Type和 Product Flavor)就是一個 Build Variant(構建變種版本)。插件

android {
    ...

    defaultConfig {
        minSdkVersion 8
        versionCode 10
    }

    productFlavors {
        flavor1 {
            packageName "com.example.flavor1"
            versionCode 20
        }

        flavor2 {
            packageName "com.example.flavor2"
            minSdkVersion 14
        }
    }
}

ProductFlavor 類型的 android.productFlavors.* 對象與 android.defaultConfig 對象的類型是相同的。就是說着它們之間屬性共享。

defaultConfig 爲全部的 flavor 提供基本的配置,每一個 flavor 均可以重寫這些配置的值。

BuildConfig 包含的值以下:

  • boolean DEBUG —— 當前構建是否開啓了 debuggable
  • int VERSION_CODE
  • String VERSION_NAME
  • String APPLICATION_ID
  • String BUILD_TYPE —— build type 的名字,例:"release"
  • String FLAVOR —— flavor 的名字,例:"paidapp"
相關文章
相關標籤/搜索