Top Banner
Building with Gradle 101 Kurt Mbanje : DStv Digital Media +KurtMbanje @ckurtm
29

Gradle 101

Apr 12, 2017

Download

Software

Kurt Mbanje
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Gradle 101

Building with Gradle 101Kurt Mbanje : DStv Digital Media

+KurtMbanje

@ckurtm

Page 2: Gradle 101

What is Gradle?

Page 3: Gradle 101

Why Gradle?

Page 4: Gradle 101

Background: Android Build Process

Page 5: Gradle 101

Why Gradle?

• Declarative vs Imperative

• Flexibility & Customization

• “Convention over Configuration” , well …convention is good but so is flexibility

Page 6: Gradle 101

Declarative vs Imperative

• Imperative : script how to do something, and as a result what you want to happen will happen

• Declarative: script what you would like to happen, and let the tool figure out how to do it

Page 7: Gradle 101

Declarative vs Imperative

• Imperative : script how to do something, and as a result what you want to happen will happen

• Declarative: script what you would like to happen, and let the tool figure out how to do it

Page 8: Gradle 101

Flexibility & Customization

• Supports Android’s diverse ecosystem, e.g. ABI, density , API level etc.

• Split apks for Play Store

• Still allows you to write plugins and tasks

Page 9: Gradle 101

Flexibility & Customization

• Supports Android’s diverse ecosystem, e.g. ABI, density , API level etc.

• Split apks for Play Store

• Still allows you to write plugins and tasks

Page 10: Gradle 101

Flexibility & Customization

• Supports Android’s diverse ecosystem, e.g. ABI, density , API level etc.

• Split apks for Play Store

• Still allows you to write plugins and tasks

Page 11: Gradle 101

Convention over Configuration ++

Page 12: Gradle 101

Android Gradle Concepts

• Build Types - how the application is packaged, and how the compiler is called (mostly)Debug vs Release (vs. <other>)

• Product Flavors – mostly has to do with featurespaid vs. free

abi packaging

• Variants = Build Types + FlavorsfreeDebug , paidRelease , paidDebug , freeRelease

Page 13: Gradle 101

Android Gradle Concepts

• Build Types - how the application is packaged, and how the compiler is called (mostly)Debug vs Release (vs. <other>)

• Product Flavors – mostly has to do with featurespaid vs. free

abi packaging

• Variants = Build Types + FlavorsfreeDebug , paidRelease , paidDebug , freeRelease

Page 14: Gradle 101

Android Gradle Concepts

• Build Types - how the application is packaged, and how the compiler is called (mostly)Debug vs Release (vs. <other>)

• Product Flavors – mostly has to do with featurespaid vs. free

abi packaging

• Variants = Build Types + FlavorsfreeDebug , paidRelease , paidDebug , freeRelease

Page 15: Gradle 101

Sample Android build script: build.gradlebuildscript {

repositories {jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:1.3.0'}

}

apply plugin: 'com.android.application'

android {compileSdkVersion 23buildToolsVersion "23.0.2"

defaultConfig {applicationId "com.example.test"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"

}buildTypes {

release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}}

}

dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:23.1.0'

}

Page 16: Gradle 101

Build Script: Repositories & Android Pluginbuildscript {

repositories {jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:1.3.0'}

}

Page 17: Gradle 101

Android Plugin

apply plugin: 'com.android.application'

Page 18: Gradle 101

Compile SDK & Tools Version

android {compileSdkVersion 23buildToolsVersion "23.0.2"

Page 19: Gradle 101

Example: Build Types

buildTypes {release {

minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

debug {minifyEnabled trueapplicationIdSuffix '.debug'versionNameSuffix '.debug'

}}

Page 20: Gradle 101

Default Config

defaultConfig {applicationId "com.example.test"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"

}

Page 21: Gradle 101

Flavors

Page 22: Gradle 101

Build Variants

• Variants = Build Types + FlavorsfreeDebug , paidRelease , paidDebug , freeRelease

Debug Release

Free freeDebug freeRelease

Paid paidDebug paidRelease

• Flavor Groups /Dimensionsadds another 3rd matrix to the mix… or 4th

Page 23: Gradle 101

Dependencies

• Local file Jar or aar file

• Remote / Local repositoriesJCenter, maven , custom etc.

Page 24: Gradle 101

Tip 1: View app dependencies

./gradlew :app:dependencies

./gradlew :app:androidDependencies

Page 25: Gradle 101

Tip 2: Use Gradle wrapper!

./gradle wrapper --gradle-version 2.0

task wrapper(type: Wrapper) {gradleVersion = '2.0'

}

Page 26: Gradle 101

Tip 3: Gradle GUI

./gradle --gui

Page 27: Gradle 101

Tip 4: Different Icon per BuildType

Page 28: Gradle 101

Tip 5: BuildConfig.java

public final class BuildConfig {public static final boolean DEBUG = Boolean.parseBoolean("true");public static final String APPLICATION_ID = "com.peirr.test.debug";public static final String BUILD_TYPE = "debug";public static final String FLAVOR = "";public static final int VERSION_CODE = 1;public static final String VERSION_NAME = "1.0.debug";

}

Page 29: Gradle 101

Thanks

https://docs.gradle.org/current/userguide/gradle_wrapper.html

https://github.com/ckurtm/Gradle101

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