gradle : Building Android Apps

Preview:

Citation preview

gradle : Building Android Apps

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

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

Build Historya brief history of java build systems

In the beginning there wasnothing

… created Antand he saw it as messy

Bottled in2000

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>

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

Bottled in2001+

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>

Maven’s build by conventionmy-app

|-- pom.xml

`-- src

|-- main

| `-- java

| `-- com

| `-- mycompany

| `-- app

| `-- App.java

`-- test

`-- java

`-- com

`-- mycompany

`-- app

`-- AppTest.java

… created Gradlebuild files in XML is so 80’s

Bottled in2011+

Out of the box features

● Declarative Syntax

● Build-By-Convention

● Task-graph

● Multi-Project Builds

● Dependency Power Tool

● Groovy

Android Build System=

Gradle + Android Gradle Plugin +

IDE integration

Why a new Build System

● Customizable and extensible but stable API

● Unified across IDE and CI

● Standard and Advanced features

One Build System to Rule them All

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

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...

Advanced Tool Interface API

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

Declarative Domain Specific Language

The Android plugin extends the Gradle DSL

Gradle Skeletbuildscript {

repositories {

mavenCentral()

}

dependencies {

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

}

}

apply plugin: 'android'

android {

compileSdkVersion 17

buildToolsVersion ‘18.1’

}

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

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

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

Build Variant=

Build Types + Product Flavors

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\""

}

}

DSL: Signing Configuration signingConfigs {

release {

storeFile file("src/mobel.keystore")

storePassword "mobelmobel"

keyAlias "release"

keyPassword "mobelmobel"

}

}

buildTypes {

release {

signingConfig signingConfigs.release

}

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

DSL: Build Types

debug release staging

debug release staging

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' }

}

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

DEMO

Stable Build Environmentkeep is stable...

Stable Build Environment

Gradle version

use the gradle-wrapper

Plugin version

dependencies {

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

}

Stable Build Environment

Build tools

android {

compileSdkVersion 18

buildToolsVersion "18.1.0"

IDE doesn’t build your project

Testingunit- and integration testing in the same

project

Test Code Generation

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

Testing Scenarios

check

connectedDevices

deviceCheck

Test Server APIuse the cloud for parallel testing

Testing Server API

● Control your lab● Services from

○ TestDroid○ Manymo○ AppThwack

image from AppThwack website

Dependency Management

flexible and reuse your current repo’s

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

}

}

Corporate Repositories

Maven Central

Repo X

Repo Y

Gradle ProjectCorporate Repository

gradle .cache

Gradle ProjectGradle Project

Gradle ProjectGradle Project

Gradle ProjectGradle Project

Corporate Repository: NexusOpen Source repository manager...

Corporate Repository: ManagementGoogle Tag Manager example...

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/ )

Recommended