Cover Page

Expert

Android® Studio

Murat Yener
Onur Dundar

 

Wiley Logo

To Nilay, Burak, Semra, and Mustafa Yener, for all your support and the time I needed to write this book.

—Murat

To Canan and my entire family: Aysel, Ismail, Ugur, Umut, Aysun, and Murat.

—Onur

ABOUT THE AUTHORS

MURAT YENER is a code geek, open source committer, Java Champion, and Google Developer Expert on Android, who is working at Intel as an Android developer. He is the author of Professional Java EE Design Patterns (Wrox, 2015). He has extensive experience with developing Android, Java, web, Java EE, and OSGi applications, in addition to teaching courses and mentoring. Murat is an Eclipse committer and one of the initial committers of the Eclipse Libra project.

Murat has been a user group leader at GDG Istanbul since 2009, organizing, participating, and speaking at events. He is also a regular speaker at major conferences such as DroidCon, JavaOne, and Devoxx.

LinkedIn: www.linkedin.com/in/muratyener

Twitter: @yenerm

Blog: www.devchronicles.com

ONUR DUNDAR started his professional career in software engineering at Intel Corporation as a Software Application Engineer working on mobile platforms, tablets, and phones with MeeGo and Android, and later on IoT platforms such as IPTV, Intel's Galileo, and Edison. He is the author of Home Automation with Intel Galileo (Packt Publishing, 2015). Onur presents training sessions on Android application development, so he was keen to author this book.

LinkedIn: www.linkedin.com/in/odundar

Twitter: @odunculuk

Amazon author page: www.amazon.com/Onur-Dundar/e/B00V0VOIGA

ABOUT THE TECHNICAL EDITOR

XAVIER HALLADE is Application Engineer at Intel Software and Services Group in France. He's been working on a wide range of Android frameworks, libraries, and applications. Xavier is a Google Developer Expert in Android, with a focus on the Android NDK, and actively contributes in the Android community, writing articles, helping developers on Stack Overflow, and giving talks around the word.

CREDITS

  1. PROJECT EDITOR

    Tom Dinse

  2. TECHNICAL EDITOR

    Xavier Hallade

  3. PRODUCTION EDITOR

    Barath Kumar Rajasekaran

  4. COPY EDITOR

    Nancy Rapoport

  5. MANAGER OF CONTENT DEVELOPMENT & ASSEMBLY

    Mary Beth Wakefield

  6. PRODUCTION MANAGER

    Kathleen Wisor

  7. MARKETING MANAGER

    Carrie Sherrill

  8. PROFESSIONAL TECHNOLOGY & STRATEGY DIRECTOR

    Barry Pruett

  9. BUSINESS MANAGER

    Amy Knies

  10. EXECUTIVE EDITOR

    Jody Lefevere

  11. PROJECT COORDINATOR, COVER

    Brent Savage

  12. PROOFREADER

    Nancy Bell

  13. INDEXER

    Johnna VanHoose

  14. COVER DESIGNER

    Wiley

  15. COVER IMAGE

    Leo Blanchette/Shutterstock

ACKNOWLEDGMENTS

I WANT TO THANK MY COAUTHOR, ONUR DUNDAR, for all his hard work and for his efforts to keep me on schedule. Without him, this book wouldn't be half as good.

I am grateful to my team and colleagues, in particular John Wei and Sunil Tiptur Nataraj, who gave me the time and flexibility to work on this book, and Angus Yeung for his support. My thanks also go to my Google Developer Relations contacts, Uttam Tripathi, Martin Omander, Baris Yesugey, and others who supported me in all ways possible. I want to thank Alex Theedom for covering for me on my Java EE–related responsibilities while I was deeply buried in Android. No words are enough to thank Jim Minatel and Tom Dinse, who patiently worked on all the details while keeping most of the stresses away from us. And thanks, of course, to everyone at Wrox/Wiley who got this book on the shelves.

I must thank three important people who are responsible for who I am in my professional life.

First, thanks to my dad, Mustafa Yener, for giving me my first computer, a C64, at an early age while I was asking for slot cars. I wrote my very first code on that computer.

Second, thanks to my thesis advisor, Professor Mahir Vardar, for the early guidance I needed to start my career.

Finally, thanks to my lifetime mentor and friend, Naci Dai, who taught me almost everything I know about being a professional software developer.

—Murat Yener

I WANT TO THANK MURAT YENER for taking me on this journey and enabling me to share in the fun of writing this book. With Murat's experience and knowledge, we delivered a high quality book for developers and engineers.

I am thankful to Oktay Ozgun for his wisdom and advice throughout my career and life. His guidance has helped me become a passionate engineer and a better person.

I also want to thank Professor Arda Yurdakul, who was generous with her experience and guidance as she encouraged me to learn more about software and computers and to become a better engineer.

In my final year at Bogazici University, Professor Cem Ersoy, Professor Alper Sen, and Dr. Hande Alemdar provided great assistance. I would also like to thank the folks at Intel—Steve Cutler, Andrew John, Brendan LeFoll, Todor Minchev, Peter Rohr, Rami Radi, Alex Klimovitski, and Marcel Wagner—for their great support and for the opportunities they provided to help me learn and develop in my engineering career.

I want to thank Professor Gurkan Kok, for the interesting stuff I am working on right now.

Thanks to all the Wiley/Wrox editors for their help with this book.

Finally, my special thanks to Ali Caglar Ozkan for motivating me (and taking great photos) after my first book, along with with Aytac Yurdakurban and Engin Efecik, for supporting my continuing interest in writing books.

—Onur Dundar

FOREWORD

“A bad workman always blames his tools.”

When my brother and I were growing up in leafy, suburban England, my father relied on that old proverb. My brother and I often found excuses to avoid the admittedly small number of chores we were asked to do, and we placed the blame for not completing the task at hand on an inanimate object that couldn't answer back. This proverb was my father's standard response to our excuses, and it immediately negated our protestations.

As I've gotten older, I have learned that there is far more wisdom in this old proverb than merely getting young boys to complete their chores. It is not just about ensuring that you have the right tools for the task at hand, but it is also about having the knowledge of how to use them effectively that is key to being productive. If you think about the standard tools in a handyman's toolkit, it is pretty clear to most people that a hammer is not the most appropriate tool to remove a screw from a piece of wood, but as tools become more complex and refined, these distinctions become less clear.

In the Android development world, the de-facto standard development tool is Android Studio, not least because it is under extremely active development by Google—the same organization responsible for Android itself. Since the early preview versions arrived in May 2013, the feature set has grown quite considerably and continues to do so at an impressive rate. If we also consider that Android Studio is built on the foundations of IntelliJ IDEA—which is already an extremely feature-rich development environment—then it should be pretty clear that any analogies with hammers or screwdrivers are going to break down rather quickly. Rather than comparing Android Studio with individual tools, it is, perhaps, better to consider it as the entire toolbox, which contains lots of individual tools that can sometimes be used individually, sometimes be used together, but, when used effectively, can simplify and speed up many of our everyday development tasks—including the really mundane or repetitive ones that we all hate!

Modern software development is so much more than simply writing code, and this is especially true on Android. The main logic of your app may be written in Java. You also have resources (which are largely XML-based) such as vector drawables (which incorporate SVG path data into that XML), build files (which are groovy/grade files), and test source code (which is Java with test domain–specific dialects such as Espresso, Fest, or Hamcrest). This is before you start considering frameworks that change the syntax and flow of your code, such as Rx, and even alternate languages that are gaining traction, such as Kotlin. Mastery of all of this can be hard. The ability to “context switch” between different components, languages, frameworks, and dialects is made much easier by basic features such as code highlighting and pre-compilation, which show errors inline as you code. But we are so used to these that we hardly notice them, and because they have become second nature to us, context switching itself becomes second nature.

While using the tools available until they become second nature is important, a prerequisite for that is actually knowing what tools there are and how to use them effectively. That is where this book comes in. Murat and Onur have provided a guide to Android Studio and its many facets that will be of great value to both the novice and the seasoned Android Studio user alike.

Mark Allison
June 2016

INTRODUCTION

NO MATTER HOW GOOD YOU ARE AT WRITING CODE, without proper knowledge of Integrated Development Environments (IDEs), you will face many obstacles. This book covers Google's Android Studio, the official tool for developing Android applications. Each chapter focuses on a specific topic, progressing from the basics of how to use the IDE to advanced topics such as writing plugins to extend the capabilities of the IDE.

WHO THIS BOOK IS FOR

This book is for developers with any level of experience. Whether you are new to Android or a seasoned Android developer who used Eclipse-based ADT before, this book will bring you to a level where you can unleash your true development potential by making use of Android Studio's tools.

WHAT THIS BOOK COVERS

This book not only covers features of Android Studio essential for developing Android apps but also touches on topics related to the whole development cycle. The following are just a few examples of the topics covered that are basic to Android Studio or that extend its capabilities:

HOW THIS BOOK IS STRUCTURED

Each chapter focuses on a specific topic related to Android Studio or an accompanying tool by explaining why it is needed and how it is used or configured. Some chapters provide code samples to demonstrate the use case or provide an example for the topic.

WHAT YOU NEED TO USE THIS BOOK

Any modern computer with an operating system that is supported by Android SDK and Android Studio is sufficient to use Android Studio, build Android apps, and run the samples given in this book. You need to install appropriate Android SDK, Android Studio, and Java Virtual Machine (JVM) for your OS. Some chapters require additional tools or frameworks to be installed such as Android NDK. You can find more information on exact hardware requirements needed in Chapter 1.

WHY WE WROTE THIS BOOK

In November 2007, Google released a preview version of Android SDK to allow developers to start playing with the new mobile operating system. Roughly two years later, in October 2009, ADT (Android Developer Tools) a plugin set for Eclipse, was released to the public.

As a Google I/O 2009 attendee, I (Murat) was lucky enough to have an Android device and was probably one of the earliest developers to download and install the plugins to my Eclipse. As years passed, we both followed the same passion to download and try new stuff released with new ADT versions.

At the time, I was an Eclipse committer who knew how to write plugins, extend the IDE's capabilities, and introduce the behavior and functionality I needed. So with each release of ADT, I was more and more excited to see what had been done with the tools.

On May 2013, at Google I/O, roughly four years after our love-hate relationship with ADT started, Google announced Android Studio, which soon became the official, supported IDE for Android development. ADT was never perfect. but it was familiar. Like many other developers, we knew all the shortcuts, how things work, what to do when something was not working, workarounds, and how the projects were structured. More significantly, we were able to write our own plugins or inspect ADT plugins to see why something went wrong. However, with the release of Android Studio, suddenly we were all in a new platform that we knew very little about.

We resisted switching to Android Studio for a while, but finally gave it a try. Suddenly, Android, a platform we were long familiar with, was a stranger. The new project structure was very different because of the changes introduced by IntelliJ and Gradle. To adopt IntelliJ, we decided to follow IntelliJ shortcuts instead of using IntelliJ shortcut mapping for Eclipse shortcuts, which made the situation even worse. We were barely able to search for a file or piece of code, navigate through menus, right-click to create files, or even generate some basic getters and setters. We went from being experts with ADT to beginners with Android Studio.

We had finally had enough! We were experienced developers, but struggled with Android Studio and were not able to show our skills. So we started following IntelliJ talks, pinning the IntelliJ shortcut cheat sheet in our cubicles, reading IntelliJ plugin code, and forcing ourselves to use Android Studio in our daily work.

This book is the summary of the lessons we learned walking unaided on this difficult path . This book is what we needed for ourselves when we were switching from Eclipse-based ADT to IntelliJ-based Android Studio. This is why we believe any developer, whether an Android newbie or a seasoned Android developer who used to work on ADT, will find this book useful for developing his or her knowledge of the tools that are actually there to support his or her coding skills.

Quoting Alex Theedom, co-author of my previous book: “Every chapter that we wrote has this goal: Write content that we would like to read ourselves.” We followed the same goal with Onur and the result is the book you are holding in your hands.

We hope that you enjoy reading this book as much as we enjoyed writing it.

CONVENTIONS

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

As for styles in the text:

We use a monofont type for code examples.
We use bold to emphasize code that is of particular importance in the current context.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at www.wiley.com/go/expertandroid. Once at the site, simply click the Download Code link on the book's detail page to obtain all the source code for the book.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or a faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.
  2. Read the terms of use and click Agree.
  3. Complete the required information to join as well as any optional information you wish to provide and click Submit.
  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.