25
Build your Android App with Gradle Android new build system

Build your android app with gradle

Embed Size (px)

Citation preview

Page 1: Build your android app with gradle

Build your Android App with Gradle

Android new build system

Page 2: Build your android app with gradle

Agenda

• What is Gradle• Getting started with gradle• Use cases• Q&A

Page 3: Build your android app with gradle

Download Meetup Sample Code

– http://goo.gl/qoXAfa

Page 4: Build your android app with gradle

What is gradle

• Gradle is a JVM based project build automation tool that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy based domain-specific-language (DSL) instead of the more traditional XML form of declaring the project configuration.

Page 5: Build your android app with gradle

Installation

• Download the latest android plugin supported Gradle version • Extract the downloaded zip “gradle-1.10-all.zip” to any

directory• Configure the GRADLE_HOME environment variable

• set GRADLE_HOME=<installed directoy>\gradle-1.10 • set PATH=%PATH%\%GRADLE_HOME%\bin

• C:\> gradle

Page 6: Build your android app with gradle

Gradle Task

• C:\> gradle tasks

• Applying the plugin’s to the build file will automatically add set of build task to run

• assemble The task to assemble the output(s) of the project

• checkThe task to run all the checks.

• buildThis task does both assemble and check

• cleanThis task cleans the output of the project

Page 7: Build your android app with gradle

Build File Skeleton

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.8.+' }}apply plugin: 'android'android { buildToolsVersion "19.0.1" compileSdkVersion 19}

Page 8: Build your android app with gradle

Gradle Wrapper

• C:\> gradle wrapper

task wrapper(type: Wrapper) { gradleVersion = '1.10‘}

/└── gradlew └── gradlew.bat └── gradle

└── wrapper └── gradle-wrapper.jar └── gradle-wrapper.properties

Page 9: Build your android app with gradle

Convention Over Configuration

src└── main l └── java l└── instrumentTest

└── java

sourceSets { main{ manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } instrumentTest.setRoot('tests') }

Page 10: Build your android app with gradle

Usecase : 1

I have project with multiple library projects, one of which is required to be shared with one of my colleague who's working in a another project.

Page 11: Build your android app with gradle

Dependency Management

• dependencies DSL element is part of the standard Gradle• Can add dependencies on local and remote libraries• To add a dependency have to provide library on the compile

configuration under dependencies DSL• Everything added to the compile configuration get packaged in

to the final APK• Gradle will follow all dependencies transitively

Page 12: Build your android app with gradle

Dependencies On Multiple Projects

• Each projects will have its own build.gradle declaring how it gets built

• There will be a file called settings.gradle at the root, which define which folder is a Gradle project

• If there are more than one library, importing order should be considered on dependencies on each other

Page 13: Build your android app with gradle

Publish Library To Artifact Repo

• Publish artifacts to local artifact repository

Maven Central

● Proper release and snapshot release management

Page 14: Build your android app with gradle

Usecase :2

• I want to make a digitally signed and secured release version of my app

Page 15: Build your android app with gradle

Build types

• This is achieved by buildTypes DSL• By default, the android plugin automatically sets up the project

to build both a debug and a release version of the application• Difference is that the ability to enable debug and how the APK

is signed• It allows to customize both debug and release versions, as

well as creating other build types

Page 16: Build your android app with gradle

Build Types…

• Build Types can contribute to the build with code and resources.

• For each Build Type, a new matching sourceSet is created, under src directory

src/<buildtypename>/

• This means the Build Type names cannot be main or instrumentTest

Page 17: Build your android app with gradle

Signing APK

• Android system will not install or run an application that is not signed appropriately.

• To sign an APK have to make use of signingConfigs DSL

Page 18: Build your android app with gradle

Obfuscating

• Tool ProGuard is used automatically to obfuscate the APK• It will rename classes, fields, and methods with semantically

obscure names to make it difficult to reverse engineer.• Android plugin support proguard version is 4.10• ProGuard will shrinks, optimizes, and remove unused code

which will result smaller APK

Page 19: Build your android app with gradle

Usecase : 3

• I want to make a free version of my application with less features and commercial version with all features.

Page 20: Build your android app with gradle

Build Variants

–Different versions of the same application or Same application packaged differently or combination of both.

• Have to occupy productFlavors DSL• Build Type + Product Flavor = Build Variant

• Product flavor will generate APK’s for all possible combinations of Build Types and Product Flavors

Debug Release

Free Free-Debug Free-Release

Commercial Commercial-Debug Commercial-Release

Page 21: Build your android app with gradle

Usecase : 4

I want to unit test my project with a testing framework,

Page 22: Build your android app with gradle

Testing

• No need to have a separate test project any more it is integrated to the application project

• Default location for the instrument test cases are located in src/instrumentTest

• Test APK is built to test the app under test using the Android instrumentation framework

• No need to have a AndroidManifest.xml for the app, cause it will get generated automatically

• Test app manifest is automatically get filled with the package name and the instrument test runner

Page 23: Build your android app with gradle

Usecase : 5

• I want to measure the quality of my source code continuously with some quality tools by Integrate it with a Continuous Integration System

Page 24: Build your android app with gradle

Q&A

Page 25: Build your android app with gradle

Thank you