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

Gradle Build Systems

Gradle Build Systems

Uncover Gradle's mysteries, learn how to write tasks, configure ProGuard, manage dependencies and more!

Krunal Kapadiya

January 21, 2017
Tweet

More Decks by Krunal Kapadiya

Other Decks in Technology

Transcript

  1. Gradle Build Systems
    Krunal Kapadiya
    Jr Android developer
    @

    View full-size slide

  2. How is android application builds?

    View full-size slide

  3. Gradle Introduction

    View full-size slide

  4. What is Gradle?

    View full-size slide

  5. Why Gradle?
    ADT Plugin Release Notes
    Android Studio is now the official IDE for Android, so you should migrate your
    projects to Android Studio as soon as possible.

    View full-size slide

  6. 1. Download latest gradle version
    2. Set environment variables
    3. In command prompt,
    type gradle -version
    Installing Gradle

    View full-size slide

  7. Say hello to Gradle
    Write sample code to say “hello” to gradle In command line, type gradle hello
    task hello {
    description “Hey MADMeetup! Don’t say
    I like your presentation, love it :D”
    group “Mumbai Android Developers”
    doLast {
    println description
    println group
    println “Hello World!”
    }
    }

    View full-size slide

  8. Gradle Structure In Android Studio
    root
    --gradleScript
    ----dependencies.gradle
    --module 1
    ----build.gradle
    --build.gradle

    View full-size slide

  9. Gradle Build Execution
    Initialization
    Configuration
    Execution

    View full-size slide

  10. Gradle Tips and Recipes

    View full-size slide

  11. Change default source set
    configurations
    Manage projects
    and sources
    // use this in app level gradle file
    // folder gradlerecepies\app\src\other
    android{
    sourceSets {
    main {
    java.srcDirs = ['other/java']
    res.srcDirs = ['other/res1',’other/res2']
    manifest.srcFile 'other/AndroidManifest.xml'
    }
    androidTest {
    setRoot 'src/test'
    }
    }
    }

    View full-size slide

  12. Manage projects
    and sources
    Configure project-wide properties // write this in project level gradle
    ext {
    // define properties
    compileSdkVersion = 25
    buildToolsVersion = "25.0.0"
    supportLibVersion = "25.0.0"
    // Contains defaultConfig files
    versionCode = 1
    versionName = "1.0"
    }

    View full-size slide

  13. Manage projects
    and sources
    Configure project-wide properties
    // write this in app level gradle
    android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
    // ...
    versionCode rootProject.ext.versionCode
    versionName rootProject.ext.versionName
    }
    }
    dependencies {
    compile "com.android.support:appcompat-v7:
    ${rootProject.ext.supportLibVersion}"
    compile "com.android.support:recyclerview-v7:
    ${rootProject.ext.supportLibVersion}"
    }

    View full-size slide

  14. Manage libraries
    and dependencies
    Target specific builds with dependency
    configurations
    android {
    //...
    configurations {
    freeDebugApk {}
    }
    productFlavors {
    free {
    versionCode rootProject.ext.versionCode
    versionName rootProject.ext.versionName
    }
    }
    }
    dependencies {
    freeCompile
    "com.android.support:appcompat-v7:${rootProject.ext.supportLi
    bVersion}"
    freeCompile 'com.google.firebase:firebase-ads:9.8.0'
    freeDebugApk fileTree(dir: 'libs', include: ['*.jar'])
    }

    View full-size slide

  15. Manage libraries
    and dependencies
    Publish non-default variants of your
    library
    android {
    //...
    publishNonDefault true
    configurations {
    demoDebugCompile {}
    fullReleaseCompile {}
    }
    }
    dependencies {
    demoDebugCompile project(path: ':app', configuration: 'demoDebug')
    fullReleaseCompile project(path: ':fullRelease', configuration:
    'fullRelease')
    }

    View full-size slide

  16. Manage libraries
    and dependencies
    Publish non-default variants of your
    library
    android {
    //...
    defaultPublishConfig "app"
    configurations {
    demoDebugCompile {}
    fullReleaseCompile {}
    }
    }
    dependencies {
    demoDebugCompile project(path: ':app', configuration: 'demoDebug')
    fullReleaseCompile project(path: ':fullRelease', configuration:
    'fullRelease')
    }

    View full-size slide

  17. Configure multiple APK support
    1. Configure separate APKs per screen
    density
    2. Configure separate APKs per ABI
    Create different
    versions of your
    app
    android {
    splits {
    // Configures multiple APKs based on screen density.
    density {
    // Enables building multiple APKs.
    enable true
    exclude "ldpi", "mdpi"
    // APKs for:
    //include "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"
    compatibleScreens 'normal', 'large', 'xlarge'
    }
    }
    }
    Configure separate APKs per screen density

    View full-size slide

  18. Configure multiple APK support
    1. Configure separate APKs per screen
    density
    2. Configure separate APKs per ABI
    Create different
    versions of your
    app
    Configure separate APKs per ABI
    android {
    //...
    splits {
    abi {
    // Enables building multiple APKs.
    enable true
    reset()
    include "x86", "armeabi-v7a", "mips"
    universalApk true
    }
    }
    }

    View full-size slide

  19. Configure dynamic version codes
    Create different
    versions of your
    app android {
    defaultConfig {
    //...
    versionCode 4
    }
    }
    ext.abiCodes = ['armeabi-v7a':1, mips:2, x86:3]
    import com.android.build.OutputFile
    android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
    def baseAbiVersionCode =
    project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
    if (baseAbiVersionCode != null) {
    output.versionCodeOverride =
    baseAbiVersionCode * 1000 + variant.versionCode
    }
    }
    }

    View full-size slide

  20. Combine multiple product flavors
    Create different
    versions of your
    app
    android {
    //...
    flavorDimensions "api", "mode"
    productFlavors {
    demo {
    dimension "mode"
    }
    full {
    dimension "mode"
    }
    minApi24 {
    dimension "api"
    minSdkVersion '24'
    versionCode 30000 + android.defaultConfig.versionCode
    versionNameSuffix "-minApi24"
    }
    minApi23 {
    dimension "api"
    minSdkVersion '23'
    versionCode 20000 + android.defaultConfig.versionCode
    versionNameSuffix "-minApi23"
    }
    minApi21 {
    dimension "api"
    minSdkVersion '21'
    versionCode 10000 + android.defaultConfig.versionCode
    versionNameSuffix "-minApi21"
    }
    }
    }

    View full-size slide

  21. Filter variants
    Create different
    versions of your
    app
    android {
    flavorDimensions "api", "mode"
    productFlavors {
    demo {flavorDimension "api"}
    full {flavorDimension "mode"}
    minApi24 {
    flavorDimension "api"
    }
    minApi23 {
    flavorDimension "api"
    }
    minApi21 {
    flavorDimension "api"
    }
    }
    variantFilter { variant ->
    def names = variant.flavors*.name
    if (names.contains("minApi21") && names.contains("demo")) {
    setIgnore(true)
    }
    }
    }

    View full-size slide

  22. Why are build
    types distinct
    from product
    flavors?

    View full-size slide

  23. Test your app
    Configure lint options
    android {
    lintOptions {
    disable 'TypographyFractions','TypographyQuotes'
    enable 'RtlHardcoded', 'RtlCompat', 'RtlEnabled'
    check 'NewApi', 'InlinedApi'
    quiet true
    abortOnError false
    ignoreWarnings true
    }
    }

    View full-size slide

  24. Test your app
    Change the test build type
    android {
    //…
    testBuildType “staging”
    }

    View full-size slide

  25. android {
    // ...
    buildTypes {
    release {
    minifyEnabled true
    //for simple project use file, proguard-android-optimize.txt
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
    'proguard-rules.pro'
    }
    }
    productFlavors {
    flavor1 {
    //...
    }
    flavor2 {
    proguardFile 'flavor2-rules.pro'
    }
    }
    }
    Optimize your build
    Shrink your code

    View full-size slide

  26. Optimize your build
    Enable code shrinking with Instant Run
    android {
    buildTypes {
    debug {
    minifyEnabled true
    useProguard false
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
    'proguard-rules.pro'
    }
    release {
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
    'proguard-rules.pro'
    }
    }
    }

    View full-size slide

  27. Optimize your build
    Configure DEX options
    android {
    //...
    dexOptions {
    // Sets the maximum number of DEX processes
    // that can be started concurrently.
    maxProcessCount 8
    // Sets the maximum memory allocation pool size
    // for the dex operation.
    javaMaxHeapSize "2g"
    // Enables Gradle to pre-dex library dependencies.
    preDexLibraries true
    }
    }
    Tips : Total Memory = maxProcessCount * javaMaxHeapSize

    View full-size slide

  28. Publish your app
    Sign your app
    android {
    // ....
    // Encapsulates signing configurations.
    signingConfigs {
    // Creates a signing configuration called "release".
    release {
    // Specifies the path to your keystore file.
    storeFile file("my-release-key.jks")
    // Specifies the password for your keystore.
    storePassword "password"
    // Specifies the identifying name for your key.
    keyAlias "my-alias"
    // Specifies the password for your key.
    keyPassword "password"
    }
    }
    buildTypes {
    release {
    // Adds the "release" signing configuration to the
    release build type.
    signingConfig signingConfigs.release
    //...
    }
    }
    }

    View full-size slide

  29. Publish your app
    Remove private signing information
    from your project
    // create new file keystore.properties
    storePassword=myStorePassword
    keyPassword=myKeyPassword
    keyAlias=myKeyAlias
    storeFile=myStoreFileLocation
    // write this code in app level gradle
    def keystorePropertiesFile =
    rootProject.file("keystore.properties")
    def keystoreProperties = new Properties()
    keystoreProperties.load(new
    FileInputStream(keystorePropertiesFile))
    android {
    //...
    signingConfigs {
    config {
    keyAlias keystoreProperties['keyAlias']
    keyPassword keystoreProperties['keyPassword']
    storeFile file(keystoreProperties['storeFile'])
    storePassword keystoreProperties['storePassword']
    }
    }
    }

    View full-size slide

  30. Bonus Tips
    Image optimization
    #PERFMATTERS
    buildTypes {
    release {
    aaptOptions {
    cruncherEnabled = false
    }
    }
    }

    View full-size slide

  31. 1) Install your latest gradle and
    set environment variables.
    2) Go to your project path.
    3) Run gradle
    :moduleName:installDebug
    Bonus Tips:
    Gradle
    Command Line
    Build your app from command
    line

    View full-size slide

  32. Bonus Tips:
    Gradle
    Command Line
    Other Gradle commands

    View full-size slide

  33. Can I create my
    own gradle
    command?

    View full-size slide

  34. Can I improve
    build time of
    Gradle?

    View full-size slide

  35. Bonus Tips: Android Gradle build report
    Use gradle yourProject Task -profile

    View full-size slide

  36. Bonus Tips
    Use this link to find dependancies: http://gradleplease.appspot.com/
    See latest gradle tips here:
    http://tools.android.com/tech-docs/new-build-system/user-guide
    Gradle commands info/cheat sheet:
    https://docs.gradle.org/current/userguide/gradle_command_line.html

    View full-size slide

  37. Summary
    ● Manage and configure resource and version dependencies
    ● Use automation in version code
    ● How to use product flavors, flavors dimension
    ● Split apk if need for abi and screen density
    ● Configure dexOptions in gradle file
    ● Use signing config in Gradle

    View full-size slide

  38. References
    https://developer.android.com/studio/build/gradle-tips.html
    https://gradle.org/
    http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.int
    ernal.dsl.DexOptions.html
    http://stackoverflow.com/a/27908019/5255006
    https://github.com/udacity/ud867
    https://www.udacity.com/course/gradle-for-android-and-java--ud867

    View full-size slide

  39. Questions please...
    Krunal Kapadiya
    @krunal3kapadiya

    View full-size slide