Gradle 是一个工具,同时它也是一个编程框架。前面也提到过,使用这个工具可以完成app的编译打包等工作。当然你也可以用它干其他的事情。

相关 Gradle的官网:http://gradle.org/

https://docs.gradle.org/current/dsl/

https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html

  • settings.gradle settings.gradle除了可以include外,还可以设置一些函数。这些函数会在gradle构建整个工程任务的时候执行,所以,可以在settings做一些初始化的工作。比如:我的settings.gradle的内容:

    <pre class=”hljs groovy” data-original-code=”” 定义一个名为initminshenggradleenvironment的函数。该函数内部完成一些初始化操作=”” 比如创建特定的目录,设置特定的参数等”=”” data-snippet-id=”ext.da31982bafbbbb24ed75235f36fda35b” data-snippet-saved=”false” data-codota-status=”done”>

    1. //定义一个名为initMinshengGradleEnvironment的函数。该函数内部完成一些初始化操作//比如创建特定的目录,设置特定的参数等
    2. def initMinshengGradleEnvironment(){
    3. println“initialize Minsheng Gradle Environment …..”
    4. ……//干一些special的私活….
    5. println“initialize Minsheng Gradle Environment completes…”
    6. }
    7. //settings.gradle加载的时候,会执行initMinshengGradleEnvironment
    8. initMinshengGradleEnvironment()
    9. //include也是一个函数:
    10. include ‘CPosSystemSdk’ , ‘CPosDeviceSdk’ , ‘CPosSdkDemo’,‘CPosDeviceServerApk’,‘CPosSystemSdkWizarPosImpl’

gradle命令介绍

  1. gradle projects查看工程信息
  2. gradle tasks查看任务信息
  3. gradle task-name执行任务
    列出了好多任务,这时候就可以通过 gradle 任务名来执行某个任务。这和make xxx很像。比如:
    gradle clean是执行清理任务,和make clean类似。
    gradle properites用来查看所有属性信息。
    gradle tasks会列出每个任务的描述,通过描述,我们大概能知道这些任务是干什么的…..。然后gradle task-name执行它就好。
    这里要强调一点:Task和Task之间往往是有关系的,这就是所谓的依赖关系。比如,assemble task就依赖其他task先执行,assemble才能完成*终的输出

Gradle基本类型

gradle基于groovy, gradle提供的基本类型也都实现了script.并且提供了大量的方法和属性

Build script -> Project (每个project的build.gradle也就是一个project)
Init script -> Gradle
Settings script -> Settings (对应setting.gradle)

  • Project 每个project的build.gradle都会转换成一个project
  • Gradle
  • Settings 每一个settings.gradle都会转换成一个Settings对象

Gradle 生命周期

1.初始化创建Settings实例
2.解析settings.gradle 构造各个Project实例
3.解析每个Project对应的build.gradle,配置相应Project

即 setting.gradle -> 各个build.gradle

%title插图%num

Paste_Image.png

%title插图%num

Paste_Image.png

上面*张图是settings.gradle的配置
第二张是执行assemble 打印出来的执行顺序,也就是说settings.gradle先执行
然后是root.gradle (也就是我们根目录下的gradle) 接下来就是按照settings.gradle配置顺序进行执行~

每个对象的方法就不再赘述,api中都有。

实战

root gradle

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories {
  4. mavenCentral()
  5. }
  6. dependencies {
  7. classpath ‘com.android.tools.build:gradle:1.2.3’
  8. }
  9. }
  10. allprojects {//对所有projects都起作用
  11. repositories {
  12. mavenCentral()
  13. }
  14. }
  15. ext {//定义所有project公用参数 使用rootProject.ext.XX就能拿到相应对象
  16. compileSdk = 23
  17. minSdk = 11
  18. targetSdk = 23
  19. support = “23.1.1”
  20. buildTools = “23.0.2”
  21. glide = “3.6.0”
  22. okio = “1.4.0”
  23. okhttp = “2.4.0”
  24. fabric = “2.4.0”
  25. leakcanary = “1.3.1”
  26. logansquare = “1.1.0”
  27. dagger = “2.0.1”
  28. packageName = “com.evilsoulm.keep_nice”
  29. butterknife = “7.0.1”
  30. retrofit = “2.0.0-beta2”
  31. greendao = “2.0.0”
  32. }
  33. task clean(type: Delete) {
  34. delete rootProject.buildDir
  35. }

commonandroid.gradle android项目共用gradle 相当于基类 可以通过apply from 引入

  1. android {
  2. compileSdkVersion rootProject.ext.compileSdk
  3. buildToolsVersion rootProject.ext.buildTools
  4. defaultConfig {
  5. minSdkVersion rootProject.ext.minSdk
  6. targetSdkVersion rootProject.ext.targetSdk
  7. }
  8. compileOptions {
  9. sourceCompatibility JavaVersion.VERSION_1_7
  10. targetCompatibility JavaVersion.VERSION_1_7
  11. }
  12. dexOptions {
  13. javaMaxHeapSize “2048m”
  14. }
  15. dexOptions {
  16. preDexLibraries project.hasProperty(‘debug’)
  17. incremental project.hasProperty(‘debug’)
  18. }
  19. packagingOptions {
  20. exclude ‘.readme’
  21. exclude ‘LICENSE.txt’
  22. exclude ‘META-INF/DEPENDENCIES’
  23. exclude ‘META-INF/dependencies’
  24. exclude ‘META-INF/DEPENDENCIES.txt’
  25. exclude ‘META-INF/dependencies.txt’
  26. }
  27. }

commonproject.gradle project项目共用gradle

  1. repositories {
  2. mavenCentral()
  3. }
  4. apply plugin: ‘findbugs’
  5. apply plugin: ‘checkstyle’
  1. apply plugin: ‘com.android.application’
  2. apply plugin: ‘com.neenbedankt.android-apt’
  3. apply from: rootProject.file(‘basegradle/commonProject.gradle’)//引入commonProject
  4. apply from: rootProject.file(‘basegradle/commonAndroid.gradle’)//引入commonAndroid
  5. buildscript {
  6. repositories {
  7. mavenCentral()
  8. }
  9. dependencies {
  10. classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
  11. }
  12. }
  13. android {
  14. defaultConfig {
  15. applicationId rootProject.ext.packageName
  16. versionCode 1
  17. versionName “1.0”
  18. }
  19. buildTypes {
  20. debug {
  21. minifyEnabled false
  22. debuggable true
  23. jniDebuggable true
  24. }
  25. release {
  26. minifyEnabled true
  27. shrinkResources false
  28. debuggable false
  29. jniDebuggable false
  30. zipAlignEnabled true
  31. proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
  32. }
  33. }
  34. configurations {
  35. all*.exclude group: ‘com.google.android’, module: ‘android’
  36. all*.exclude group: ‘com.google.android’, module: ‘support-v4’
  37. all*.exclude group: ‘asm’, module: ‘asm’
  38. all*.exclude group: ‘com.google.code.gson’
  39. }
  40. productFlavors {//很棒的一个参数可以在底下更改包名,资源等。。
  41. normal {
  42. }
  43. ddebug {
  44. }
  45. }
  46. }
  47. dependencies {
  48. compile project(‘:common’)
  49. compile project(‘:model’)
  50. compile fileTree(dir: ‘libs’, include: [‘*.jar’])
  51. compile ‘com.android.support:appcompat-v7:’ + rootProject.ext.support
  52. compile ‘com.android.support:design:’ + rootProject.ext.support
  53. compile ‘com.squareup.okio:okio:’ + rootProject.ext.okio
  54. compile(‘com.squareup.okhttp:okhttp:’ + rootProject.ext.okhttp) {
  55. exclude group: ‘com.squareup.okio’, module: ‘okio’
  56. }
  57. //图片加载
  58. compile ‘com.github.bumptech.glide:glide:’ + rootProject.ext.glide
  59. //leakcanary
  60. debugCompile ‘com.squareup.leakcanary:leakcanary-android:’ + rootProject.ext.leakcanary
  61. releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:’ + rootProject.ext.leakcanary
  62. //logansquare
  63. apt ‘com.bluelinelabs:logansquare-compiler:’ + rootProject.ext.logansquare
  64. compile ‘com.bluelinelabs:logansquare:’ + rootProject.ext.logansquare
  65. //dagger2 注入
  66. apt ‘com.google.dagger:dagger-compiler:’ + rootProject.ext.dagger
  67. compile ‘com.google.dagger:dagger:’ + rootProject.ext.dagger
  68. //UI注入
  69. compile ‘com.jakewharton:butterknife:’ + rootProject.ext.butterknife
  70. //rxjava
  71. compile ‘io.reactivex:rxjava:1.0.10’
  72. compile ‘io.reactivex:rxandroid:0.24.0’
  73. //网络框架
  74. compile ‘com.squareup.retrofit:retrofit:’ + rootProject.ext.retrofit
  75. compile ‘com.squareup.retrofit:converter-gson:’ + rootProject.ext.retrofit
  76. compile ‘com.squareup.retrofit:adapter-rxjava:’ + rootProject.ext.retrofit
  77. }

写法2

另一种写法就是只有一个root.build 通过configure进行每个module的配置 ,每个module下的build.gradle就可以删除了~统一在root.build下进行管理
eg.只写了一个module

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath ‘com.android.tools.build:gradle:1.2.3’
  7. }
  8. }
  9. allprojects {
  10. repositories {
  11. mavenCentral()
  12. }
  13. }
  14. ext {
  15. compileSdk = 23
  16. minSdk = 11
  17. targetSdk = 23
  18. support = “23.1.1”
  19. buildTools = “23.0.2”
  20. glide = “3.6.0”
  21. okio = “1.4.0”
  22. okhttp = “2.4.0”
  23. fabric = “2.4.0”
  24. leakcanary = “1.3.1”
  25. logansquare = “1.1.0”
  26. dagger = “2.0.1”
  27. packageName = “com.evilsoulm.keep_nice”
  28. butterknife = “7.0.1”
  29. retrofit = “2.0.0-beta2”
  30. greendao = “2.0.0”
  31. }
  32. configure(project(‘:app’).subprojects) {//相当于放在app下的build.gradle
  33. apply plugin: ‘com.android.application’
  34. apply plugin: ‘com.neenbedankt.android-apt’
  35. apply from: rootProject.file(‘basegradle/commonProject.gradle’)
  36. apply from: rootProject.file(‘basegradle/commonAndroid.gradle’)
  37. buildscript {
  38. repositories {
  39. mavenCentral()
  40. }
  41. dependencies {
  42. classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
  43. }
  44. }
  45. android {
  46. defaultConfig {
  47. applicationId rootProject.ext.packageName
  48. versionCode 1
  49. versionName “1.0”
  50. }
  51. buildTypes {
  52. debug {
  53. minifyEnabled false
  54. debuggable true
  55. jniDebuggable true
  56. }
  57. release {
  58. minifyEnabled true
  59. shrinkResources false
  60. debuggable false
  61. jniDebuggable false
  62. zipAlignEnabled true
  63. proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
  64. }
  65. }
  66. configurations {
  67. all*.exclude group: ‘com.google.android’, module: ‘android’
  68. all*.exclude group: ‘com.google.android’, module: ‘support-v4’
  69. all*.exclude group: ‘asm’, module: ‘asm’
  70. all*.exclude group: ‘com.google.code.gson’
  71. }
  72. productFlavors {
  73. normal {
  74. }
  75. ddebug {
  76. }
  77. }
  78. }
  79. dependencies {
  80. compile project(‘:common’)
  81. compile project(‘:model’)
  82. compile fileTree(dir: ‘libs’, include: [‘*.jar’])
  83. compile ‘com.android.support:appcompat-v7:’ + rootProject.ext.support
  84. compile ‘com.android.support:design:’ + rootProject.ext.support
  85. compile ‘com.squareup.okio:okio:’ + rootProject.ext.okio
  86. compile(‘com.squareup.okhttp:okhttp:’ + rootProject.ext.okhttp) {
  87. exclude group: ‘com.squareup.okio’, module: ‘okio’
  88. }
  89. //图片加载
  90. compile ‘com.github.bumptech.glide:glide:’ + rootProject.ext.glide
  91. //leakcanary
  92. debugCompile ‘com.squareup.leakcanary:leakcanary-android:’ + rootProject.ext.leakcanary
  93. releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:’ + rootProject.ext.leakcanary
  94. //logansquare
  95. apt ‘com.bluelinelabs:logansquare-compiler:’ + rootProject.ext.logansquare
  96. compile ‘com.bluelinelabs:logansquare:’ + rootProject.ext.logansquare
  97. //dagger2 注入
  98. apt ‘com.google.dagger:dagger-compiler:’ + rootProject.ext.dagger
  99. compile ‘com.google.dagger:dagger:’ + rootProject.ext.dagger
  100. //UI注入
  101. compile ‘com.jakewharton:butterknife:’ + rootProject.ext.butterknife
  102. //rxjava
  103. compile ‘io.reactivex:rxjava:1.0.10’
  104. compile ‘io.reactivex:rxandroid:0.24.0’
  105. //网络框架
  106. compile ‘com.squareup.retrofit:retrofit:’ + rootProject.ext.retrofit
  107. compile ‘com.squareup.retrofit:converter-gson:’ + rootProject.ext.retrofit
  108. compile ‘com.squareup.retrofit:adapter-rxjava:’ + rootProject.ext.retrofit
  109. }
  110. }

属性

如果是单个脚本,则不需要考虑属性的跨脚本传播,但是Gradle往往包含不止一个build.gradle文件,比如我设置的utils.gradle,settings.gradle。如何在多个脚本中设置属性呢?
Gradle提供了一种名为extra property的方法。extra property是额外属性的意思,在*次定义该属性的时候需要通过ext前缀来标示它是一个额外的属性。定义好之后,后面的存取就不需要ext前缀了。ext属性支持Project和Gradle对象。即Project和Gradle对象都可以设置ext属性

在上面的例子中其实我们已经用到了ext来定义一些常亮,eg:packageName,compileversion等
现在结合groovy相关知识,再重构下上面的gradle配置.

定义一个const.gradle用来管理我们的常亮和各个库所依赖的库.

  1. ext {
  2. android_const = [
  3. compileSdk : 23,
  4. minSdk : 11,
  5. targetSdk : 23,
  6. support : “23.1.1”,
  7. buildTools : “23.0.2”,
  8. packageName: “com.evilsoulm.keep_nice”
  9. ]
  10. dep_const = [
  11. glide : “3.6.0”,
  12. okio : “1.4.0”,
  13. okhttp : “2.4.0”,
  14. fabric : “2.4.0”,
  15. leakcanary : “1.3.1”,
  16. logansquare: “1.1.0”,
  17. dagger : “2.0.1”,
  18. butterknife: “7.0.1”,
  19. retrofit : “2.0.0-beta2”,
  20. greendao : “2.0.0”
  21. ]
  22. app_dep = [
  23. “appcompat-v7” : ‘com.android.support:appcompat-v7:’ + android_const.support,
  24. “design” : ‘com.android.support:design:’ + android_const.support,
  25. “okio” : ‘com.squareup.okio:okio:’ + dep_const.okio,
  26. “okhttp” : ‘com.squareup.okhttp:okhttp:’ + dep_const.okhttp,
  27. “glide” : ‘com.github.bumptech.glide:glide:’ + dep_const.glide,
  28. “debug_leakcanary” : ‘com.squareup.leakcanary:leakcanary-android:’ + dep_const.leakcanary,
  29. “release_leakcanary”: ‘com.squareup.leakcanary:leakcanary-android-no-op:’ + dep_const.leakcanary,
  30. “glide” : ‘com.github.bumptech.glide:glide:’ + dep_const.glide,
  31. “apt_logansquare” : ‘com.bluelinelabs:logansquare-compiler:’ + dep_const.logansquare,
  32. “logansquare” : ‘com.bluelinelabs:logansquare:’ + dep_const.logansquare,
  33. “apt_dagger” : ‘com.google.dagger:dagger-compiler:’ + dep_const.dagger,
  34. “dagger” : ‘com.google.dagger:dagger:’ + dep_const.dagger,
  35. “butterknife” : ‘com.jakewharton:butterknife:’ + dep_const.butterknife,
  36. “rxjava” : ‘io.reactivex:rxjava:1.0.10’,
  37. “rxandroid” : ‘io.reactivex:rxandroid:0.24.0’,
  38. “retrofit” : ‘com.squareup.retrofit:retrofit:’ + dep_const.retrofit,
  39. “converter-gson” : ‘com.squareup.retrofit:converter-gson:’ + dep_const.retrofit,
  40. “adapter-rxjava” : ‘com.squareup.retrofit:adapter-rxjava:’ + dep_const.retrofit
  41. ]
  42. model_generator_dep = [
  43. “greendao-generator”: ‘de.greenrobot:greendao-generator:’ + dep_const.greendao
  44. ]
  45. model_dep = [
  46. “greendao”: ‘de.greenrobot:greendao:’ + dep_const.greendao,
  47. “android” : ‘com.google.android:android:4.1.1.4’
  48. ]
  49. }

根目录的build.gradle

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. apply from: “basegradle/const.gradle” //引入const.gradle 有点继承的感觉 也就是现在rootProject也就拥有const.gradle里的方法和变量
  3. buildscript {
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. classpath ‘com.android.tools.build:gradle:1.2.3’
  9. }
  10. }
  11. allprojects {
  12. repositories {
  13. mavenCentral()
  14. }
  15. }

使用
//老的版本

  1. apply plugin: ‘java’
  2. dependencies {
  3. compile ‘de.greenrobot:greendao:’ + rootProject.ext.greendao
  4. compile(group: ‘com.google.android’, name: ‘android’, version: ‘4.1.1.4’)
  5. }

改造后 简单清楚

  1. apply plugin: ‘java’
  2. dependencies {
  3. compile rootProject.ext.model_dep.greendao
  4. compile rootProject.ext.model_dep.android
  5. }

查看依赖树

gradle dependencies
  1. task helloShortCut << {
  2. println ‘short cut’
  3. }
  4. //打印0道9
  5. task count << {
  6. 10.times { print(“$it “) }
  7. }
  8. task intro(dependsOn: helloShortCut) << {
  9. print “i am gradle”
  10. }
  11. //动态task
  12. 10.times {
  13. time ->
  14. task “task$time” << {
  15. println ” i am task number $time”
  16. }
  17. }
  18. task8.dependsOn task1, task2, task3, task4
  19. task hello << {
  20. println ‘Hello Earth’
  21. }
  22. hello.doFirst {
  23. println ‘Hello Venus’
  24. }
  25. hello.doLast {
  26. println ‘Hello Mars’
  27. }
  28. hello << {
  29. println ‘Hello Jupiter’
  30. }
  31. task loadFile << {
  32. def files = file(‘../antLoadfileResources’).listFiles().sort();
  33. files.each {
  34. File file ->
  35. if (file.isFile()) {
  36. }
  37. }
  38. }
  39. gradle.taskGraph.whenReady {taskGraph ->
  40. if (taskGraph.hasTask(“assembleSpeedCompilation”)) {
  41. println ‘_____________________________assembleSpeedCompilationDebug’
  42. } else {
  43. println ‘_____________________________–不包含’
  44. }
  45. }
  1. //显示依赖包的存储路径task
  2. showRemoteDependencies << {
  3. configurations.compile.each { println it
  4. }}

demo->github