新建一个 android 项目,查看一下 gradle 文件:
settings.gradle
Gradle 使用 settings.gradle
来配置多 gradle 项目构建,新构建的项目的内容如下:
include ':app'
它告诉 Gradle app
目录是一个 Gradle 项目,当通过 as 新建一个 moduel,在 settings.gradle
中也会自动加入相应的目录名称。
gradle-wrapper.properties
gradle-wrapper.properties 用来配置 Gradle Wrapper。
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl
:gradle 某个版本的下载地址
zipStoreBase/zipStorePath
:zip 文件下载后的存储位置
distributionBase/distributionPath
:zip 文件解压后的存储位置
顶层 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// 下载插件
buildscript {
// 插件仓库
repositories {
google()
jcenter()
}
// 插件地址
dependencies {
// android 的 gradle 插件
// 会从 repositories 中声明的地址去找
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
// 配置顶层 project 和子 project 所需要的 dependencies 的地址
allprojects {
repositories {
google()
jcenter()
}
}
// 自定义 Task
task clean(type: Delete) {
delete rootProject.buildDir
}
app build.gradle
// android 插件
// 即顶层 build.gradle 的 buildscript/dependencies 中声明的插件
apply plugin: 'com.android.application'
// android 插件中定义的方法
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.gdeer.gradledemo"
minSdkVersion 15
targetSdkVersion 27
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 是 android 插件中定义的方法
// 会从顶层 build.gradle 中 allprojects/repositories 里声明的地址里找
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}