Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding Gradle for Android

Understanding Gradle for Android

Presented at Big Android BBQ Europe 2015

Kevin Pelgrims

November 12, 2015
Tweet

More Decks by Kevin Pelgrims

Other Decks in Programming

Transcript

  1. Understanding
    Gradle for Android
    Kevin Pelgrims

    View full-size slide

  2. 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

    View full-size slide

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

    View full-size slide

  4. The build file

    View full-size slide

  5. 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'
    }

    View full-size slide

  6. Groovy basics
    To get Gradle, you need to get Groovy

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  16. The build file

    View full-size slide

  17. 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'
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. 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

    View full-size slide

  21. Custom tasks

    View full-size slide

  22. Gradle build lifecycle
    Initialization
    Discover all modules

    View full-size slide

  23. Gradle build lifecycle
    Initialization
    Configuration
    Configure project objects

    View full-size slide

  24. Gradle build lifecycle
    Initialization
    Configuration
    Execution
    Execute selected tasks

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. Android tasks

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  34. Tips and tricks

    View full-size slide

  35. 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

    View full-size slide

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

    View full-size slide

  37. 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

    View full-size slide

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

    View full-size slide

  39. 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

    View full-size slide

  40. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide