Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Understanding Gradle for Android
Search
Kevin Pelgrims
November 12, 2015
Programming
2
650
Understanding Gradle for Android
Presented at Big Android BBQ Europe 2015
Kevin Pelgrims
November 12, 2015
Tweet
Share
More Decks by Kevin Pelgrims
See All by Kevin Pelgrims
Data binding on Android
kevinpelgrims
5
4.4k
Other Decks in Programming
See All in Programming
Honoの来た道とこれから
yusukebe
19
3k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
130
Amazon Neptuneで始めてみるグラフDB-OpenSearchによるグラフの全文検索-
satoshi256kbyte
4
320
色々なIaCツールを実際に触って比較してみる
iriikeita
0
260
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
240
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
820
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
260
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
390
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
360
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
330
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Building Your Own Lightsaber
phodgson
102
6k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
32
1.8k
Speed Design
sergeychernyshev
24
570
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
Code Reviewing Like a Champion
maltzj
519
39k
Transcript
Understanding Gradle for Android Kevin Pelgrims
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
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: 'com.android.application' android { compileSdkVersion 23
buildToolsVersion "23.1.0" defaultConfig { applicationId "com.muchgradle" } } dependencies { compile 'com.android.support:appcompat-v7:23.1.0' }
Groovy basics To get Gradle, you need to get Groovy
Verbosity System.out.println("Hello, Java"); println("Hello, Java"); println("Hello, Java") println "Hello, Java"
println 'Hello, Groovy'
Dynamic typing String name = "Andy" def name = 'Andy'
String interpolation def name = 'Andy' def greeting = "Hello,
$name" def name_size = "Your name is ${name.size()} characters long"
Methods public int square(int num) { return num * num;
} square(2); def square(def num) { num * num } square 4
Closures def square = { num -> num * num
} square 8 Closure square = { it * it } square 16
Closures void runClosure(Closure closure) { closure() } runClosure({ println 'Yo!'})
runClosure() { println 'Yo!'} runClosure { println 'Yo!'}
Lists List list = [1, 2, 3, 4, 5] list.each
{ element -> println element } list.each { println it }
Maps Map map = [one:1, two:2, three:3] map.get('one') map['two'] map.three
Maps void print(Map args, String message) { println args println
message } print(one:1, two:2, three:3, 'hello')
The build file
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' }
Back to the build file apply plugin: 'com.android.application' project.apply([plugin: 'com.android.application']);
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 }); });
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
Custom tasks
Gradle build lifecycle Initialization Discover all modules
Gradle build lifecycle Initialization Configuration Configure project objects
Gradle build lifecycle Initialization Configuration Execution Execute selected tasks
Defining a task task hello { doLast { println 'Hello,
world!' } } task hello << { println 'Hello, world!' }
Defining a task task hello { println 'Configuration' doLast {
println 'Goodbye' } doFirst { println 'Hello' } }
Ordering task actions task hello { doFirst { println 'Not
really first' } doFirst { println 'First' } doLast { println 'Not really last' } doLast { println 'Last' } }
Ordering tasks (1) task task1 << { println 'Task 1'
} task task2 << { println 'Task 2' } task2.mustRunAfter task1 > gradlew task2 task1 task1 task2
Ordering tasks (2) task task1 << { println 'Task 1'
} task task2 << { println 'Task 2' } task2.dependsOn task1 > gradlew task2 task1 task2
Android tasks
Hooking into the Android plugin android.applicationVariants.all { variant -> println
variant }
Hooking into the Android plugin task hello << { println
'Hello' } android.applicationVariants.all { variant -> variant.assemble.dependsOn hello }
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"))
Tips and tricks
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
Speeding up the build • Use the latest version of
Gradle distributionUrl=https\://services.gradle.org/distributions/ gradle-2.8-all.zip
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
Speeding up the build • Use the latest version of
Gradle • Change your Gradle properties • Build modules separately gradlew :app:build :moduledirectoryname:build
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
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
Optimizing the APK • ProGuard android { buildTypes { release
{ minifyEnabled true proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro ...
Optimizing the APK • ProGuard • Automatic resource shrinking android
{ buildTypes { release { minifyEnabled true shrinkResources true ...
Optimizing the APK • ProGuard • Automatic resource shrinking •
Manual resource shrinking android { defaultConfig { resConfigs "en", "da", "nl" } }
Optimizing the APK • ProGuard • Automatic resource shrinking •
Manual resource shrinking android { defaultConfig { resConfigs "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" } }
Resources
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
Resources I wrote a book! https://www.packtpub.com/ application-development/gradle- android
Understanding Gradle for Android twitter.com/kevinpelgrims google.com/+kevinpelgrims kevinpelgrims.com