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 can’t 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 book’s 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. Appendix A contains a quick summary of Groovy syntax and techniques. Groovy concepts are also reviewed as they come up in various recipes. Appendix B 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:

Italic

Indicates 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.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/kousen/GradleRecipesForAndroid.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Gradle Recipes for Android by Ken Kousen (O’Reilly). Copyright 2016 Gradleware, Inc., 978-1-4919-4702-9.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/gradle-recipes-for-android.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

The author would like to thank several members of Gradle, Inc. for their gracious help and assistance, including Hans Dockter, Luke Daley, Rooz Mohazabbi, and Cédric Champeau, among others. They are part of the reason both Gradle the technology and Gradle the company have such a bright future.

I also need to thank Xavier Ducrohet, head of the Android Studio team at Google as well as head of the Android plugin project. His hard work made both the IDE and the plugin a joy to use. I’m also glad he and his team haven’t found time to update the online documentation sufficiently, leaving a great opening for this book.1

As a regular member of the No Fluff, Just Stuff conference series, I need to thank Jay Zimmerman for the opportunity to present on both Gradle- and Android-related topics many times over the years. I’m very happy to be part of No Fluff speaker community, many of whom have become good friends. I’m especially thinking of Nate Schutta, Raju Gandhi, Venkat Subramaniam, Neal Ford, Dan Hinojosa, Brian Sletten, Michael Carducci, and Craig Walls, but I could add another dozen names to that list without a problem. I’m also sure I’ll hear about the people I didn’t mention at my next No Fluff conference after they get around to reading this.

I’m also grateful to Matthew McCullough and Tim Berglund, the authors of the previous books of this series. Both men are friendly and helpful, and I’m honored to have my book included with theirs.

The reviewers for this book helped improve it considerably. I have to call out specifically the contributions of Andrew Reitz and James Harmon, who provided great insights into the technical parts of the book as well as its readability.

I have to mention my editors at O’Reilly, Meghan Blanchette and Brian Foster. Meghan was key in launching the book and helping edit the early stages, and Brian took over from her and shepherded it throughout the rest of the process. I’m grateful to the rest of the team at O’Reilly who helped throughout, even if I only vaguely understood the massive details that go into bringing a book to its final published form.

Even though it is ostensibly a competitor, the book Gradle for Android by Kevin Pelgrims (Packt Publishing) is excellent and taught me a lot. My book takes a different, recipe-based approach and is, of course, newer and therefore more up-to-date, but if you can do so I honestly recommend getting both.

Most of all I need to thank my wife Ginger and my son Xander for all the support they’ve given me over the years. I’m sorry again for getting involved in a book project so soon after finishing the previous one, and I promise I’ll wait a while before starting the next one (probably).

Thank you, too, for reading the book. I hope you find it useful. Any errors or omissions are, of course, my own.

1 That was a joke. Honestly. But if you’d like to update the website now, I’m sure nobody will mind.

Get Gradle Recipes for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.