一:Project和Module中的gradle脚本的基本格式
- 一:project的gradle脚本如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// 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
}
说明:最重要的是buildScript中的代码,其中指定了jcenter为代码仓库,声明了依赖的gradle插件版本,allprojects 为全局的一些设置
- 二:Module的gradle脚本
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.testtts"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
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:24.2.1'
testCompile 'junit:junit:4.12'
}
gradle使用的是领域特定语言,因此分析问题的时候只需要定位到特定的位置进行排查即可。
apply plugin: ‘com.android.application’:apply plugin表示了改module是一个application,引入了Android项目的工具,若是library的话就表示该module是一个库。
android领域:表示在构建过程中所用到的所有参数,默认创建了compileSdkVersion 24
buildToolsVersion “24.0.1”两个参数,分别表示编译的SDK的版本和Android build tool的版本defaultConfig:默认的一些配置放在此领域中,可覆盖清单文件中已经预先定义好的一些配置。
buildTypes:通过构建不同的构建类型,从而生成不同的APK,可以为构建类型实现不同的参数设置
dependencies:表示该module在构建过程中依赖的所有的库,可以是jar,也可以是aar,aar的优势在于相当于依赖了整个项目源码,可以有资源文件等。
二:gradle 中task的使用
assemble task :为项目打包,assembleDebug和assembleRelease分别表示打包一个debug的包和一个release的包,命令简写为gradle aD 和gradle aR
clean task:为清理已经构建好的编译结果,作用与IDE本身的clean一样,其他具体的使用方法可进行Google。