Share
Embed
Gradle Build Systems
Uncover Gradle's mysteries, learn how to write tasks, configure ProGuard, manage dependencies and more!
Krunal Kapadiya
January 21, 2017
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
39 slides
-
Why Gradle? ADT Plugin Release Notes Android Studio is now
the official IDE for Android, so you should migrate your projects to Android Studio as soon as possible. -
1. Download latest gradle version 2. Set environment variables 3.
In command prompt, type gradle -version Installing Gradle -
Say hello to Gradle Write sample code to say “hello”
to gradle In command line, type gradle hello task hello { description “Hey MADMeetup! Don’t say I like your presentation, love it :D” group “Mumbai Android Developers” doLast { println description println group println “Hello World!” } } -
Gradle Structure In Android Studio root --gradleScript ----dependencies.gradle --module 1
----build.gradle --build.gradle -
Change default source set configurations Manage projects and sources //
use this in app level gradle file // folder gradlerecepies\app\src\other android{ sourceSets { main { java.srcDirs = ['other/java'] res.srcDirs = ['other/res1',’other/res2'] manifest.srcFile 'other/AndroidManifest.xml' } androidTest { setRoot 'src/test' } } } -
Manage projects and sources Configure project-wide properties // write this
in project level gradle ext { // define properties compileSdkVersion = 25 buildToolsVersion = "25.0.0" supportLibVersion = "25.0.0" // Contains defaultConfig files versionCode = 1 versionName = "1.0" } -
Manage projects and sources Configure project-wide properties // write this
in app level gradle android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { // ... versionCode rootProject.ext.versionCode versionName rootProject.ext.versionName } } dependencies { compile "com.android.support:appcompat-v7: ${rootProject.ext.supportLibVersion}" compile "com.android.support:recyclerview-v7: ${rootProject.ext.supportLibVersion}" } -
Manage libraries and dependencies Target specific builds with dependency configurations
android { //... configurations { freeDebugApk {} } productFlavors { free { versionCode rootProject.ext.versionCode versionName rootProject.ext.versionName } } } dependencies { freeCompile "com.android.support:appcompat-v7:${rootProject.ext.supportLi bVersion}" freeCompile 'com.google.firebase:firebase-ads:9.8.0' freeDebugApk fileTree(dir: 'libs', include: ['*.jar']) } -
Manage libraries and dependencies Publish non-default variants of your library
android { //... publishNonDefault true configurations { demoDebugCompile {} fullReleaseCompile {} } } dependencies { demoDebugCompile project(path: ':app', configuration: 'demoDebug') fullReleaseCompile project(path: ':fullRelease', configuration: 'fullRelease') } -
Manage libraries and dependencies Publish non-default variants of your library
android { //... defaultPublishConfig "app" configurations { demoDebugCompile {} fullReleaseCompile {} } } dependencies { demoDebugCompile project(path: ':app', configuration: 'demoDebug') fullReleaseCompile project(path: ':fullRelease', configuration: 'fullRelease') } -
Configure multiple APK support 1. Configure separate APKs per screen
density 2. Configure separate APKs per ABI Create different versions of your app android { splits { // Configures multiple APKs based on screen density. density { // Enables building multiple APKs. enable true exclude "ldpi", "mdpi" // APKs for: //include "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" compatibleScreens 'normal', 'large', 'xlarge' } } } Configure separate APKs per screen density -
Configure multiple APK support 1. Configure separate APKs per screen
density 2. Configure separate APKs per ABI Create different versions of your app Configure separate APKs per ABI android { //... splits { abi { // Enables building multiple APKs. enable true reset() include "x86", "armeabi-v7a", "mips" universalApk true } } } -
Configure dynamic version codes Create different versions of your app
android { defaultConfig { //... versionCode 4 } } ext.abiCodes = ['armeabi-v7a':1, mips:2, x86:3] import com.android.build.OutputFile android.applicationVariants.all { variant -> variant.outputs.each { output -> def baseAbiVersionCode = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI)) if (baseAbiVersionCode != null) { output.versionCodeOverride = baseAbiVersionCode * 1000 + variant.versionCode } } } -
Combine multiple product flavors Create different versions of your app
android { //... flavorDimensions "api", "mode" productFlavors { demo { dimension "mode" } full { dimension "mode" } minApi24 { dimension "api" minSdkVersion '24' versionCode 30000 + android.defaultConfig.versionCode versionNameSuffix "-minApi24" } minApi23 { dimension "api" minSdkVersion '23' versionCode 20000 + android.defaultConfig.versionCode versionNameSuffix "-minApi23" } minApi21 { dimension "api" minSdkVersion '21' versionCode 10000 + android.defaultConfig.versionCode versionNameSuffix "-minApi21" } } } -
Filter variants Create different versions of your app android {
flavorDimensions "api", "mode" productFlavors { demo {flavorDimension "api"} full {flavorDimension "mode"} minApi24 { flavorDimension "api" } minApi23 { flavorDimension "api" } minApi21 { flavorDimension "api" } } variantFilter { variant -> def names = variant.flavors*.name if (names.contains("minApi21") && names.contains("demo")) { setIgnore(true) } } } -
Test your app Configure lint options android { lintOptions {
disable 'TypographyFractions','TypographyQuotes' enable 'RtlHardcoded', 'RtlCompat', 'RtlEnabled' check 'NewApi', 'InlinedApi' quiet true abortOnError false ignoreWarnings true } } -
Test your app Change the test build type android {
//… testBuildType “staging” } -
android { // ... buildTypes { release { minifyEnabled true
//for simple project use file, proguard-android-optimize.txt proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { flavor1 { //... } flavor2 { proguardFile 'flavor2-rules.pro' } } } Optimize your build Shrink your code -
Optimize your build Enable code shrinking with Instant Run android
{ buildTypes { debug { minifyEnabled true useProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } -
Optimize your build Configure DEX options android { //... dexOptions
{ // Sets the maximum number of DEX processes // that can be started concurrently. maxProcessCount 8 // Sets the maximum memory allocation pool size // for the dex operation. javaMaxHeapSize "2g" // Enables Gradle to pre-dex library dependencies. preDexLibraries true } } Tips : Total Memory = maxProcessCount * javaMaxHeapSize -
Publish your app Sign your app android { // ....
// Encapsulates signing configurations. signingConfigs { // Creates a signing configuration called "release". release { // Specifies the path to your keystore file. storeFile file("my-release-key.jks") // Specifies the password for your keystore. storePassword "password" // Specifies the identifying name for your key. keyAlias "my-alias" // Specifies the password for your key. keyPassword "password" } } buildTypes { release { // Adds the "release" signing configuration to the release build type. signingConfig signingConfigs.release //... } } } -
Publish your app Remove private signing information from your project
// create new file keystore.properties storePassword=myStorePassword keyPassword=myKeyPassword keyAlias=myKeyAlias storeFile=myStoreFileLocation // write this code in app level gradle def keystorePropertiesFile = rootProject.file("keystore.properties") def keystoreProperties = new Properties() keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) android { //... signingConfigs { config { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } } } -
Bonus Tips Image optimization #PERFMATTERS buildTypes { release { aaptOptions
{ cruncherEnabled = false } } } -
1) Install your latest gradle and set environment variables. 2)
Go to your project path. 3) Run gradle :moduleName:installDebug Bonus Tips: Gradle Command Line Build your app from command line -
Bonus Tips Use this link to find dependancies: http://gradleplease.appspot.com/ See
latest gradle tips here: http://tools.android.com/tech-docs/new-build-system/user-guide Gradle commands info/cheat sheet: https://docs.gradle.org/current/userguide/gradle_command_line.html -
Summary • Manage and configure resource and version dependencies •
Use automation in version code • How to use product flavors, flavors dimension • Split apk if need for abi and screen density • Configure dexOptions in gradle file • Use signing config in Gradle