Gradle认识
Android Studio 的核心是Google新推出的Gradle编译系统。gradle用于替代Eclipse所使用的ant作为android的编译工具,相对于ant编译工具,gradle吸纳了ant的脚本特性、Maven丰富的依赖管理策略和强大的插件式环境。所以它比ant上手要难。
gradle只是构建工具,而新的版本总是在更迭,所以使用Gradle Wrapper将会是一个好的选择去避免由于gradle版本更新的问题。Gradle wrapper 提供了一个windows的batch文件和其他系统的shell文件,当你使用这些脚本的时候,当前gradle版本自动下载,并且自动构建项目,所以每个开发者在构建自己app的时候只需要使用Wrapper,所以开发者不需要为你的电脑安装任何gradle版本,在mac上只需要运行gradlew,windows 上你只需要运行gradlew.bat。
如何学习Gradle
Gradle 本身是基于Groovy脚本语言进行构建的,并通过DSL语言进行描述和控制构建逻辑。如果读者需要更深入了解可以直接参考官方文档 http://gradle.org/documentation
开发者可以根据自己的需要进行学习:
Gradle用户开发指南,https://docs.gradle.org/current/release-notes?_ga=1.254652552.1054415640.1473667985。
Gradle DSL 语言API,https://docs.gradle.org/current/dsl/index.html
从一个android 工程认识Gradle
在根目录下,包含(build.gradle)、(gradle.properties)、(local.properties)、(setting.gradle)这四个Gradle构建文件,此外在module app下还有一个(build.gradle)。整个结构类似于android源代码编译工具–Mark。在Mark工具中,在每个模块的目录下使用一个makefile进行代码的组织与编译逻辑控制,除了Project的build.gradle和setting.gradle外其他的gradle都是Gradle构建工具的配置文
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.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 ,在buildscript 中指定使用了jcenter代码仓库,同时声明依赖android Gradle插件的版本
在allprojects 中,开发者可以为项目整体配置一些属性。
Module build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "cn.hodi.hoditenant"
minSdkVersion 14
targetSdkVersion 21
versionCode getVersionCode()
versionName getVersionName()
}
signingConfigs {
releaseConfig {
storeFile file('../../HodiRent_keyStore/hodirent.jks')
keyAlias 'HodiRent'
File secureFile = file("./secure.properties");
if (secureFile.exists()) {
Properties props = new Properties()
props.load(new FileInputStream(secureFile))
storePassword props.getProperty('key.store.password')
keyPassword props.getProperty('key.alias.password')
}
else {
println "Cann't find hodirent.jks."
}
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
repositories {
maven { url "https://jitpack.io" }
flatDir {
dirs 'aar'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':msqframework')
compile(name:'hodirentnetworkservice', ext:'aar')
testCompile 'junit:junit:4.12'
compile files('libs/alipaySdk-20151103.jar')
compile files('libs/afinal_0.5.1_bin.jar')
compile files('libs/eventbus.jar')
compile files('libs/picasso-2.4.0.jar')
compile files('libs/jmessage-android-1.2.5.jar')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.github.orhanobut:logger:1.12'
compile 'homhomlin.lib:apsts:1.2.0'
compile 'com.android.support:percent:23.4.0'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'nl.qbusict:cupboard:2.1.4'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'cn.bingoogolapple:bga-refreshlayout:1.1.5'
compile 'com.github.PhilJay:MPAndroidChart:v2.1.5'
compile 'com.tencent.bugly:crashreport:latest.release'
compile 'com.alipay.euler:andfix:0.3.1@aar'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
}
//生成签名的apk: app-release.apk后,复制到发布的文件夹,用于加密处理
//检查是否已签名: jarsigner -verbose -certs -verify app/build/outputs/apk/app-release.apk
task copyReleaseAPKToDis(type: Copy, dependsOn:'assembleRelease') {
from('./build/outputs/apk')
into('../../../distribute_apk')
include('app-release.apk')
}
这里的信息比前面的build.gradle要复杂多了,Gradle是使用了DSL语言,即领域特定语言。
Gradle的思考
学习gradle最好的方法就是在实践中学习,Groovy对于Gradle,就好比java对于android,了解一些基本的Groovy对于掌握Gradle是非常有必要的。Groovy是增强java唯一脚本语言。
Groovy的特点
Groovy是一种基于JVM的动态语言,可以运行在java虚拟机上的脚本语言- 与java使用一套注释系统,语句末尾不用分号表示结束
- 不用指定变量、函数类型,使用动态类型
- 不用指定返回值,以最后一行代码为返回值
- 自动为属性添加get/set方法
- 大量使用闭包
如果想详细了解可以参考Gradle in Action 这本书
【Gradle 实战中文版】