47
Gradle & Android Etienne Studer, VP of Product Tooling

Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Embed Size (px)

Citation preview

Page 1: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Gradle & AndroidEtienne Studer,

VP of Product Tooling

Page 2: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into
Page 3: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Motivation

Page 4: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Facts• implicit complexity of the domain

• multi-language support

• resource/code generation

• platform diversity

Page 5: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Complexity

Page 6: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

android { compileSdkVersion 20 buildToolsVersion "20.0.0" flavorDimensions "version", "abi" productFlavors { free { flavorDimension "version" } paid { flavorDimension "version" } arm { flavorDimension "abi" } x86 { flavorDimension "abi" } }

ConcisenessassembleX86FreeTest - Assembles the Test build for the X86FreeDebug build compileX86FreeDebug compileX86FreeDebugAidl compileX86FreeDebugRenderscript compileX86FreeTest compileX86FreeTestAidl compileX86FreeTestRenderscript dexX86FreeTest generateX86FreeDebugBuildConfig generateX86FreeTestBuildConfig mergeX86FreeDebugAssets mergeX86FreeDebugResources mergeX86FreeTestAssets mergeX86FreeTestResources packageX86FreeTest prepareX86FreeDebugDependencies prepareX86FreeTestDependencies processX86FreeDebugManifest processX86FreeDebugResources processX86FreeTestJavaRes processX86FreeTestResources processX86FreeTestTestManifest validateDebugSigningassembleX86Paid - Assembles all builds for flavor X86Paid [assembleX86PaidDebug, assembleX86PaidRelease]assembleX86PaidDebug - Assembles the Debug build for flavor X86Paid compileX86PaidDebug compileX86PaidDebugAidl compileX86PaidDebugRenderscript dexX86PaidDebug generateX86PaidDebugBuildConfig mergeX86PaidDebugAssets mergeX86PaidDebugResources packageX86PaidDebug prepareX86PaidDebugDependencies processX86PaidDebugJavaRes processX86PaidDebugManifest processX86PaidDebugResources validateDebugSigningassembleX86PaidRelease - Assembles the Release build for flavor X86Paid compileX86PaidRelease compileX86PaidReleaseAidl compileX86PaidReleaseRenderscript dexX86PaidRelease generateX86PaidReleaseBuildConfig mergeX86PaidReleaseAssets mergeX86PaidReleaseResources packageX86PaidRelease prepareX86PaidReleaseDependencies processX86PaidReleaseJavaRes processX86PaidReleaseManifest processX86PaidReleaseResourcesassembleX86PaidTest - Assembles the Test build for the X86PaidDebug build compileX86PaidDebug compileX86PaidDebugAidl compileX86PaidDebugRenderscript compileX86PaidTest compileX86PaidTestAidl compileX86PaidTestRenderscript dexX86PaidTest generateX86PaidDebugBuildConfig generateX86PaidTestBuildConfig mergeX86PaidDebugAssets mergeX86PaidDebugResources mergeX86PaidTestAssets mergeX86PaidTestResources packageX86PaidTest prepareX86PaidDebugDependencies prepareX86PaidTestDependencies processX86PaidDebugManifest processX86PaidDebugResources processX86PaidTestJavaRes processX86PaidTestResources processX86PaidTestTestManifest validateDebugSigningbuild - Assembles and tests this project. [assemble, check]buildDependents - Assembles and tests this project and all projects that depend on it. [build]buildNeeded - Assembles and tests this project and all projects it depends on. [build]clean - Deletes the build directory.

Help tasks----------dependencies - Displays all dependencies declared in root project 'flavors'.dependencyInsight - Displays the insight into a specific dependency in root project 'flavors'.help - Displays a help messageprojects - Displays the sub-projects of root project 'flavors'.properties - Displays the properties of root project 'flavors'.tasks - Displays the tasks runnable from root project 'flavors' (some of the displayed tasks may belong to subprojects).

Install tasks-------------installArmFreeDebug - Installs the Debug build for flavor ArmFree compileArmFreeDebug compileArmFreeDebugAidl compileArmFreeDebugRenderscript dexArmFreeDebug generateArmFreeDebugBuildConfig mergeArmFreeDebugAssets mergeArmFreeDebugResources packageArmFreeDebug prepareArmFreeDebugDependencies processArmFreeDebugJavaRes processArmFreeDebugManifest processArmFreeDebugResources validateDebugSigninginstallArmFreeTest - Installs the Test build for the ArmFreeDebug build compileArmFreeDebug compileArmFreeDebugAidl compileArmFreeDebugRenderscript compileArmFreeTest compileArmFreeTestAidl compileArmFreeTestRenderscript dexArmFreeTest generateArmFreeDebugBuildConfig generateArmFreeTestBuildConfig mergeArmFreeDebugAssets mergeArmFreeDebugResources mergeArmFreeTestAssets mergeArmFreeTestResources packageArmFreeTest prepareArmFreeDebugDependencies prepareArmFreeTestDependencies processArmFreeDebugManifest processArmFreeDebugResources processArmFreeTestJavaRes processArmFreeTestResources processArmFreeTestTestManifest validateDebugSigninginstallArmPaidDebug - Installs the Debug build for flavor ArmPaid compileArmPaidDebug compileArmPaidDebugAidl compileArmPaidDebugRenderscript dexArmPaidDebug generateArmPaidDebugBuildConfig mergeArmPaidDebugAssets mergeArmPaidDebugResources packageArmPaidDebug prepareArmPaidDebugDependencies processArmPaidDebugJavaRes processArmPaidDebugManifest processArmPaidDebugResources validateDebugSigninginstallArmPaidTest - Installs the Test build for the ArmPaidDebug build compileArmPaidDebug compileArmPaidDebugAidl compileArmPaidDebugRenderscript compileArmPaidTest compileArmPaidTestAidl compileArmPaidTestRenderscript dexArmPaidTest generateArmPaidDebugBuildConfig generateArmPaidTestBuildConfig mergeArmPaidDebugAssets mergeArmPaidDebugResources mergeArmPaidTestAssets mergeArmPaidTestResources packageArmPaidTest prepareArmPaidDebugDependencies prepareArmPaidTestDependencies processArmPaidDebugManifest processArmPaidDebugResources processArmPaidTestJavaRes processArmPaidTestResources processArmPaidTestTestManifest validateDebugSigninginstallX86FreeDebug - Installs the Debug build for flavor X86Free compileX86FreeDebug compileX86FreeDebugAidl compileX86FreeDebugRenderscript dexX86FreeDebug generateX86FreeDebugBuildConfig mergeX86FreeDebugAssets mergeX86FreeDebugResources packageX86FreeDebug prepareX86FreeDebugDependencies processX86FreeDebugJavaRes processX86FreeDebugManifest processX86FreeDebugResources validateDebugSigninginstallX86FreeTest - Installs the Test build for the X86FreeDebug build compileX86FreeDebug compileX86FreeDebugAidl compileX86FreeDebugRenderscript compileX86FreeTest compileX86FreeTestAidl compileX86FreeTestRenderscript dexX86FreeTest generateX86FreeDebugBuildConfig generateX86FreeTestBuildConfig mergeX86FreeDebugAssets mergeX86FreeDebugResources mergeX86FreeTestAssets mergeX86FreeTestResources packageX86FreeTest prepareX86FreeDebugDependencies prepareX86FreeTestDependencies processX86FreeDebugManifest processX86FreeDebugResources processX86FreeTestJavaRes processX86FreeTestResources processX86FreeTestTestManifest validateDebugSigninginstallX86PaidDebug - Installs the Debug build for flavor X86Paid compileX86PaidDebug compileX86PaidDebugAidl compileX86PaidDebugRenderscript

Page 7: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Polyglot• multiple languages

• multiple teams

• coordinated releases

Page 8: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Product delivery• more than just building APKs

• documentation with tested code examples

• auto-provisioned dev environments

• automated release process

• …

Page 9: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into
Page 10: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Android Studio IDE +Android Gradle Plugin +

Gradle Platform +Android Tooling

The Android Build System

Page 11: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Have a single truth of build logic

Put all build logic into the build.Derive all information from the build model.

Page 12: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

In a unified build, Gradle is the single source of build logic.

GradleLauncher

AS

Tooling API

Server(daemon)

Cmd Line(developer) (build master)

(cont integ)

Page 13: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

• Proxy for embedding Gradle

• Extension mechanism to provide custom models

• See DefaultAndroidProject model

Client VM with gradle-tooling-api.jar

Gradle Daemon for Gradle build

get build models

invoke build tasks

Tooling API

Page 14: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

• Backward & forward compatible From 1.0 to 2.8

• Runtime Isolation Separate daemon process

• High Level Services Build Cancellation,Continuous Mode, Test Execution, etc.

• Build Event Model Life-cycle events, task events, test events

Tooling API

Page 15: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Performance

Page 16: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Minimize the build time while using as little memory as needed.

Goal

Page 17: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Typically, not much changes in the build between consecutive invocations of the build.

When little changes in the build, little work should be done by the build.

Observation

Page 18: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Performance enhancements are achieved through evolutionary improvements and

revolutionary changes.

Approach

Page 19: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Building with Gradle - today2-phase build:

- Configuration phase

- Execution phase

Page 20: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Execution Phase• Incremental build feature

.Only run a task if its input or output has changed since the previous run

Inputs —> Task —> Outputs

Define inputs and outputs on your custom tasks.

Page 21: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Execution Phaseclass ConversionTask extends DefaultTask {

@InputFiles def sourceFiles @OutputDirectory def targetDirectory @TaskAction def doSomeWork() { // consume the sourceFiles and write the result // to a file in the targetDirectory }

}

task foo(type: MyTask) { sourceFiles = files('input.txt'); targetDirectory = file('build/result') }

Page 22: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Execution Phase• Continuous build feature

.Keep the session running between build runs

gradlew test -t

Page 23: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Execution Phase• Faster incremental builds

.Improvements to the management of File checksums

.Most notable for almost uptodate builds

• Faster compilation for continuous builds.Compiler daemon is kept alive for the entire ContB session

Evolutionary improvements with each release!

Page 24: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Configuration Phase• Imperative build logic (input)

.Even when providing a declarative DSL

.Lazy evaluation tricks

.Lazy collection tricks

.Project#afterEvaluate tricks

• Declarative build model (output)

Know all things, build some things.

Page 25: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Configuration Phase• Android

.Full model construction at evaluation time

.Full dependency resolution/processing at evaluation

.Hard to know when things are ready, e.g flavors

.Even when calling gradlew tasks

Page 26: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Configuration Phase• Faster build script compilation

.Caching the compiled form of the build scripts

.Newer version of Groovy used for the DSL parsing

• Configure on demand.Attempt to only configure the projects relevant to the requested tasks

Evolutionary improvements with each release!

Page 27: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Android Build - Pre-dexing

Page 28: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

• Clean Safe Cache

• Parallelization

• Distributed Cache

Pre-Dexing Optimizations

Page 29: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Android Build - Dexing

Page 30: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

• Faster Dexing

• Incremental Dexing

Dexing Optimizations

Page 31: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Building with Gradle - experimentalThe revolutionary new Configuration model

• From know all things, build some things to know some things, build some things

Page 32: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

New Configuration Model

Apply the concepts already available in the Execution phase to the Configuration phase.

Describe what the model should look like and Gradle will provide the implementation.

Page 33: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

• Richer modeling

• Cleaner modeling

• Collaborative modeling

• Comprehensible model

Modeling

Page 34: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Managed types@Managedinterface Picture {

String getName() void setName(String name)

List<String> getTags()

}

Page 35: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Pluginclass PicturesPlugin extends RuleSource { @Model void createPicture(Picture picture) {} @Mutate void configurePicture(Picture picture) { picture.name = 'mypic.jpg' picture.tags.addAll(['nature', 'night']) } }

Page 36: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

DSLmodel { picture(Picture) { name = 'mypic.jpg' tags.addAll(['night', 'moon']) } }

Page 37: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Report

Page 38: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Android (experimental)model { android { compileSdkVersion = 21 } android.buildTypes { debug { } create(‘qa’) } }

Page 39: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

New Configuration Model• Configuration becomes parallelizable

• Managed types are externalizable

• Model becomes reusable between invocations

Revolutionary change!

Page 40: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Roadmap 6-12 months• Radically improve performance

• Gentrify dependency management

No guarantees or commitments.

Page 41: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Performance

• Fix hotspots

• Cache and reuse

• Work in parallel

• Work in background

1. Build configuration

2. Dependency resolution

3. (Task Execution)

Apply To

Page 42: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Performance

Page 43: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Dependency Management• dependency management for all the variants of

the project

• variant-aware dependency management for the external dependencies

Page 44: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Gradle.com

Page 45: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

MOOC

Page 46: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Gradle is hiringhttp://gradle.org/gradle-jobs

Page 47: Gradle & Android - GOTO Blog · PDF fileAndroid Gradle Plugin + Gradle Platform + Android Tooling The Android Build System. Have a single truth of build logic Put all build logic into

Gradle & AndroidEtienne Studer,

VP of Product Tooling