Twenty-five years is just the beginning.
Foreword
This is the book we needed. We were about halfway through writing Head First Android Development when Google switched IDEs. At the time, pretty much everyone was using Eclipse with the Android Development Toolkit installed. But now, Google was pushing for developers to switch to the Idea-based Android Studio.
Were used to this kind of thingmost technical authors are. Some manufacturer startup switches from some new shiny thing to some even newer, even shinier thing. It happens all the time. You rewrite all your example code, update all the images, drop the features that are now irrelevant, and include whats most useful from the new technology. But what made the switch from Eclipse to Android Studio different was that under the hood the new IDE had a much, much more powerful engine.
Android Studio used Gradle for building, packaging, and deploying code. Other than knowing the name, neither of us had any experience of using Gradle directly. It was kind of like Maven, but rather than using lengthy XML configuration files, it used a sturdy and concise scripting language: Groovy.
We replaced all the screenshots, and updated the text in the seven or so chapters that were already written and then moved on to write the rest of the book. But it soon became clear that the process of creating applications with Gradle was subtly, but significantly different. Pretty much anything that you could do from the IDE was suddenly possible from the command line, which meant we could automate our build pipelines. It took just a few key presses to try out different library versions, or different build flavors. And because everything is just code, we could write the builds in the same way that we wrote the rest of the app.
Learning Gradle is now an important task for every Android developer. Its up there with knowing Java, or understanding the Activity lifecycle. But learning Gradle through trial-and-error can sometimes be a painful process. And thats where this book comes in. In these pages, youll find a wealth of useful recipes that will help you avoid the commonest build problems. Whether youre setting up a testing system, automating your signed APK production, or just trying to speed up your build pipeline, this book is for you. Kens lively writing style and realistic examples will keep you coming back again and again. With this book, Ken has shown that not only is he the go-to guy for Groovy, hes now also the go-to guy for Gradle.
Dawn and David Griffiths
Authors, Head First
Android Development
April 20th, 2016
Preface
About the Book
This book contains recipes for working with the Gradle build system for Android projects. Gradle is one of the most popular tools for building applications from the Java world, and is expanding into other languages like C++. The Android team at Google adopted Gradle as the preferred build system for Android in the spring of 2013, and its use has grown steadily since then.
Since Gradle comes from the Groovy ecosystem, many Android developers may not be familiar with it. Groovy, however, is very easy for existing Java developers to learn. The purpose of this book is to provide examples that help you use Gradle to accomplish the most common build tasks for Android applications.
All code examples use Android SDK version 23, with emulators from either Marshmallow (Android 6) or Lollipop (Android 5.*). Android Studio versions 2.0 or 2.1 (beta) were used as the primary IDE, which included Gradle version 2.10 or above as the build tool.
Prerequisites
The Gradle plugin for Android involves at least some knowledge of Java, Groovy, Gradle, and Android. Since entire books are available for each of those topics, they cant all be covered in detail here.
The text in this book is aimed chiefly at developers who are comfortable with Android development. Very little Android background is provided, though complete code listings of all examples are available through the books GitHub repository. Understanding Android means understanding Java, so that background is assumed as well.
Very little knowledge of either Groovy or Gradle is assumed, however. has basic Gradle information, but the recipes themselves discuss Gradle in detail throughout the book.
Beyond those limitations, the book is designed to be as self-contained as possible, with links to external references (especially documentation) provided wherever appropriate.
The book also makes extensive use of Android Studio, as it is now the only officially supported IDE for Android development. Android Studio provides views and tools for Gradle, which are illustrated in many recipes. While the book is not designed to be a tutorial on Android Studio, its relevant features are shown wherever possible, and if that helps the reader learn more about the IDE, so much the better.
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.