Slide 1

Slide 1 text

Understanding Gradle for Android Kevin Pelgrims

Slide 2

Slide 2 text

About me • Mobile Developer at Drivr • Just launched Forward • Working with Android since 2011 • .NET developer in a previous life • Big interest in web technology

Slide 3

Slide 3 text

Schedule • The build file • Groovy basics • Back to the build file • Custom tasks • Tasks for Android • Tips and tricks

Slide 4

Slide 4 text

The build file

Slide 5

Slide 5 text

The build file apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.1.0" defaultConfig { applicationId "com.muchgradle" } } dependencies { compile 'com.android.support:appcompat-v7:23.1.0' }

Slide 6

Slide 6 text

Groovy basics To get Gradle, you need to get Groovy

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Dynamic typing String name = "Andy" def name = 'Andy'

Slide 9

Slide 9 text

String interpolation def name = 'Andy' def greeting = "Hello, $name" def name_size = "Your name is ${name.size()} characters long"

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

The build file

Slide 17

Slide 17 text

Back to the build file apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.1.0" defaultConfig { applicationId "com.muchgradle" } } dependencies { compile 'com.android.support:appcompat-v7:23.1.0' }

Slide 18

Slide 18 text

Back to the build file apply plugin: 'com.android.application' project.apply([plugin: 'com.android.application']);

Slide 19

Slide 19 text

Back to the build file dependencies { compile 'com.android.support:appcompat-v7:23.1.0' } project.dependencies({ add('compile', 'com.android.support:appcompat-v7:23.1.0', { // Configuration statements }); });

Slide 20

Slide 20 text

Back to the build file android { compileSdkVersion 23 buildToolsVersion "23.1.0" defaultConfig { applicationId "com.muchgradle" } } Android plugin: https://developer.android.com/tools/building/plugin-for-gradle.html

Slide 21

Slide 21 text

Custom tasks

Slide 22

Slide 22 text

Gradle build lifecycle Initialization Discover all modules

Slide 23

Slide 23 text

Gradle build lifecycle Initialization Configuration Configure project objects

Slide 24

Slide 24 text

Gradle build lifecycle Initialization Configuration Execution Execute selected tasks

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Ordering tasks (1) task task1 << { println 'Task 1' } task task2 << { println 'Task 2' } task2.mustRunAfter task1 > gradlew task2 task1 task1 task2

Slide 29

Slide 29 text

Ordering tasks (2) task task1 << { println 'Task 1' } task task2 << { println 'Task 2' } task2.dependsOn task1 > gradlew task2 task1 task2

Slide 30

Slide 30 text

Android tasks

Slide 31

Slide 31 text

Hooking into the Android plugin android.applicationVariants.all { variant -> println variant }

Slide 32

Slide 32 text

Hooking into the Android plugin task hello << { println 'Hello' } android.applicationVariants.all { variant -> variant.assemble.dependsOn hello }

Slide 33

Slide 33 text

Automatically renaming APKs android.applicationVariants.all { variant -> variant.outputs.each { output -> } } def file = output.outputFile output.outputFile = new File(file.parent, file.name.replace(".apk", "${variant.versionName}.apk"))

Slide 34

Slide 34 text

Tips and tricks

Slide 35

Slide 35 text

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 36

Slide 36 text

Speeding up the build • Use the latest version of Gradle distributionUrl=https\://services.gradle.org/distributions/ gradle-2.8-all.zip

Slide 37

Slide 37 text

Speeding up the build • Use the latest version of Gradle • Change your Gradle properties org.gradle.parallel=true org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m

Slide 38

Slide 38 text

Speeding up the build • Use the latest version of Gradle • Change your Gradle properties • Build modules separately gradlew :app:build :moduledirectoryname:build

Slide 39

Slide 39 text

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 40

Slide 40 text

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 41

Slide 41 text

Optimizing the APK • ProGuard android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro ...

Slide 42

Slide 42 text

Optimizing the APK • ProGuard • Automatic resource shrinking android { buildTypes { release { minifyEnabled true shrinkResources true ...

Slide 43

Slide 43 text

Optimizing the APK • ProGuard • Automatic resource shrinking • Manual resource shrinking android { defaultConfig { resConfigs "en", "da", "nl" } }

Slide 44

Slide 44 text

Optimizing the APK • ProGuard • Automatic resource shrinking • Manual resource shrinking android { defaultConfig { resConfigs "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" } }

Slide 45

Slide 45 text

Resources

Slide 46

Slide 46 text

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 47

Slide 47 text

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

Slide 48

Slide 48 text

Understanding Gradle for Android twitter.com/kevinpelgrims google.com/+kevinpelgrims kevinpelgrims.com