vxh.viet
7/18/2017 - 11:40 AM

Gradle evevything

Gradle evevything

Optimizing Gradle Build:

  1. Common Stuffs: put these in gradle.properties
## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

# optimize gradle build, see here: http://zeroturnaround.com/rebellabs/making-gradle-builds-faster/
# and here: https://medium.com/@cesarmcferreira/speeding-up-gradle-builds-619c442113cb#.urfs6u1rm
org.gradle.daemon=true
org.gradle.configureondemand=true
  1. Use Instant Run and Offline mode during debug.

  2. Disable PNG crunching for debug build: (not necessary anymore, Android Studio 3.0 has this enable by default)

  • Go to File > Settings > Build, Execution, Deployment > Compiler > Command-line Options add -PdevBuild.

  • In each module's build.gradle (for Android Studio 3.+ see HERE):

android {
    
    ...
    
    // For android studio 3.0 this will be turned off by default for debug build
    // see https://androidstudio.googleblog.com/2017/06/android-studio-30-canary-5-is-now.html
    // For now we need to manually turn it on for release build.
    if (project.hasProperty('devBuild')) {
        aaptOptions {
            cruncherEnabled = false
        }
    }
    
    ...
    
}
  1. (Source: jlelse, Gist) Set minSdkVersion to 21+ when using Multidex. But if we need to support <21 we need to use this trick to set minSdkVersion to 21+ for debug build only (without affecting Lint warning):
  • Go to File > Settings > Build, Execution, Deployment > Compiler > Command-line Options add -PminSdk=21.

  • In the root's build.gradle add this block:

ext {
    minSdk = project.hasProperty('minSdk') ? minSdk.toInteger() : 18
}
  • In each module's build.gradle reference to minSdk like this:
android {
    defaultConfig {
            minSdkVersion rootProject.ext.minSdk
    }
}

IMPORTANT for release build we need to manually remove the -PminSdk=21 -PdevBuild params.

Extra:

  1. To test the Version of the compiled APK go to ~\Android\sdk\build-tools\26.0.0 (or any builtool version) and use this command:

aapt dump badging app-release.apk|grep Version

  1. (Source Android Developer) We can add common properties to root's build.gradle like this:
ext {
    compileSdkVersion = 25
    buildToolsVersion = "25.0.3"
    targetSdkVersion = 22
    minSdk = project.hasProperty('minSdk') ? minSdk.toInteger() : 18
}

And access them from each module's build.gradle like this:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdk
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
}

The Android Studio IDE doesn't seem to know about "ext" in subprojects, but gradle does. So it shows up as a warning when viewing the gradle files, but builds will still work.