首发链接
我的博客:https://hubinqiang.com
什么是Gradle?
Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言。
Android Stdio中的Gradle文件
1. project/app/build.gradle
这个文件是peoject项目app这个Module下的Gradle文件。
//声明是Android程序
apply plugin: 'com.android.application'
android {
// 编译SDK的版本
compileSdkVersion 25
// build tools的版本
buildToolsVersion "25.0.2"
defaultConfig {
// 应用的包名
applicationId "com.hubinqiang.lab"
//最小支持SDK的版本
minSdkVersion 19
//最佳设备SDK的版本
targetSdkVersion 25
// 应用程序的版本
versionCode 1
// 应用程序的版本号
versionName "1.0"
// 安卓自动化测试工具
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
// debug模式
debug {
}
// release版本配置
release {
// 是否进行混淆
minifyEnabled false
// 混淆文件的位置
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// 编译libs目录下的所有jar包
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:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
2. project/Gradle
这个目录下有个 wrapper 文件夹,里面可以看到有两个文件,主要看 gradle-wrapper.properties 这个文件的内容:
#Fri Mar 24 21:39:48 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
这里面声明了gradle的目录与下载路径以及当前项目使用的gradle版本,这些默认的路径一般不会更改的,这个文件里指明的gradle版本不对也是很多导包不成功的原因之一。
3. project/build.gradle
这个文件是整个项目的gradle基础配置文件。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// 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
}
内容主要包含了两个方面:一个是声明仓库的源,这里可以看到是指明的jcenter(),jcenter可以理解成是一个新的中央远程仓库,兼容maven中心仓库,而且性能更优。另一个是声明了android gradle plugin的版本。
4. project/settings.gradle
这个文件是全局的项目配置文件,里面主要声明一些需要加入gradle的module。
include ':app'