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

Stefan Oehme -Improving Android build performance

Stefan Oehme -Improving Android build performance

droidcon Berlin

July 17, 2018
Tweet

More Decks by droidcon Berlin

Other Decks in Programming

Transcript

  1. | X Process 1. Define scenario to improve 2. Profile

    scenario 3. IdenBfy biggest boFleneck 4. Fix boFleneck 5. Verify fix by measurement 6. Repeat
  2. | X Automate your measurements github.com/gradle/gradle-profiler configurationTime { tasks =

    ["help"] } cleanBuild { tasks = ["build"] cleanup-tasks = ["clean"] } cachedCleanBuild { tasks = ["build"] cleanup-tasks = ["clean"] gradle-args = ["--build-cache"] }
  3. | X Stay Up-To-Date ./gradlew wrapper --gradle-version 4.8.1 buildscript {

    repositories { google() } dependencies { classpath ‘com.android.tools.build:gradle:3.1.2' } }
  4. | X JVM tuning Provide enough heap space Other tweaks

    oSen do more harm than good Spend your Bme on structural improvements
  5. | X Red Flags Startup/SeVngs/buildSrc > 1s ConfiguraBon Bme >10ms/project

    Single-line change ℳ clean build No-op build doing any work at all High GC Bme
  6. | X ConfiguraIon Time When running any task Even gradle

    help/gradle tasks Android Studio sync
  7. | X Eager resoluBon task uberJar(type:Jar) { from sourceSets.main.output from

    configurations.runtime.collect { it.directory ? it : zipTree(it) } classifier "uber-jar" } ResoluIon at ConfiguraIon Time
  8. | X Use lazy evaluaBon instead task uberJar(type:Jar) { inputs.files

    configurations.runtime from sourceSets.main.output from { configurations.runtime.collect { it.directory ? it : zipTree(it) } } classifier "uber-jar" } ResoluIon at ConfiguraIon Time
  9. | X task projectStats() { def statsFile = new File(buildDir,

    'stats.txt') statsFile.parentFile.mkdirs() statsFile.text = "Source files: ${sourceSets.main.java.size()}" } I/O at ConfiguraIon Time Careful when wriBng custom tasks!
  10. | X I/O at ConfiguraIon Time task projectStats() { def

    statsFile = new File(buildDir, 'stats.txt') inputs.files sourceSets.main.java outputs.file statsFile doLast { statsFile.parentFile.mkdirs() statsFile.text = "Source files: ${sourceSets.main.java.size()}" } } Don’t forget doLast {}
  11. | X task projectStats(type: ProjectStats) { statsFile = new File(buildDir,

    'stats.txt') sources = sourceSets.main.java } class ProjectStats extends DefaultTask { @InputFiles FileCollection sources @OutputFile File statsFile @TaskAction def stats() { statsFile.text = "Source files: ${sources.size()}" } } I/O at ConfiguraIon Time
  12. | X Expensive logic for each project def out =

    new ByteArrayOutputStream() exec { commandLine 'git', 'rev-parse', "HEAD" standardOutput = out workingDir = rootDir } version = new String(out.toByteArray()) subprojects { apply from: "$rootDir/gradle/version-from-status.gradle" } Inefficient Plugins
  13. | X Reuse expensive calculaBons def out = new ByteArrayOutputStream()

    exec { commandLine 'git', 'rev-parse', "HEAD" standardOutput = out workingDir = rootDir } version = new String(out.toByteArray()) subprojects { version = rootProject.version } Inefficient Plugins
  14. | X Extract Script Plugins Makes finding issues easier apply

    from: "$rootDir/gradle/environment.gradle" apply from: "gradle/chuck-norris-fact.gradle" apply from: "$rootDir/gradle/version-from-status.gradle" subprojects { apply plugin: 'java' apply from: "$rootDir/gradle/test-fixtures.gradle" apply from: "$rootDir/gradle/compiler-settings.gradle" }
  15. | X Example: CrashlyIcs Unique IDs are the bane of

    local dev performance apply plugin: 'io.fabric' android { buildTypes { debug { alwaysUpdateBuildId = false } } }
  16. | X Faster CompilaIon ModularizaBon => Compile avoidance Decoupled code

    => Faster incremental compilaBon Careful with Kotlin migraBon (for now)
  17. | X Incremental AnnotaIon Processing Since Gradle 4.7 Early adopters:

    Lombok & Android-State Others: github.com/gradle/gradle/issues/5277
  18. | X The build side of Android apps 
 Nikita

    Kozlov, Boris Faber Compiling…? How to reduce your wait Bmes and “get back to work”
 Paul Davies ModularizaBon - How hard can it be? 
 Elin Nilsson Talks Today
  19. | X Gradle performance guide hSps:/ /guides.gradle.org/performance Android performance guide

    hSps:/ /developer.android.com/studio/build/opImize-your-build.html Plugin development guide hSps:/ /guides.gradle.org/implemenIng-gradle-plugins Structuring build logic guide hSps:/ /docs.gradle.org/current/userguide/organizing_build_logic.html Guides