Book Image

Gradle for Android

By : Kevin Pelgrims
Book Image

Gradle for Android

By: Kevin Pelgrims

Overview of this book

Table of Contents (16 chapters)
Gradle for Android
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The build process for an Android app is an incredibly complex procedure that involves many tools. To begin with, all the resource files are compiled and referenced in a R.java file, before the Java code is compiled and then converted to Dalvik bytecode by the dex tool. These files are then packaged into an APK file, and that APK file is signed with a debug or release key, before the app can finally be installed onto a device.

Going through all these steps manually would be a tedious and time-consuming undertaking. Luckily, the Android Tools team has continued to provide developers with tools that take care of the entire process, and in 2013, they introduced Gradle as the new preferred build system for Android apps. Gradle is designed in a way that makes it easy to extend builds and plug into the existing build processes. It provides a Groovy-like DSL to declare builds and create tasks, and makes dependency management easy. Additionally, it is completely free and open source.

By now, most Android developers have switched to Gradle, but many do not know how to make the best of it, and are unaware of what can be achieved with just a few lines of code. This book aims to help those developers, and turn them into Gradle power users. Starting with the basics of Gradle in an Android context, this book goes on to cover dependencies, build variants, testing, creating tasks, and more.

What this book covers

Chapter 1, Getting Started with Gradle and Android Studio, explains why Gradle is useful, how to get started with Android Studio, and what the Gradle Wrapper is.

Chapter 2, Basic Build Customization, goes into detail about the Gradle build files and tasks, and shows how to do simple customizations to the build process.

Chapter 3, Managing Dependencies, shows how to use dependencies, both local and remote ones, and explains dependency-related concepts.

Chapter 4, Creating Build Variants, introduces build types and product flavors, explains what the difference between them is, and shows how to use signing configurations.

Chapter 5, Managing Multimodule Builds, explains how to manage app, library, and test modules, and mentions how to integrate them into the build.

Chapter 6, Running Tests, introduces several testing frameworks for unit tests and functional tests, how to automate testing and how to get test coverage reports.

Chapter 7, Creating Tasks and Plugins, explains the basics of Groovy, and shows how to create custom tasks and how to hook them into the Android build process. This chapter also explains how to create a reusable plugin.

Chapter 8, Setting Up Continuous Integration, provides guidance on automating builds using the most commonly used CI systems.

Chapter 9, Advanced Build Customization, shows some tips and tricks to shrink APKs, speed up the build process, and split up APKs based on density or platform.

What you need for this book

To follow all the examples, you will need to have access to a computer with Microsoft Windows, Mac OS X, or Linux. You will also need to have the Java Development Kit installed, and it is recommended that you install Android Studio, as it is mentioned in most chapters.

Who this book is for

This book is for Android developers who want to get a better understanding of the build system and become masters of the build process. Throughout the book, we will go from the basics of Gradle, to creating custom tasks and plugins, and automating multiple parts of the build process. You are assumed to be familiar with developing for the Android platform.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Every build.gradle file represents a project."

A block of code is set as follows:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

Any command-line input or output is written as follows:

$ gradlew tasks

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can start a new project in Android Studio by clicking on Start a new Android Studio project on the start screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.