About this ebook
- Create custom Gradle tasks and plugins for your Android projects
- Configure different build variants, each with their own dependencies and properties
- Manage multi-module projects, and integrate modules interdependently
If you are an experienced Android developer wanting to enhance your skills with the Gradle Android build system, then this book is for you. As a prerequisite, you will need some knowledge of the concepts of Android application development.
Related to Gradle for Android
Related ebooks
Mastering Android NDK Rating: 0 out of 5 stars0 ratingsAndroid Application Development with Maven Rating: 0 out of 5 stars0 ratingsAndroid Studio Cookbook Rating: 4 out of 5 stars4/5Android System Programming Rating: 5 out of 5 stars5/5Gradle Essentials Rating: 0 out of 5 stars0 ratingsLearn iOS Application Development: Take Your Mobile App Development Skills to the Next Level with Swift and Xcode (English Edition) Rating: 0 out of 5 stars0 ratingsAngularJS Deployment Essentials Rating: 0 out of 5 stars0 ratingsAsynchronous Android Rating: 4 out of 5 stars4/5Jasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Android Application Development Rating: 5 out of 5 stars5/5iOS 13 Programming for Beginners - Fourth Edition: Get started with building iOS apps with Swift 5 and Xcode 11, 4th Edition Rating: 0 out of 5 stars0 ratingsAndroid Design Patterns and Best Practice Rating: 5 out of 5 stars5/5Application Development with Swift Rating: 0 out of 5 stars0 ratingsTest-Driven iOS Development with Swift Rating: 5 out of 5 stars5/5Learning AngularJS Animations Rating: 4 out of 5 stars4/5Learning Node.js for Mobile Application Development Rating: 0 out of 5 stars0 ratingsInstant MinGW Starter Rating: 0 out of 5 stars0 ratingsTypeScript Essentials Rating: 4 out of 5 stars4/5Git Best Practices Guide Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsJavaScript Mobile Application Development Rating: 0 out of 5 stars0 ratingsFlask Blueprints Rating: 0 out of 5 stars0 ratingsGetting Started with React Native Rating: 4 out of 5 stars4/5Learning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5Backbone.js Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsInstant Play Framework Starter Rating: 0 out of 5 stars0 ratingsMastering Spring 5.0 Rating: 5 out of 5 stars5/5Beginning Flutter: A Hands On Guide to App Development Rating: 0 out of 5 stars0 ratings
Internet & Web For You
Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsSix Figure Blogging Blueprint Rating: 5 out of 5 stars5/5No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Coding with AI For Dummies Rating: 1 out of 5 stars1/5Blogging For Dummies Rating: 0 out of 5 stars0 ratingsPodcasting For Dummies Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Introduction to Internet Scams and Fraud: Credit Card Theft, Work-At-Home Scams and Lottery Scams Rating: 4 out of 5 stars4/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Content Chemistry: The Illustrated Handbook for Content Marketing Rating: 5 out of 5 stars5/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Wordpress for Beginners: The Easy Step-by-Step Guide to Creating a Website with WordPress Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Ultimate guide for being anonymous: Avoiding prison time for fun and profit Rating: 5 out of 5 stars5/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5
Reviews for Gradle for Android
0 ratings0 reviews
Book preview
Gradle for Android - Kevin Pelgrims
Table of Contents
Gradle for Android
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with Gradle and Android Studio
Android Studio
Staying up to date
Understanding Gradle basics
Projects and tasks
The build lifecycle
The build configuration file
The project structure
Creating a new project
Getting started with the Gradle Wrapper
Getting the Gradle Wrapper
Running basic build tasks
Migrating from Eclipse
Using the import wizard
Migrating manually
Keeping the old project structure
Converting to the new project structure
Migrating libraries
Summary
2. Basic Build Customization
Understanding the Gradle files
The settings file
The top-level build file
The module build file
Plugin
Android
Dependencies
Getting started with tasks
Base tasks
Android tasks
Inside Android Studio
Customizing the build
Manipulating manifest entries
Inside Android Studio
BuildConfig and resources
Project-wide settings
Project properties
Default tasks
Summary
3. Managing Dependencies
Repositories
Preconfigured repositories
Remote repositories
Local repositories
Local dependencies
File dependencies
Native libraries
Library projects
Creating and using library project modules
Using .aar files
Dependency concepts
Configurations
Semantic versioning
Dynamic versions
Inside Android Studio
Summary
4. Creating Build Variants
Build types
Creating build types
Source sets
Dependencies
Product flavors
Creating product flavors
Source sets
Multiflavor variants
Build variants
Tasks
Source sets
Resource and manifest merging
Creating build variants
Variant filters
Signing configurations
Summary
5. Managing Multimodule Builds
The anatomy of a multimodule build
The build lifecycle revisited
Module tasks
Adding modules to a project
Adding a Java library
Adding an Android library
Integrating Android Wear
Using Google App Engine
Analyzing the build file
Using the backend in an app
Custom tasks
Tips and best practices
Running module tasks from Android Studio
Speeding up multimodule builds
Module coupling
Summary
6. Running Tests
Unit tests
JUnit
Robolectric
Functional tests
Espresso
Test coverage
Jacoco
Summary
7. Creating Tasks and Plugins
Understanding Groovy
Introduction
Classes and members
Methods
Closures
Collections
Groovy in Gradle
Getting started with tasks
Defining tasks
Anatomy of a task
Using a task to simplify the release process
Hooking into the Android plugin
Automatically renaming APKs
Dynamically creating new tasks
Creating your own plugins
Creating a simple plugin
Distributing plugins
Using a custom plugin
Summary
8. Setting Up Continuous Integration
Jenkins
Setting up Jenkins
Configuring the build
TeamCity
Setting up TeamCity
Configuring the build
Travis CI
Configuring the build
Further automation
The SDK manager plugin
Running tests
Continuous deployment
Beta distribution
Summary
9. Advanced Build Customization
Reducing the APK file size
ProGuard
Shrinking resources
Automatic shrinking
Manual shrinking
Speeding up builds
Gradle properties
Android Studio
Profiling
Jack and Jill
Ignoring Lint
Using Ant from Gradle
Running Ant tasks from Gradle
Importing an entire Ant script
Properties
Advanced app deployment
Split APK
Summary
Index
Gradle for Android
Gradle for Android
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2015
Production reference: 1140715
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-682-8
www.packtpub.com
Credits
Author
Kevin Pelgrims
Reviewers
Peter Friese
Jonathan H. Kau
Takuya Miyamoto
Marco A. Rodriguez-Suarez
Felix Schulze
Hugo Visser
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Nikhil Karkal
Content Development Editor
Prachi Bisht
Technical Editor
Pankaj Kadam
Copy Editors
Puja Lalwani
Laxmi Subramanian
Project Coordinator
Shipra Chawhan
Proofreader
Safis Editing
Indexer
Tejal Soni
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
About the Author
Kevin Pelgrims started his career as a .NET developer in Belgium. After some years working on Windows and web development for corporate clients, he moved to Copenhagen to become part of its start-up community. There, he began working on mobile platforms, and within a year, he was the lead developer of several Android and Windows Phone apps. Kevin is also a regular speaker at the Android developers group in Copenhagen. He maintains multiple Android apps in his spare time and likes to experiment with server-side technology. When he is not busy writing code, he is most likely playing the guitar with his wife and their cat.
To follow what Kevin is up to, you can take a look at his blog at http://www.kevinpelgrims.com or follow him on Twitter at @kevinpelgrims.
I could not have written this book without the support of my brilliant wife, Louise. Not only did she put up with me spending entire evenings and weekends with my computer, she also proofread the entire book, improving the language and the grammar.
I would like to thank Packt Publishing for giving me the opportunity to write this book, especially Nikhil Karkal and Prachi Bisht, for believing in me and guiding me throughout the process. I appreciate the help of my friend Koen Metsu, whose comments helped in making sure that the explanations and examples are easy to understand. Thanks to Anders Bo Pedersen and Emanuele Zattin for coming up with several improvements.
I also want to thank the reviewers: Hugo Visser, Peter Friese, Felix Schulze, Takuya Miyamoto, Jonathan H. Kau, and Marco Rodriguez-Suarez. Their input has significantly improved the content of this book. Last but not least, special thanks to our cat, Jigsaw, who encouraged me to stay focused and keep going, by sitting on my lap so I couldn't get up.
About the Reviewers
Peter Friese works as a developer advocate at Google in the Developer Relations team in London, UK. He is a regular open source contributor, blogger, and public speaker. He is on Twitter at @peterfriese and on Google+ at +PeterFriese. You can find his blog at http://www.peterfriese.de.
Jonathan H. Kau is an experienced Android developer, having published several standalone applications to Google Play and worked on numerous hackathon projects leveraging Android.
In the past, he has worked on the mobile team at Yelp and as a contractor for Propeller Labs. Jonathan is currently working in the engineering team at Shyp, both on the Android application and the corresponding backend API.
I'd like to thank Packt Publishing for giving me the opportunity to review this educational book that will hopefully simplify the use of Gradle for many developers.
Takuya Miyamoto is a full-stack developer with 6 years of experience in designing, implementing, and maintaining various types of web services and APIs such as e-commerce and SNS. He also has some experience as an Android developer. He has worked as a senior engineer in an enterprise, as a lead engineer in start-ups, and is currently working as an individual developer.
Marco A. Rodriguez-Suarez is the head of mobile at Snapwire, a collaborative platform and marketplace that connects photographers with brands and businesses around the world. Prior to that, he worked in mobile consulting on diverse projects, ranging from topics such as video streaming to game emulation. He has been working with Android since the release of the first public device in 2008 and has been hooked on mobile development ever since. He is passionate about build systems and has extensive experience with Gradle, Maven, and Ant. Marco received his master's degree in electrical engineering from the University of California at Santa Barbara.
Felix Schulze is leading the mobile software development department at AutoScout24 and is responsible for the Android and iOS development. He also gives a lot of talks about continuous integration in app development and contributes to open source tools. His Twitter handle is @x2on; you can also check out his website at www.felixschulze.de.
Hugo Visser has many years of experience as a software engineer, ranging from server-side to desktop and from web to mobile. Since the original announcement, he has closely followed the development of the Android platform, which resulted in his first app in 2009, Rainy Days, which has since been downloaded over 1,000,000 times worldwide.
He runs his own company, Little Robots, which focuses on apps and other clever uses of the Android platform. He has been named the Google Developer Expert for Android by Google and is an organizer of The Dutch Android User Group, which is a community in the Netherlands where Android professionals can meet and share knowledge with each other during monthly meetings.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
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,