一:Project和Module中的gradle腳本的基本格式
- 一:project的gradle腳本如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.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
}
說明:最重要的是buildScript中的代碼,其中指定了jcenter爲代碼倉庫,聲明瞭依賴的gradle插件版本,allprojects 爲全局的一些設置
- 二:Module的gradle腳本
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.testtts"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.0"
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:24.2.1'
testCompile 'junit:junit:4.12'
}
gradle使用的是領域特定語言,因此分析問題的時候只需要定位到特定的位置進行排查即可。
apply plugin: ‘com.android.application’:apply plugin表示了改module是一個application,引入了Android項目的工具,若是library的話就表示該module是一個庫。
android領域:表示在構建過程中所用到的所有參數,默認創建了compileSdkVersion 24
buildToolsVersion “24.0.1”兩個參數,分別表示編譯的SDK的版本和Android build tool的版本defaultConfig:默認的一些配置放在此領域中,可覆蓋清單文件中已經預先定義好的一些配置。
buildTypes:通過構建不同的構建類型,從而生成不同的APK,可以爲構建類型實現不同的參數設置
dependencies:表示該module在構建過程中依賴的所有的庫,可以是jar,也可以是aar,aar的優勢在於相當於依賴了整個項目源碼,可以有資源文件等。
二:gradle 中task的使用
assemble task :爲項目打包,assembleDebug和assembleRelease分別表示打包一個debug的包和一個release的包,命令簡寫爲gradle aD 和gradle aR
clean task:爲清理已經構建好的編譯結果,作用與IDE本身的clean一樣,其他具體的使用方法可進行Google。