47
Understanding Gradle for Android Kevin Pelgrims

Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Embed Size (px)

Citation preview

Page 1: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

UnderstandingGradle for Android

Kevin Pelgrims

Page 2: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Schedule

• The build file

• Groovy basics

• Back to the build file

• Custom tasks

• Tasks for Android

• Tips and tricks

Page 3: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

The build file

Page 4: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

The build file

apply plugin: 'com.android.application'

android {compileSdkVersion 24buildToolsVersion "24.2.0"defaultConfig {

applicationId "com.muchgradle"}

}

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

}

Page 5: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Groovy basicsTo get Gradle, you need to get Groovy

Page 6: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Verbosity

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

println("Hello, Java");

println("Hello, Java")

println "Hello, Java"

println 'Hello, Groovy'

Page 7: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Dynamic typing

String name = "Andy"

def name = 'Andy'

Page 8: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

String interpolation

def name = 'Andy'

def greeting = "Hello, $name"

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

Page 9: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Methods

public int square(int num) {return num * num;

}

square(2);

def square(def num) {num * num

}square 4

Page 10: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Closures

def square = { num ->num * num

}

square 8

Closure square = {it * it

}square 16

Page 11: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Closures

void runClosure(Closure closure) {closure()

}

runClosure({ println 'Yo!'})

runClosure() { println 'Yo!'}

runClosure { println 'Yo!'}

Page 12: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Lists

List list = [1, 2, 3, 4, 5]

list.each { element ->println element

}

list.each { println it }

Page 13: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Maps

Map map = [one:1, two:2, three:3]

map.get('one')

map['two']

map.three

Page 14: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Maps

void print(Map args, String message) {println argsprintln message

}

print(one:1, two:2, three:3, 'hello')

Page 15: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

The build file

Page 16: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Back to the build file

apply plugin: 'com.android.application'

android {compileSdkVersion 24buildToolsVersion "24.2.0"defaultConfig {

applicationId "com.muchgradle"}

}

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

}

Page 17: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Back to the build file

apply plugin: 'com.android.application'

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

Page 18: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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});

});

Page 19: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Back to the build file

android {compileSdkVersion 24buildToolsVersion "24.2.0"defaultConfig {

applicationId "com.muchgradle"}

}

Android plugin:https://developer.android.com/tools/building/plugin-for-gradle.html

Page 20: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Custom tasks

Page 21: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Gradle build lifecycle

Initialization

Discover all modules

Page 22: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Gradle build lifecycle

Initialization

Configuration

Configure project objects

Page 23: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Gradle build lifecycle

Initialization

Configuration

Execution

Execute selected tasks

Page 24: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Defining a task

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

}}

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

}

Page 25: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Defining a task

task hello {println 'Configuration'

doLast {println 'Goodbye'

}

doFirst {println 'Hello'

}

}

Page 26: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Ordering task actions

task hello {

doFirst { println 'Not really first' }

doFirst { println 'First' }

doLast { println 'Not really last' }

doLast { println 'Last' }

}

Page 27: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Ordering tasks (1)

task task1 << {println 'Task 1'

}

task task2 << {println 'Task 2'

}

task2.mustRunAfter task1

> gradlew task2 task1

task1task2

Page 28: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Ordering tasks (2)

task task1 << {println 'Task 1'

}

task task2 << {println 'Task 2'

}

task2.dependsOn task1

> gradlew task2

task1task2

Page 29: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Android tasks

Page 30: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Hooking into the Android plugin

android.applicationVariants.all { variant ->println variant

}

Page 31: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Hooking into the Android plugin

task hello << {println 'Hello'

}

android.applicationVariants.all { variant ->variant.assemble.dependsOn hello

}

Page 32: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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"))

Page 33: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Tips and tricks

Page 34: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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

Page 35: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Speeding up the build

• Use the latest version of Gradle

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

Page 36: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Speeding up the build

• Use the latest version of Gradle

• Change your Gradle properties

org.gradle.parallel=trueorg.gradle.daemon=trueorg.gradle.jvmargs=-Xms256m -Xmx1024m

Page 37: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Speeding up the build

• Use the latest version of Gradle

• Change your Gradle properties

• Build modules separately

gradlew :app:build :moduledirectoryname:build

Page 38: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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

Page 39: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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

Page 40: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Optimizing the APK

• ProGuard

android {buildTypes {

release {minifyEnabled trueproguardFiles getDefaultProguardFile

('proguard-android.txt'), 'proguard-rules.pro...

Page 41: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Optimizing the APK

• ProGuard

• Automatic resource shrinking

android {buildTypes {

release {minifyEnabled trueshrinkResources true...

Page 42: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Optimizing the APK

• ProGuard

• Automatic resource shrinking

• Manual resource shrinking

android {defaultConfig {

resConfigs "en", "da", "nl"}

}

Page 43: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Optimizing the APK

• ProGuard

• Automatic resource shrinking

• Manual resource shrinking

android {defaultConfig {

resConfigs "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}

}

Page 44: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Resources

Page 45: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

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

Page 46: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

Resources

I wrote a book!

https://www.packtpub.com/

application-development/gradle-android

Page 47: Understanding Gradle for Android - GOTO Blog · PDF fileUnderstanding Gradle for Android Kevin Pelgrims. Schedule •The build file •Groovy basics •Back to the build file •Custom

UnderstandingGradle for Android

twitter.com/kevinpelgrims

google.com/+kevinpelgrims

kevinpelgrims.com