??Android Studio默認的構建項目工具,基于Groovy的領域特定語言來聲明項目配置寂诱,和基于XML的Ant和Maven相比更簡單。
外層build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
??兩處repositories閉包中都有jcenter()和google()這兩行表示我們可以從jcenter和google中輕松引用任何開源項目撵摆。
??dependencies閉包中聲明的是Grandle引用com.android.tools.build:gradle:3.1.4版本伯顶。
內層build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.administrator.testhelloworld"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
??apply表示這是一個應用程序模塊,一般有兩個值:com.android.application(應用程序模塊鳖眼,可以直接運行)和com.android.library(庫模塊黑毅,需要一來在別的應用程序模塊中才能運行)。
??android閉包钦讳,指定目標版本矿瘦,指定項目構建工具的版本。
??android閉包中defaultConfig閉包愿卒,指定包名缚去,最小支持版本,目標版本琼开,指定項目版本號易结,指定項目的版本名。
??android閉包中buildType閉包,用于指定生成安裝文件的相關配置搞动,有兩個子包:debug和release躏精。debug可以忽略不記。release中minifyEnabled是否對代碼進行混淆鹦肿,proguardFiles指定混淆文件矗烛,第一個proguard-android.txt系統(tǒng)目錄下的,proguard-rules.pro項目目錄下的配置特定的混淆規(guī)則箩溃。
??dependencies閉包瞭吃,指定當前項目所有依賴關系。通常有三種依賴:本地依賴碾篡、庫依賴和遠程依賴虱而。implementation fileTree將libs目錄下所有.jar后綴的文件都添加進項目構建路徑; implementation 'com.android.support.constraint:constraint-layout:1.1.1'遠程依賴开泽;implementation project(':helper')庫依賴