1、 Gradle 是什么?
gradle跟ant/maven一样,是一种依赖管理/自动化构建工具。但是跟ant/maven不一样, 它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言,面向Java应用为主。这使得它更加简洁、灵活,更加强大的是,gradle 完全兼容maven和ivy。
更多详细介绍可以看它的官网:http://www.gradle.org/
2、为什么要用?
- 更强大的代码提示与便捷操作;
- 更容易配置,扩展;
- 更强大的依赖管理, 版本控制
- 更好的 IDE 集成
AS 中的 gradle
文件目录结构
project 下的build.gradle
// 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 //这个文件是全局的项目配置文件
gradle 文件夹及其子文件
包含
gradle-wrapper.jar
gradle-wrapper.properties
这两个是 gradle 需要的两个文件, 在创建 Project 时自动生成, 不用我们修改