Table Of ContentReal-World Android by Tutorials
Real-World Android by Tutorials
By Ricardo Costeira, Subhrajyoti Sen & Kolin Stürt
Copyright ©2021 Razeware LLC.
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com 2
Real-World Android by Tutorials
Dedications
To my parents, Céu and Manuel, and sister, Joana, who always
gave everything they could and more to ensure my education,
happiness and well-being. To my partner, Joana, for all the
love, support and unbelievable patience while I was writing
the book. To my friend, Carlos, for all the support and
understanding every time I told him I didn’t have the time to
help him with GDG and Kotlin Knights events. And finally, to
the amazing Android devs from my team at Mindera - Maja,
Rita, Garcês, Belchi and Gui - for all the incredible discussions,
for teaching me so much and for making me love what I do
even more.
— Ricardo Costeira
I’d like to thank my parents, @StephanieBraganza, Pom-Pom
the Pomeranian (IG @PomThePomeranian) and 0xAKBArt, as
well as The Wildlife Trade Monitoring Network and The Save
Movement for inspiration of the app concepts.
— Kolin Stürt
I’d like to thank my parents, friends, and colleagues, who have
always been very supportive and pushed me to achieve more.
This book would not have been possible without them.
— Subhrajyoti Sen
raywenderlich.com 3
Real-World Android by Tutorials About the Team
About the authors
Ricardo Costeira is an author of this book. He is an Android dev
with a crush on clean code and software architecture. Based in
Portugal, Ricardo works as a senior Android engineer at Mindera,
where he builds and maintains a retailer app with tens of
thousands of daily active users. Ricardo loves Android and the
community, so it’s only natural that he tries to have an active part
in it! He’s a co-organizer of GDG Coimbra and a co-founder of
Kotlin Knights. He occasionally writes (mostly about Android) at
his site, ricardocosteira.com, or for raywenderlich.com. He loves
cats, food and the gym and he’s a specialty coffee hobbyist. You can
find him on Twitter at @rcosteira79.
Subhrajyoti Sen is an author of this book. He is an Android
Engineer at KeepTruckin, where he develops apps to improve the
trucking industry. Before that, he also worked on apps to improve
the experience of Indian investors. He believes in the power of
open source and communities, and actively tries to give back.
When not writing code, you can find him binge-watching anime,
reading up on public policy or playing Rocket League.
Kolin Stürt is an author of this book. He is a software team lead
with a focus on encryption, reverse-engineering, forensics and
application hardening. He’s worked on many platforms, most
notably Android, iOS and Linux. He has an interest in networking
and has been hacking and developing apps since 2009. Outside of
cybersecurity, he composes and performs music, as well as
practicing and holding a black belt in Aikido. You can find him at
https://kolinsturt.github.io.
raywenderlich.com 4
Real-World Android by Tutorials About the Team
About the editors
Andy Gibel is a technical editor of this book. He started with
Android around 2010 and never looked back. He’s written Android
apps that run on phones, tablets, embedded devices and even a dog
(long story). He once learned himself a Haskell and is fascinated by
functional programming and general CS theory.
Eric Crawford is a technical editor of this book. He is a Senior
Software Developer at John Deere, where he bounces between iOS
and Android development. Before coming to Deere, he did
freelance mobile development and server side web development
using Java. In his free time, he likes to dabble in other platforms,
like IOT and cloud computing.
Sandra Grauschopf is the editor of this book. She is a freelance
writer, editor and content strategist as well as the Editing Team
Lead at raywenderlich.com. She loves to untangle tortured
sentences and to travel the world with a trusty book in her hand.
Massimo Carli is the final pass editor of this book. Massimo has
been working with Java since 1995, when he co-founded the first
Italian magazine about this technology http://www.mokabyte.it.
After many years creating Java desktop and enterprise
applications, Massimo started to work in the mobile world. In 2001,
he wrote his first book about J2ME. After many J2ME and
Blackberry apps, he then started to work with Android in 2008. The
same year, Massimo wrote the first Italian book about Android, and
it became a best seller on Amazon.it; that was the first of a series
of 12 books. Massimo has worked at Yahoo and Facebook and he’s
currently working as a Senior Engineer at Spotify. Massimo is a
musical theater lover and a supporter of the soccer team, S.P.A.L.
raywenderlich.com 5
Real-World Android by Tutorials About the Team
About the artist
Vicki Wenderlich is the designer and artist of the cover of this
book. She is Ray’s wife and business partner. She’s a digital artist
who creates illustrations, game art and a lot of other art or design
work for the tutorials and books on raywenderlich.com. When she’s
not making art, she loves hiking, a good glass of wine and
attempting to create the perfect cheese plate.
raywenderlich.com 6
Real-World Android by Tutorials
Table of Contents: Overview
Book License............................................................................................. 16
Before You Begin................................................................ 17
What You Need........................................................................................ 18
Book Source Code & Forums............................................................. 19
About the Cover...................................................................................... 20
Introduction.............................................................................................. 22
Section I: Developing Real World Apps ...................... 24
Chapter 1: Introduction........................................................... 25
Chapter 2: Starting from the Beginning............................ 30
Chapter 3: Domain Layer......................................................... 46
Chapter 4: Data Layer — Network....................................... 66
Chapter 5: Data Layer — Caching........................................ 93
Chapter 6: Building Features — Animals Near You.... 124
Chapter 7: Building Features — Search........................... 157
Section II: Modularizing Your App ............................. 198
Chapter 8: Multi-Module Apps.......................................... 199
Chapter 9: Dynamic Features Theory............................. 221
Chapter 10: Building a Dynamic Feature....................... 229
Section III: Enhancing Your UI ..................................... 257
Chapter 11: Animations........................................................ 258
Chapter 12: MotionLayout & Motion Editor................ 281
raywenderlich.com 7
Real-World Android by Tutorials
Chapter 13: Custom Views.................................................. 304
Chapter 14: Style & Theme.................................................. 330
Section IV: Securing Your App..................................... 356
Chapter 15: User Privacy...................................................... 357
Chapter 16: Securing Data at Rest................................... 372
Chapter 17: Securing Data in Transit............................... 396
Chapter 18: App Hardening................................................. 419
Section V: Maintaining Your App................................ 437
Chapter 19: Firebase Integration...................................... 438
Chapter 20: Release Optimizations................................. 460
Chapter 21: Advanced Debugging.................................... 473
Chapter 22: App Analysis..................................................... 499
Conclusion.............................................................................................. 520
raywenderlich.com 8
Real-World Android by Tutorials
Table of Contents: Extended
Book License. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Before You Begin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
About the Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
How to read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Section I: Developing Real World Apps . . . . . . . . . . . . 24
Chapter 1: Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
What is this book about?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Who is this book for?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The sample project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Signing up for an API key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Chapter 2: Starting from the Beginning . . . . . . . . . . . . . . . . . . . . . 30
Package by feature approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Full stack features through layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Bridging requirements and implementation. . . . . . . . . . . . . . . . . . . . . . . . . . 38
Devising a plan of attack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Chapter 3: Domain Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
What is a domain layer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Creating your domain model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Inverting dependencies with repositories. . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Testing your domain logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
raywenderlich.com 9
Real-World Android by Tutorials
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 4: Data Layer — Network. . . . . . . . . . . . . . . . . . . . . . . . . . . 66
What is a data layer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Network data models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Connecting to the API with Retrofit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Testing the network code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Key points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Chapter 5: Data Layer — Caching. . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Cache data models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Caching data with Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Managing cache dependencies with Hilt. . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Putting it all together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Testing your repository. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Chapter 6: Building Features — Animals Near You. . . . . . . . . 124
What is a presentation layer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Making your life easier with architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Building animals near you . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Creating the UI components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Creating the view state. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Creating the data flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Your first use case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Connecting the layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Hilt on Android components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Displaying cute animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Allowing an infinite scroll. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Chapter 7: Building Features — Search. . . . . . . . . . . . . . . . . . . . . 157
Building a search feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
raywenderlich.com 10