gradle : building android apps

43
gradle : Building Android Apps Mobel Meetup 2013-10-15 @alexvb http://alex.vanboxel.be/

Upload: dodung

Post on 30-Dec-2016

240 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: gradle : Building Android Apps

gradle : Building Android Apps

Mobel Meetup2013-10-15@alexvbhttp://alex.vanboxel.be/

Page 2: gradle : Building Android Apps

Biography

Software Architect - Mobile@ Vente-Exclusive.com

Working with Java since the dark ages… at Progress Software, Alcatel-Lucent, …

Interested in science and technology

Alex Van Boxel

Page 3: gradle : Building Android Apps

Build Historya brief history of java build systems

Page 4: gradle : Building Android Apps

In the beginning there wasnothing

Page 5: gradle : Building Android Apps

… created Antand he saw it as messy

Bottled in2000

Page 6: gradle : Building Android Apps

Ant example<project>

<target name="clean">

<delete dir="build"/>

</target>

<target name="compile">

<mkdir dir="build/classes"/>

<javac srcdir="src" destdir="build/classes"/>

</target>

<target name="jar">

<mkdir dir="build/jar"/>

<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">

<manifest>

<attribute name="Main-Class" value="io.mobel.HelloWorld"/>

</manifest>

</jar>

</target>

<target name="run">

<java jar="build/jar/HelloWorld.jar" fork="true"/>

</target>

</project>

Page 7: gradle : Building Android Apps

… created Mavenand he saw it was better, but...

Bottled in2001+

Page 8: gradle : Building Android Apps

Maven example<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>

<artifactId>my-app</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Maven Quick Start Archetype</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

Page 9: gradle : Building Android Apps

Maven’s build by conventionmy-app

|-- pom.xml

`-- src

|-- main

| `-- java

| `-- com

| `-- mycompany

| `-- app

| `-- App.java

`-- test

`-- java

`-- com

`-- mycompany

`-- app

`-- AppTest.java

Page 10: gradle : Building Android Apps

… created Gradlebuild files in XML is so 80’s

Bottled in2011+

Page 11: gradle : Building Android Apps

Out of the box features

● Declarative Syntax

● Build-By-Convention

● Task-graph

● Multi-Project Builds

● Dependency Power Tool

● Groovy

Page 12: gradle : Building Android Apps

Android Build System=

Gradle + Android Gradle Plugin +

IDE integration

Page 13: gradle : Building Android Apps

Why a new Build System

● Customizable and extensible but stable API

● Unified across IDE and CI

● Standard and Advanced features

Page 14: gradle : Building Android Apps

One Build System to Rule them All

Same build system from IDE, command-line and continuous-integration environment

Page 15: gradle : Building Android Apps

Advanced Tool Interface API

MobelDemoProject git:(master) gradle tasks:tasks

------------------------------------------------------------All tasks runnable from root project------------------------------------------------------------

Android tasks-------------androidDependencies - Displays the Android dependencies of the projectsigningReport - Displays the signing info for each variant

Build tasks-----------assemble - Assembles all variants of all applications and secondary packages.assembleDebug - Assembles all Debug buildsassembleFreeGoogleIO - Assembles all builds for flavor FreeGoogleIOassembleFreeGoogleIODebug - Assembles the Debug build for flavor FreeGoogleIOassembleFreeGoogleIORelease - Assembles the Release build for flavor FreeGoogleIOassembleFreeGoogleIOStaging - Assembles the Staging build for flavor FreeGoogleIO

IDEA and Studio Command Line

Others

TeamCity

EclipseAtlassian BambooJenkinsetc...

Page 16: gradle : Building Android Apps

Advanced Tool Interface API

Integrates with Android Studio based on IDEA…or works in Eclipse with Android Plugin...

Page 17: gradle : Building Android Apps

Declarative Domain Specific Language

The Android plugin extends the Gradle DSL

Page 18: gradle : Building Android Apps

Gradle Skeletbuildscript {

repositories {

mavenCentral()

}

dependencies {

classpath 'com.android.tools.build:gradle:0.6.1'

}

}

apply plugin: 'android'

android {

compileSdkVersion 17

buildToolsVersion ‘18.1’

}

Page 19: gradle : Building Android Apps

Convention over Configurationmy-app|-- build.gradle`-- src |-- main | `-- java `-- test `-- java

Page 20: gradle : Building Android Apps

Convention over Configurationmy-app|-- build.gradle`-- src |-- main | |-- AndroidManifest.xml | |-- res | |-- assets | |-- aidl | |-- rs | |-- jni | `-- java |-- instrumentedTest | |-- res | |-- assets | |-- aidl | |-- rs | |-- jni | `-- java `-- test `-- java

Page 21: gradle : Building Android Apps

Gradle Tasks

gradle <task> [<task>]

gradle tasks [--all]

● assemble○ assemble the project(s)

● check○ run all the checks

● build○ assemble + check

● clean○ clean all the output

Page 22: gradle : Building Android Apps

Build Variant=

Build Types + Product Flavors

Page 23: gradle : Building Android Apps

DSL: Build Types buildTypes {

release {

signingConfig signingConfigs.release

}

debug {

packageNameSuffix ".debug"

versionNameSuffix = ".D"

debuggable true

}

staging.initWith(buildTypes.debug)

staging {

packageNameSuffix ".staging"

buildConfig "private final static String server = \"staging.example.com\""

}

}

Page 24: gradle : Building Android Apps

DSL: Signing Configuration signingConfigs {

release {

storeFile file("src/mobel.keystore")

storePassword "mobelmobel"

keyAlias "release"

keyPassword "mobelmobel"

}

}

buildTypes {

release {

signingConfig signingConfigs.release

}

Page 25: gradle : Building Android Apps

Convention over Configurationmy-app|-- build.gradle`-- src |-- main | |-- AndroidManifest.xml | |-- res | |-- assets | |-- aidl | |-- rs | |-- jni | `-- java |-- debug | |-- ... |-- staging | |-- ... |-- instrumentedTest | |-- ... `-- test `-- java

Page 26: gradle : Building Android Apps

DSL: Build Types

debug release staging

debug release staging

Page 27: gradle : Building Android Apps

DSL: Product FlavorExample without groups

productFlavors {

Mobel {

}

GoogleIO {

}

Example with groups

flavorGroups 'version','session'

productFlavors {

Mobel { flavorGroup 'session' }

GoogleIO { flavorGroup 'session' }

free { flavorGroup 'version' }

paid { flavorGroup 'version' }

}

Page 28: gradle : Building Android Apps

Build Type + Product Flavor

debug release

free mobel FreeMobelDebug FreeMobelRelease

googleIO FreeGoogleIODebug FreeGoogleIORelease

pail mobel PaidMobelDebug PaidMobelRelease

googleIO PaidGoogleIODebug PaidGoogleIORelease

… and Flavor Groups

debug release

mobel MobelDebug MobelRelease

googleIO GoogleIODebug GoogleIORelease

Page 29: gradle : Building Android Apps

DEMO

Page 30: gradle : Building Android Apps

Stable Build Environmentkeep is stable...

Page 31: gradle : Building Android Apps

Stable Build Environment

Gradle version

use the gradle-wrapper

Plugin version

dependencies {

classpath 'com.android.tools.build:gradle:0.6.1'

}

Page 32: gradle : Building Android Apps

Stable Build Environment

Build tools

android {

compileSdkVersion 18

buildToolsVersion "18.1.0"

IDE doesn’t build your project

Page 33: gradle : Building Android Apps

Testingunit- and integration testing in the same

project

Page 34: gradle : Building Android Apps

Test Code Generation

Testing Sources Sets● Standard test/● Android instrumentedTest(flavor)/● Generated Manifest● Special APK for library projects

Page 35: gradle : Building Android Apps

Testing Scenarios

check

connectedDevices

deviceCheck

Page 36: gradle : Building Android Apps

Test Server APIuse the cloud for parallel testing

Page 37: gradle : Building Android Apps

Testing Server API

● Control your lab● Services from

○ TestDroid○ Manymo○ AppThwack

image from AppThwack website

Page 38: gradle : Building Android Apps

Dependency Management

flexible and reuse your current repo’s

Page 39: gradle : Building Android Apps

Dependencies dependencies {

compile 'com.android.support:support-v4:18.0.+'

compile 'com.android.support:appcompat-v7:18.0.+'

compile 'com.google.android.gms:play-services:3.2.65'

compile 'com.google.android.gtm:tagmanager:3.0.0'

compile 'joda-time:joda-time:2.3'

compile 'com.fasterxml.jackson.core:jackson-core:2.2.3'

compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'

compile 'com.google.guava:guava:15.0'

compile 'com.squareup.dagger:dagger:1.1.0'

compile 'com.squareup.dagger:dagger-compiler:1.1.0'

compile(group: 'de.keyboardsurfer.android.widget', name: 'crouton', version: '1.8.1')

{

transitive = false

}

}

Page 40: gradle : Building Android Apps

Corporate Repositories

Maven Central

Repo X

Repo Y

Gradle ProjectCorporate Repository

gradle .cache

Gradle ProjectGradle Project

Gradle ProjectGradle Project

Gradle ProjectGradle Project

Page 41: gradle : Building Android Apps

Corporate Repository: NexusOpen Source repository manager...

Page 42: gradle : Building Android Apps

Corporate Repository: ManagementGoogle Tag Manager example...

Page 43: gradle : Building Android Apps

Reference

● http://tools.android.com/tech-docs/new-build-system

● http://www.gradle.org/

● http://www.gradleware.com/

● Google I/O (on youtube)Follow me @alexvb

+Alex Van Boxel ( web: http://alex.vanboxel.be/ )