G radle R ecipes for Android MASTER THE NEW BUILD SYSTEM FOR ANDROID Ken Kousen Gradle Recipes for Android Master the New Build System for Android Ken Kousen Gradle Recipes for Android by Ken Kousen Copyright © 2016 Gradleware, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Heather Scherer and Brian Foster Indexer: Angela Howard Production Editor: Colleen Lobner Interior Designer: David Futato Copyeditor: Colleen Toporek Cover Designer: Karen Montgomery Proofreader: Kim Cofer Illustrator: Rebecca Demarest June 2016: First Edition Revision History for the First Edition 2016-06-02: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491947029 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Gradle Recipes for Android, the cover image of a great potoo, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-94702-9 [LSI] This book is dedicated to my wife Ginger: my best friend, my partner, and the love of my life. Twenty-five years is just the beginning. Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Gradle for Android Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Gradle Build Files in Android 1 1.2 Configure SDK Versions and Other Defaults 6 1.3 Executing Gradle Builds from the Command Line 9 1.4 Executing Gradle Builds from Android Studio 15 1.5 Adding Java Library Dependencies 18 1.6 Adding Library Dependencies Using Android Studio 23 1.7 Configuring Repositories 26 2. From Project Import to Release. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.1 Setting Project Properties 29 2.2 Porting Apps from Eclipse ADT to Android Studio 33 2.3 Porting Apps from Eclipse ADT Using Eclipse 37 2.4 Upgrading to a Newer Version of Gradle 40 2.5 Sharing Settings Among Projects 43 2.6 Signing a Release APK 45 2.7 Signing a Release APK Using Android Studio 49 3. Build Types and Flavors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.1 Working with Build Types 53 3.2 Product Flavors and Variants 56 3.3 Merging Resources 60 3.4 Flavor Dimensions 67 3.5 Merging Java Sources Across Flavors 71 v 4. Custom Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1 Writing Your Own Custom Tasks 77 4.2 Adding Custom Tasks to the Build Process 80 4.3 Excluding Tasks 83 4.4 Custom Source Sets 85 4.5 Using Android Libraries 88 5. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.1 Unit Testing 97 5.2 Testing with the Android Testing Support Library 103 5.3 Functional Testing with Robotium 108 5.4 Activity Testing with Espresso 112 6. Performance and Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.1 Performance Recommendations 119 6.2 DSL Documentation 125 A. Just Enough Groovy to Get By. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 B. Gradle Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 vi | Table of Contents 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 every‐ one was using Eclipse with the Android Development Toolkit installed. But now, Google was pushing for developers to switch to the Idea-based Android Studio. We’re used to this kind of thing—most 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 what’s 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 sig‐ nificantly different. Pretty much anything that you could do from the IDE was sud‐ denly 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 differ‐ ent 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. It’s up there with knowing Java, or understanding the Activity lifecycle. But learning Gradle through trial-and-error can sometimes be a painful process. And that’s where this book comes in. In these pages, you’ll find a wealth of useful recipes that will help you avoid the commonest build problems. Whether you’re setting up a testing system, automating your signed APK production, or just trying to speed up your build pipe‐ vii line, this book is for you. Ken’s 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, he’s now also the go-to guy for Gradle. —Dawn and David Griffiths Authors, Head First Android Development April 20th, 2016 viii | Foreword
Description: