Understanding Gradle for Android Kevin Pelgrims Schedule The - - PowerPoint PPT Presentation

understanding gradle for android
SMART_READER_LITE
LIVE PREVIEW

Understanding Gradle for Android Kevin Pelgrims Schedule The - - PowerPoint PPT Presentation

Understanding Gradle for Android Kevin Pelgrims Schedule The build file Groovy basics Back to the build file Custom tasks Tasks for Android Tips and tricks The build file The build file apply plugin:


slide-1
SLIDE 1

Understanding Gradle for Android

Kevin Pelgrims

slide-2
SLIDE 2

Schedule

  • The build file
  • Groovy basics
  • Back to the build file
  • Custom tasks
  • Tasks for Android
  • Tips and tricks
slide-3
SLIDE 3

The build file

slide-4
SLIDE 4

The build file

apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.2.0" defaultConfig { applicationId "com.muchgradle" } } dependencies { compile 'com.android.support:appcompat-v7:24.2.0' }

slide-5
SLIDE 5

Groovy basics

To get Gradle, you need to get Groovy

slide-6
SLIDE 6

Verbosity

System.out.println("Hello, Java"); println("Hello, Java"); println("Hello, Java") println "Hello, Java" println 'Hello, Groovy'

slide-7
SLIDE 7

Dynamic typing

String name = "Andy" def name = 'Andy'

slide-8
SLIDE 8

String interpolation

def name = 'Andy' def greeting = "Hello, $name"

def name_size = "Your name is ${name.size()} characters long"

slide-9
SLIDE 9

Methods

public int square(int num) { return num * num; } square(2); def square(def num) { num * num } square 4

slide-10
SLIDE 10

Closures

def square = { num -> num * num } square 8 Closure square = { it * it } square 16

slide-11
SLIDE 11

Closures

void runClosure(Closure closure) { closure() } runClosure({ println 'Yo!'}) runClosure() { println 'Yo!'} runClosure { println 'Yo!'}

slide-12
SLIDE 12

Lists

List list = [1, 2, 3, 4, 5] list.each { element -> println element } list.each { println it }

slide-13
SLIDE 13

Maps

Map map = [one:1, two:2, three:3] map.get('one') map['two'] map.three

slide-14
SLIDE 14

Maps

void print(Map args, String message) { println args println message } print(one:1, two:2, three:3, 'hello')

slide-15
SLIDE 15

The build file

slide-16
SLIDE 16

Back to the build file

apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.2.0" defaultConfig { applicationId "com.muchgradle" } } dependencies { compile 'com.android.support:appcompat-v7:24.2.0' }

slide-17
SLIDE 17

Back to the build file

apply plugin: 'com.android.application' project.apply([plugin: 'com.android.application']);

slide-18
SLIDE 18

Back to the build file

dependencies { compile 'com.android.support:appcompat-v7:24.2.0' }

project.dependencies({ add('compile', 'com.android.support:appcompat-v7:24.2.0', { // Configuration statements }); });

slide-19
SLIDE 19

Back to the build file

android { compileSdkVersion 24 buildToolsVersion "24.2.0" defaultConfig { applicationId "com.muchgradle" } } Android plugin: https://developer.android.com/tools/building/plugin-for-gradle.html

slide-20
SLIDE 20

Custom tasks

slide-21
SLIDE 21

Gradle build lifecycle

Initialization Discover all modules

slide-22
SLIDE 22

Gradle build lifecycle

Initialization Configuration Configure project objects

slide-23
SLIDE 23

Gradle build lifecycle

Initialization Configuration Execution Execute selected tasks

slide-24
SLIDE 24

Defining a task

task hello { doLast { println 'Hello, world!' } } task hello << { println 'Hello, world!' }

slide-25
SLIDE 25

Defining a task

task hello { println 'Configuration' doLast { println 'Goodbye' } doFirst { println 'Hello' } }

slide-26
SLIDE 26

Ordering task actions

task hello { doFirst { println 'Not really first' } doFirst { println 'First' } doLast { println 'Not really last' } doLast { println 'Last' } }

slide-27
SLIDE 27

Ordering tasks (1)

task task1 << { println 'Task 1' } task task2 << { println 'Task 2' } task2.mustRunAfter task1 > gradlew task2 task1 task1 task2

slide-28
SLIDE 28

Ordering tasks (2)

task task1 << { println 'Task 1' } task task2 << { println 'Task 2' } task2.dependsOn task1 > gradlew task2 task1 task2

slide-29
SLIDE 29

Android tasks

slide-30
SLIDE 30

Hooking into the Android plugin

android.applicationVariants.all { variant -> println variant }

slide-31
SLIDE 31

Hooking into the Android plugin

task hello << { println 'Hello' } android.applicationVariants.all { variant -> variant.assemble.dependsOn hello }

slide-32
SLIDE 32

Automatically renaming APKs

android.applicationVariants.all { variant -> variant.outputs.each { output -> } } def file = output.outputFile

  • utput.outputFile = new File(file.parent,

file.name.replace(".apk", "${variant.versionName}.apk"))

slide-33
SLIDE 33

Tips and tricks

slide-34
SLIDE 34

The Gradle Wrapper

  • It’s there by default
  • It’s everywhere
  • It’s always the right version
  • You can use different versions of Gradle for different projects
slide-35
SLIDE 35

Speeding up the build

  • Use the latest version of Gradle

distributionUrl=https\://services.gradle.org/distributions/ gradle-2.8-all.zip

slide-36
SLIDE 36

Speeding up the build

  • Use the latest version of Gradle
  • Change your Gradle properties
  • rg.gradle.parallel=true
  • rg.gradle.daemon=true
  • rg.gradle.jvmargs=-Xms256m -Xmx1024m
slide-37
SLIDE 37

Speeding up the build

  • Use the latest version of Gradle
  • Change your Gradle properties
  • Build modules separately

gradlew :app:build :moduledirectoryname:build

slide-38
SLIDE 38

Speeding up the build

  • Use the latest version of Gradle
  • Change your Gradle properties
  • Build modules separately
  • Exclude modules from the build

gradlew assemble -x :libraryproject:assemble

slide-39
SLIDE 39

Speeding up the build

  • Use the latest version of Gradle
  • Change your Gradle properties
  • Build modules separately
  • Exclude modules from the build
  • Do some profiling

gradlew task --profile

slide-40
SLIDE 40

Optimizing the APK

  • ProGuard

android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro ...

slide-41
SLIDE 41

Optimizing the APK

  • ProGuard
  • Automatic resource shrinking

android { buildTypes { release { minifyEnabled true shrinkResources true ...

slide-42
SLIDE 42

Optimizing the APK

  • ProGuard
  • Automatic resource shrinking
  • Manual resource shrinking

android { defaultConfig { resConfigs "en", "da", "nl" } }

slide-43
SLIDE 43

Optimizing the APK

  • ProGuard
  • Automatic resource shrinking
  • Manual resource shrinking

android { defaultConfig { resConfigs "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" } }

slide-44
SLIDE 44

Resources

slide-45
SLIDE 45

Resources

  • Groovy SDK
  • http://www.groovy-lang.org/download.html
  • Gradle DSL
  • https://docs.gradle.org/current/dsl/
  • Android plugin documentation
  • https://developer.android.com/tools/building/plugin-for-gradle.html
slide-46
SLIDE 46

Resources

I wrote a book! https://www.packtpub.com/ application-development/gradle- android

slide-47
SLIDE 47

Understanding Gradle for Android

twitter.com/kevinpelgrims google.com/+kevinpelgrims kevinpelgrims.com