1, Gradle what is?
gradle with ant / maven, it is a dependency management / automated build tools. But with ant / maven is not the same, it abandoned the complicated configuration based on a variety of XML, based on the internal replaced by a specific field Groovy (DSL) language, for Java-based applications. This makes it more simple, flexible and more powerful is, gradle fully compatible with maven and ivy.
More details can be seen on its official website: http://www.gradle.org/
2, Why?
- More powerful code hinting and easy to operate;
- Easier to configure, extended;
- More powerful dependency management, version control
- Better integration IDE
AS in gradle
File directory structure
build.gradle under the project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
//指定远程中央仓库
repositories {
google()
//jcenter指向的是: https://jcenter.bintray.com/,兼容maven中心仓库,性能更优
jcenter()
}
//指定整个工程的依赖
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
//所有工程及其modle都使用jcenter、google中央仓库
allprojects {
repositories {
google()
jcenter()
}
}
//执行delete构建时, 删除工程下所有构建产生的文件夹
task clean(type: Delete) {
delete rootProject.buildDir
}
module 下的 build.gradle
// 声明是Android程序
apply plugin: 'com.android.application'
android {
// 指定编译SDK的版本
compileSdkVersion 28
defaultConfig {
// 应用的包名
applicationId "com.example.recyclerdome"
//最小版本
minSdkVersion 16
//目标版本
targetSdkVersion 28
versionCode 1
//应用版本号
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// 是否进行代码混淆
minifyEnabled false
// 混淆配置文件的位置
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
//包含所有依赖的jar或库
dependencies {
// 编译libs目录下的所有jar包
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//测试时才编译junit包
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
settings.gradle // This file is global project configuration files
gradle folder and its subfolders
contain
gradle-wrapper.jar
gradle-wrapper.properties
These two are gradle two required files , creating Project automatically generated , we need not modify