gradle 101

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

Upload: kurt-mbanje

Post on 12-Apr-2017

519 views

Category:

Software


1 download

TRANSCRIPT

Building with Gradle 101Kurt Mbanje : DStv Digital Media

+KurtMbanje

@ckurtm

What is Gradle?

Why Gradle?

Background: Android Build Process

Why Gradle?

• Declarative vs Imperative

• Flexibility & Customization

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

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

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

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

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

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

Convention over Configuration ++

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

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

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

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'

}

Build Script: Repositories & Android Pluginbuildscript {

repositories {jcenter()

}dependencies {

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

}

Android Plugin

apply plugin: 'com.android.application'

Compile SDK & Tools Version

android {compileSdkVersion 23buildToolsVersion "23.0.2"

Example: Build Types

buildTypes {release {

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

}

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

}}

Default Config

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

}

Flavors

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

Dependencies

• Local file Jar or aar file

• Remote / Local repositoriesJCenter, maven , custom etc.

Tip 1: View app dependencies

./gradlew :app:dependencies

./gradlew :app:androidDependencies

Tip 2: Use Gradle wrapper!

./gradle wrapper --gradle-version 2.0

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

}

Tip 3: Gradle GUI

./gradle --gui

Tip 4: Different Icon per BuildType

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

}

Thanks

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

https://github.com/ckurtm/Gradle101

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