0% found this document useful (0 votes)
145 views

Instant download Real World Android by Tutorials Second Edition Professional App Development With Kotlin Raywenderlich Tutorial Team pdf all chapter

Development

Uploaded by

dhunnaguthu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Instant download Real World Android by Tutorials Second Edition Professional App Development With Kotlin Raywenderlich Tutorial Team pdf all chapter

Development

Uploaded by

dhunnaguthu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Download Full Version ebook - Visit ebookmeta.

com

Real World Android by Tutorials Second Edition


Professional App Development With Kotlin
Raywenderlich Tutorial Team

https://ebookmeta.com/product/real-world-android-by-
tutorials-second-edition-professional-app-development-with-
kotlin-raywenderlich-tutorial-team/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmeta.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

Android Accessibility by Tutorials Make Your Apps


Accessible for All Audiences 2nd Edition Raywenderlich
Tutorial Team
https://ebookmeta.com/product/android-accessibility-by-tutorials-make-
your-apps-accessible-for-all-audiences-2nd-edition-raywenderlich-
tutorial-team/
ebookmeta.com

iOS Animations by Tutorials Seventh Edition Setting Swift


in Motion Raywenderlich Tutorial Team

https://ebookmeta.com/product/ios-animations-by-tutorials-seventh-
edition-setting-swift-in-motion-raywenderlich-tutorial-team/

ebookmeta.com

Advanced IOS App Architecture Fourth Edition


Raywenderlich Tutorial Team

https://ebookmeta.com/product/advanced-ios-app-architecture-fourth-
edition-raywenderlich-tutorial-team/

ebookmeta.com

Design Patterns in Modern C++20: Reusable Approaches for


Object-Oriented Software Design 2nd Edition Dmitri
Nesteruk
https://ebookmeta.com/product/design-patterns-in-modern-c20-reusable-
approaches-for-object-oriented-software-design-2nd-edition-dmitri-
nesteruk/
ebookmeta.com
The Everything Kids Basketball Book A Guide to Your
Favorite Players and Teams and Tips on Playing Like a Pro
5th Edition Bob Schaller
https://ebookmeta.com/product/the-everything-kids-basketball-book-a-
guide-to-your-favorite-players-and-teams-and-tips-on-playing-like-a-
pro-5th-edition-bob-schaller/
ebookmeta.com

Bulldog Mike Faricy

https://ebookmeta.com/product/bulldog-mike-faricy/

ebookmeta.com

Industry 4 0 and Circular Economy Towards a Wasteless


Future or a Wasteful Planet 1st Edition Antonis
Mavropoulos
https://ebookmeta.com/product/industry-4-0-and-circular-economy-
towards-a-wasteless-future-or-a-wasteful-planet-1st-edition-antonis-
mavropoulos/
ebookmeta.com

Link Keepers of the Lake 5 1st Edition Emilia Hartley

https://ebookmeta.com/product/link-keepers-of-the-lake-5-1st-edition-
emilia-hartley/

ebookmeta.com

Transforming Airlines A Flight Plan for Navigating


Structural Changes 1st Edition Nawal K Taneja

https://ebookmeta.com/product/transforming-airlines-a-flight-plan-for-
navigating-structural-changes-1st-edition-nawal-k-taneja/

ebookmeta.com
Design of Innovation Processes Flow From Idea to Market
Launch With Higher Speed and Value Time After Time 1st
Edition Darrell Velegol
https://ebookmeta.com/product/design-of-innovation-processes-flow-
from-idea-to-market-launch-with-higher-speed-and-value-time-after-
time-1st-edition-darrell-velegol/
ebookmeta.com
Real-World Android by Tutorials Real-World Android by Tutorials

Real-World Android by Tutorials


Ricardo Costeira, Subhrajyoti Sen, Kolin Stürt & Antonio Roa-Valverde

Copyright ©2022 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

Table of Contents: Overview


Book License ............................................................................................. 12
Before You Begin ................................................................ 13
What You Need ........................................................................................ 14
Book Source Code & Forums ............................................................. 15
Acknowledgments .................................................................................. 19
Section I: Developing Real World Apps ...................... 20
Chapter 1: Introduction ........................................................... 21
Chapter 2: Starting From the Beginning ........................... 26
Chapter 3: Domain Layer......................................................... 42
Chapter 4: Data Layer — Network....................................... 62
Chapter 5: Data Layer — Caching ........................................ 89
Chapter 6: Building Features — Animals Near You .... 121
Chapter 7: Building Features — Search........................... 155
Section II: Modularizing Your App ............................. 197
Chapter 8: Multi-Module Apps .......................................... 198
Chapter 9: Dynamic Features Theory ............................. 220
Chapter 10: Building a Dynamic Feature ....................... 228
Section III: Enhancing Your UI ..................................... 257
Chapter 11: Animations ........................................................ 258
Chapter 12: MotionLayout & Motion Editor ................ 281
Chapter 13: Custom Views .................................................. 304

raywenderlich.com 3
Real-World Android by Tutorials

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 ................................. 461
Chapter 21: Advanced Debugging .................................... 471
Chapter 22: App Analysis ..................................................... 498
Conclusion .............................................................................................. 519

raywenderlich.com 4
Real-World Android by Tutorials

Table of Contents: Extended


Book License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
About the Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Content Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Section I: Developing Real World Apps . . . . . . . . . . . . 20


Chapter 1: Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
What Is This Book About? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Who Is This Book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The Sample Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Signing Up for an API Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Where to Go From Here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 2: Starting From the Beginning . . . . . . . . . . . . . . . . . . . . . 26
Package by Feature Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Full Stack Features Through Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Bridging Requirements and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 35
Devising a Plan of Attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Key Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Chapter 3: Domain Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
What Is a Domain Layer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Creating Your Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Inverting Dependencies With Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Testing Your Domain Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

raywenderlich.com 5
Real-World Android by Tutorials

Key Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Chapter 4: Data Layer — Network . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
What Is a Data Layer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Network Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Connecting to the API With Retrofit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Testing the Network Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Key Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 5: Data Layer — Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Cache Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Caching Data With Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Managing Cache Dependencies With Hilt . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Testing Your Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Chapter 6: Building Features — Animals Near You . . . . . . . . . 121
What Is a Presentation Layer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Making Your Life Easier With Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 123
Building Animals Near You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Creating the UI Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Creating the View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Creating the Data Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Your First Use Case. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Connecting the Layers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Hilt on Android Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Displaying Cute Animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Allowing an Infinite Scroll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Chapter 7: Building Features — Search. . . . . . . . . . . . . . . . . . . . . 155
Building a Search Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

raywenderlich.com 6
Real-World Android by Tutorials

Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156


Searching Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Triggering the Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Adding Search to the Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Adding Search to the ViewModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Searching Remotely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Canceling Old Search Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Finishing Touches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
ViewModel Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
UI Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Section II: Modularizing Your App . . . . . . . . . . . . . . . . 197


Chapter 8: Multi-Module Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
What is Modularization? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Types of Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Why Modularization Is Good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Using Gradle With Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Looking Back Over Your Decisions so Far . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Creating the Onboarding Feature Module . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Navigating Between Feature Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Using Deep Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Additional Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 9: Dynamic Features Theory . . . . . . . . . . . . . . . . . . . . . . 220
Android App Bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Dynamic Delivery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
What Are Dynamic Features? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Chapter 10: Building a Dynamic Feature . . . . . . . . . . . . . . . . . . . 228

raywenderlich.com 7
Real-World Android by Tutorials

PetSave’s New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229


Deciding How to Create Your Dynamic Feature . . . . . . . . . . . . . . . . . . . . 231
Preparing the App MNodule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Preparing the Feature Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Handling Navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Handling Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Testing Module Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Where to Go From Here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

Section III: Enhancing Your UI . . . . . . . . . . . . . . . . . . . . 257


Chapter 11: Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Lottie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Animated Vector Drawables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Physics-based Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Chapter 12: MotionLayout & Motion Editor . . . . . . . . . . . . . . . 281
Getting to Know MotionLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Adding Your First Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Motion Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Adding a Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Overriding Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Animating More Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Adding Non-linear Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
ImageFilterView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Chapter 13: Custom Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Creating Custom Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Implementing a Progress Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Initializing the Paint Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

raywenderlich.com 8
Real-World Android by Tutorials

Designing the Animation Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312


Painting Your Shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Previewing Your Shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Adding Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Drawing the Check Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Putting Everything Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Manually Stopping the Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Enhancing Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Chapter 14: Style & Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Defining Styles and Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Structure of a Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Structure of a Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Style Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Theme Overlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
TextAppearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Setting Up Dark Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Styling Custom Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

Section IV: Securing Your App . . . . . . . . . . . . . . . . . . . . 356


Chapter 15: User Privacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Securing the Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Using Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Opting Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Clearing Caches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Disabling Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Disabling Screenshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Wiping Memory Securely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Where to Go From Here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

raywenderlich.com 9
Real-World Android by Tutorials

Chapter 16: Securing Data at Rest . . . . . . . . . . . . . . . . . . . . . . . . . 372


Implementing the Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Securing Data with Biometrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Customizing Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Chapter 17: Securing Data in Transit . . . . . . . . . . . . . . . . . . . . . . . 396
Understanding HTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Updating Security Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Understanding Certificate and Public Key Pinning. . . . . . . . . . . . . . . . . . 400
Using Certificate Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Preventing Information Leaks with OCSP Stapling . . . . . . . . . . . . . . . . . 407
Understanding Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
End-to-end Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Where to Go From Here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Chapter 18: App Hardening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Introducing Overflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Paying Attention to Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Sanitizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Nullability and Safety Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Checking App Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436

Section V: Maintaining Your App . . . . . . . . . . . . . . . . . 437


Chapter 19: Firebase Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Setting up Firebase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Crashlytics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Remote Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Firebase Test Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

raywenderlich.com 10
Real-World Android by Tutorials

Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460


Chapter 20: Release Optimizations . . . . . . . . . . . . . . . . . . . . . . . . 461
Using APK Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Enabling an Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
ProGuard Versus R8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Fixing Compilation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Enabling More Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
A Few Things To Keep in Mind… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Chapter 21: Advanced Debugging . . . . . . . . . . . . . . . . . . . . . . . . . 471
Memory Leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Android Studio Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Layout Inspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Chapter 22: App Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Debugging Versus Investigating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Extracting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Recovering Deleted Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Black Box Testing And Reverse-engineering . . . . . . . . . . . . . . . . . . . . . . . . 508
Using Reverse-engineering Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Debugging With ProGuard Output Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Some Final Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

raywenderlich.com 11
L Book License

By purchasing Real-World Android by Tutorials, you have the following license:

• You are allowed to use and/or modify the source code in Real-World Android by
Tutorials in as many apps as you want, with no attribution required.

• You are allowed to use and/or modify all art, images and designs that are included
in Real-World Android by Tutorials in as many apps as you want, but must include
this attribution line somewhere inside your app: “Artwork/images/designs: from
Real-World Android by Tutorials, available at www.raywenderlich.com”.

• The source code included in Real-World Android by Tutorials is for your personal
use only. You are NOT allowed to distribute or sell the source code in Real-World
Android by Tutorials without prior authorization.

• This book is for your personal use only. You are NOT allowed to reproduce or
transmit any part of this book by any means, electronic or mechanical, including
photocopying, recording, etc. without previous authorization. You may not sell
digital versions of this book or distribute them to friends, coworkers or students
without prior authorization. They need to purchase their own copies.

All materials provided with this book are provided on an “as is” basis, without
warranty of any kind, express or 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 an action of contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.

All trademarks and registered trademarks appearing in this guide are the properties
of their respective owners.

raywenderlich.com 12
Before You Begin

This section tells you a few things you need to know before you get started, such as
what hardware and software you’ll need, where to find the project files for this book
and more.

raywenderlich.com 13
i What You Need

To follow along with this book, you’ll need the following:

• Android Studio 4.1.x: Available at https://developer.android.com/studio/. This is


the environment in which you’ll develop most of the sample code in this book.

raywenderlich.com 14
ii Book Source Code &
Forums

Where to Download the Materials for This


Book
The materials for this book can be cloned or downloaded from the GitHub book
materials repository:

• https://github.com/raywenderlich/adva-materials/tree/editions/2.0

Forums
We’ve also set up an official forum for the book at https://
forums.raywenderlich.com/c/books/real-world-android-by-tutorials. This is a great
place to ask questions about the book or to submit any errors you may find.

raywenderlich.com 15
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

To Elisabeth, for your patience and support while the laptop


stole part of our time.

—Antonio Roa-Valverde

raywenderlich.com 16
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.

Antonio Roa-Valverde is an author of this book. Antonio is a


software engineer specialized in Android development. He’s
interested in innovation and shaping new products with potential
impact on people. Google Developer Group co-organizer in
Innsbruck and Munich. You can find him close to the mountains,
either hiking, riding the mountain-bike or catching some curvy
roads by motorcycle.

raywenderlich.com 17
Real-World Android by Tutorials About the Team

About the Editors


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 is a musical theater lover and a supporter of
the soccer team, S.P.A.L.

raywenderlich.com 18
v Acknowledgments

Content Development
We would like to thank Kolin Stürt for his work as an author on the previous edition
of this book.

Kolin Stürt 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 19
Section I: Developing Real
World Apps

In this section, you’ll learn how to design and implement PetSave as an example of a
professional, real world app. You’ll learn how to choose the right architectural
pattern and how to structure the code to make the app testable and maintainable.

To make your app easier to change, it’s a good practice to define different layers with
specific responsibilities. In particular, you’ll learn how to design and implement the
domain layer for the PerSave app. You’ll also see how to access the network
efficiently, by implementing the repository pattern in the data layer.

At the end of this section, you’ll have a clear idea about how to structure the code of
your app.

raywenderlich.com 20
1 Chapter 1: Introduction
By Ricardo Costeira

Ever since its inception, Android development has been known for being as
frustrating as it is fun. Ironically, both the frustration and the fun come from the
same place. Developing an Android app involves not only juggling an ever-changing
framework, but also handling interesting and challenging design decisions.

The Android framework keeps growing, and it’s not likely to stop anytime soon.
Think of the sheer amount of documentation online at the Android Developers site,
or even on Kotlin’s own documentation pages. On top of this, time and experience
have taught us that new architectures and best practices will keep appearing, giving
us new and better ways to do the same things.

This isn’t necessarily bad, though. Android developers have come a long way since
the times when having a god Activity was the standard. Over the years, through the
effort of an amazing and supporting community, multiple architectures and
techniques have surfaced, aiming to improve both app stability and developer
quality of life. Even Google has pitched in on this project with their own
recommended app architecture.

With such a large, continuously mutating ecosystem, developing even the simplest
of features can feel overwhelming for beginners — and, sometimes, even for
advanced developers.

raywenderlich.com 21
Real-World Android by Tutorials Chapter 1: Introduction

For instance, say that you want to get data from a data source and display it. Even if
you’ve implemented this in the past, you know Android. Can you be 100% sure that
the way you did it back then is still the best way to do it now?

You can’t. And that’s OK! With an ecosystem that changes so frequently, it’s
impossible for even the most experienced engineer to be aware of all its quirks and
nuances.

Android developers need to know about a wide array of subjects to build apps
effectively. That’s where this book comes in.

What Is This Book About?


This book covers a lot of topics. It shows you how to:

• Build features in a scalable way, covering each architectural layer.

• Modularize your app, if you ever need to, while touching on dynamic features.

• Handle animations, styles, themes and other elements of the UI.

• Make your app more secure.

• Maintain and care for your app.

It does all of this by showing you practical solutions that lead to the desired results.
Yet, the idea here is much more than that. Of course, you can study the
implementation details if you want, but that’s not the main point. Instead of
reasoning about how to write code, you should instead focus on why you write it that
way.

The goal is for you to go beyond the code and understand why you’re writing it in
the first place. This book doesn’t simply cover the basics at the problem’s surface or
give high-level descriptions of the simplest use cases. Rather, it aims to share with
you some of the knowledge that developers acquire over time, while developing real-
world apps.

You’ll see how to solve real-world problems following real-world best practices and
techniques. Sometimes, these best practices and techniques are general rules of
thumb regarding software development and design principles. In other cases, they’re
tightly coupled to the tools used to fix the problem. You can only acquire them by
either getting your hands dirty with those tools or by having someone point them
out for you.

raywenderlich.com 22
Real-World Android by Tutorials Chapter 1: Introduction

The book focuses not only on why should you do things a specific way, but also on
the corresponding advantages, trade-offs and drawbacks of that solution. It makes it
clear when a certain decision is not ideal, explaining the reasoning behind discarding
a choice or going through with it anyway.

As you go through the content, you might notice that some of it is opinionated; you
might even disagree with some of the statements. That’s good! As a developer, it’s
only natural for you to question other developers’ decisions. You might even see
some problems that others completely missed.

On the other hand, if you agree with everything and don’t see any problems, that’s
fine too! What’s important here is for you to go through the roller coaster ride that is
Android, with all its ups and downs, just like developers do in the real world.

Who Is This Book for?


This book is useful for developers of all levels. Some of the content assumes prior
knowledge of complex topics, like RxJava and Dagger/Hilt, and is more tailored to
intermediate or advanced developers. But there’s plenty of content suitable for less-
experienced developers as well.

Note: If you need to fill some gaps in your knowledge,


www.raywenderlich.com, provides a variety of books to help. If you need to
learn Kotlin, Kotlin Apprentice (https://www.raywenderlich.com/books/kotlin-
apprentice), is the right book for you. With Reactive Programming with Kotlin
(https://www.raywenderlich.com/books/reactive-programming-with-kotlin),
you can learn all about RxJava and RxKotlin.

Dependency injection is a fundamental tool for simplifying the architecture of


your app and making it more testable and maintainable. If you need to learn
more, Dagger by Tutorials (https://www.raywenderlich.com/books/dagger-by-
tutorials), is a must. Finally, good, professional apps must implement different
types of tests, from unit tests to end-to-end tests. To learn more about how to
do this, Android Test-Driven Development by Tutorials (https://
www.raywenderlich.com/books/android-test-driven-development-by-
tutorials), is the book for you.

raywenderlich.com 23
Real-World Android by Tutorials Chapter 1: Introduction

As mentioned already, a large part of the book’s focus is around the reasons why
developers prefer specific solutions. This usually boils down to software design
principles, architectural considerations or tidbits of wisdom acquired through
experience. In these cases, everyone can benefit from sharing information.

The Sample Project


Throughout this book, you’ll work on building and improving a sample project called
PetSave. PetSave is a pet adoption and fostering app that aggregates and matches
animals available to you. It uses the medical history of the pet along with some of
your personal information for matching and searching.

Figure 1.1 — PetSave Screen With a List of Animals


When you start, the app will only show a blank screen. Even the screen in the image
above doesn’t exist yet. It’ll be up to you to develop the app from the ground up.

The app connects to an external service, which it uses to fetch the animals. This
service, Petfinder, is an online, searchable database of animals who need homes.
They also have information on shelters and adoption organizations across the United
States. Check out their site at https://www.petfinder.com/. Who knows, you might
find your new pet. :]

raywenderlich.com 24
Real-World Android by Tutorials Chapter 1: Introduction

Signing Up for an API Key


Petfinder allows you to access their database, provided that you sign up for one of
their API keys. To do so, you first need to go to their site and sign up for an account.
After that, head to https://www.petfinder.com/developers/, click the GET AN API
KEY button and follow the process from there.

After acquiring an API key and its corresponding secret, you’ll need to set them for
each chapter’s starter project. You’ll set them in ApiConstants.kt, which is in the
common.data.api package.

Figure 1.2 — Replace Those Two Strings With Your API Key and Secret
If you’re curious about Petfinder’s API, check out its documentation at https://
www.petfinder.com/developers/v2/docs/.

Where to Go From Here?


Now that you have an idea of what this book is about and how it’ll unfold, you’re
ready to start building PetSave. As you already know, the app is at a very early stage,
with very little code written. However, you won’t write any code in the next chapter.

Thinking about a problem, getting familiar with it and planning ahead just enough
that you have a solid starting point is always a better option than starting to write
code right away and hoping for the best. So in the next chapter, you’ll lay the
groundwork to develop a robust and scalable app.

raywenderlich.com 25
2 Chapter 2: Starting From
the Beginning
By Ricardo Costeira

Android development can be both straightforward and extremely complex. Not only
does the framework keep growing at a ridiculously fast pace, but it also repeatedly
reinvents itself — think asynchronous programming, lifecycle and state management
and even animations. It’s common to feel overwhelmed by or even lost amidst all the
continuous library releases, shiny new features and multiple ways of achieving the
same goals.

One section of this book (or, truth be told, the whole book!) wouldn’t be enough to
cover everything the Android framework has to offer. However, as you develop real-
world apps, you start to notice that apps gravitate around a few common ways to use
the framework. Additionally, many design decisions and best practices can be
universally applied to produce better software.

In this chapter, you’ll read about some of these design decisions and practices that
you can follow in the early development stages. They’ll allow you to build a solid
foundation for your app while avoiding over-engineering.

More specifically, you’ll learn:

• How to structure and organize your app so you can tell what it does just by looking
at the package names.

• Why it’s essential to keep high cohesion and low coupling.

• How to produce better apps by investing in upfront planning.

• Why you should use a layered architecture.

Buckle up!

raywenderlich.com 26
Real-World Android by Tutorials Chapter 2: Starting From the Beginning

Package by Feature Approach


First, check out the code you’ll work with by opening the starter project in the
material for this chapter, and examining its contents. Expand the
com.realworld.android.petsave package. Did you hear that? That was the project
screaming its purpose at you!

Figure 2.1 — Screaming Architecture


This project is organized in a package by feature structure. Everything that’s
related to a feature, and only to that feature, is stored inside the same package. Code
shared by two or more features is stored in separate common packages. This type of
package organization has a few advantages:

1. Just by looking at the package structure, you easily get a feeling for what the app
does. Some people also like to call this a screaming architecture — hence the
awful “screaming” joke attempt earlier.

2. You end up with packages that not only have high cohesion, they’re also either
loosely coupled or completely decoupled from one another. Cohesion and
coupling are two very important metrics in software development that you
should always consider.

Figure 2.2 — Not Screaming vs. Screaming Architecture

raywenderlich.com 27
Real-World Android by Tutorials Chapter 2: Starting From the Beginning

High Cohesion
Cohesion refers to the relationship between different programming elements. The
stronger the connection between code inside a component, the more cohesive that
component is.

For instance, imagine you have a class that’s responsible for applying a cute filter to
a cat’s picture, called CatFilter.

class CatFilter(private val picture: Picture) {

// Properties related to filter and picture state.


// ...

private fun parsePixels() {


// Store individual pixels and the relationships between
them.
}

private fun filterPixels() {


// Apply the filter to each pixel.
}

private fun smoothenResult() {


// Apply picture smoothing techniques.
}

fun apply(): Picture {


// Use methods above.
}

// Other methods.
}

The methods and properties of this class are all closely related to each other, which
means that the class is highly cohesive.

Now, imagine the case where you start adding more responsibilities to the class. Not
only does CatFilter apply a filter, but now it also saves and loads the result with
the help of the file system. You’ll start having elements in the class that have
nothing to do with each other — parsePixels() and save(picture: Picture)
have completely different purposes.

In other words, your class will now have a lower degree of cohesion.

raywenderlich.com 28
Real-World Android by Tutorials Chapter 2: Starting From the Beginning

Low Coupling
Coupling has to do with dependencies between programming elements. Continuing
from the previous example, say you move the I/O logic to another class called
CatPictureFileSaver.

class CatPictureFileSaver {

fun save(picture: Picture) {


// File writing code.
// Calls compression and encoding methods.
}

fun load(picturePath: String): Picture {


// File reading code.
// Calls decompression and decoding methods.
}

private fun compress(picture: Picture): CompressedPicture {


// Fancy compression algorithm.
}

private fun encode(


compressedPicture: CompressedPicture
): ByteArray {
// Byte encoding.
}

// Other methods.
}

This new class also has methods to compress/decompress and encode/decode the
image, which are strongly related to its purpose. Nice, now CatFilter and
CatPictureFileSaver are two highly cohesive classes!

After some time, requirements change. You now have to cache the intermediate
results of the filtering. To implement this, you call the persistence methods of
CatPictureFileSaver directly in a few different places in CatFilter.

This may seem like the logical way to accomplish your goals but, by doing so, you’re
forcing CatFilter to be tightly coupled with CatPictureFileSaver. Consider a
scenario where a requirement change dictates that you drastically change or even
remove CatPictureFileSaver. Due to the coupled nature of the classes, you’d have
to make significant changes to CatFilter as well.

raywenderlich.com 29
Real-World Android by Tutorials Chapter 2: Starting From the Beginning

On the other hand, if you have something like a CatPictureSaver interface that
CatPictureFileSaver extends, and have CatFilter depend on it, then the classes
would be loosely coupled. Changes to CatPictureFileSaver that don’t affect this
interface would likely not affect CatFilter at all.

interface CatPictureSaver {
fun save(picture: Picture)
fun load(picturePath: String): Picture
}

class CatPictureFileSaver : CatPictureSaver {


// Interface overrides and private methods/properties.
}

class CatFilter(
private val picture: Picture,
private val pictureSaver: CatPictureSaver
) {

// Code...

private fun filterPixels() {


// ...
// CatFilter knows nothing about save method's inner
// workings!
pictureSaver.save(filteredPicture)
// ...
}

// More code...
}

The interface would ideally use generic naming to keep your implementation options
open. For example, a CatPictureSaver interface with a method named
savePictureToFile() would be a bad choice. You’d have to change the method
name if you stop using the file system to save pictures!

raywenderlich.com 30
Another Random Document on
Scribd Without Any Related Topics
“I find that the odor from a good cigar is rather attractive. At any
rate, Joe has done so much for me that I can afford to give in to one
little vice of his. To think of the poor dear fellow hiding himself away
like that. It makes me almost cry to think how miserably cruel I
have been to him. But I mean to make it up to Joe in the future, Mr.
Darrell.”
Eric swallowed a lump that seemed to be sticking in his throat.
This, the woman whom he suspected of being false to her husband
—he did not know how it was, but whenever he came into her
presence he seemed to be in some way charmed.
She was a siren.
The same power, exercised by the nymphs of the sea in olden days,
causing the sailors to jump over to their death, is given to certain of
the gentler sex to-day.
Adam sunk all his manhood and forgot his duty to his Maker when
tempted by Eve, and from that day to this few men there are strong
enough to do the right when a beautiful woman smiles upon them
and teaches them the lesson of love.
It would be impossible to describe the influence Lillian had upon
nearly all who came in contact with her—her manner was soothing
and pleasant, so that general admiration followed her acquaintance.
Darrell was a man of strong purposes and he put down with a firm
hand any feeling that interfered with his stern sense of duty.
In a business way he was here to see whether Lillian was what she
appeared to be, or deceptive by nature.
Hence he was not to be charmed from his purpose in any way.
The human feeling of admiration must give way to the professional
energy.
“You spoke of several reasons for calling to see me, Mr. Darrell—will
you kindly state what the others are?” she asked.
“With pleasure, and I trust you will not feel offended, my dear Mrs.
Leslie. It is a custom on my part in a case like this, to take from the
party with whom I have been engaged, a little note, stating that
they have been well satisfied with my services. I hope you may not
think it out of the way and give me this.”
She appeared troubled.
“I do not know that I ought to—such an affair is essentially private.”
“I only desire the paper for my own satisfaction, and not to show.
You can merely state that you are entirely satisfied with the services
of Eric Darrell, and if you prefer, simply sign your initials.”
Crafty man—the initial was what he wanted above all else.
Her face brightened.
“I do not know that I would object to that, Mr. Darrell, since my
identity is concealed. Do you want it now?”
“If you please,” humbly, but secretly exulting over his success.
She went to the desk, opened it and sat down—after a minute’s
thought she wrote something upon a sheet of paper.
“Will that do, Mr. Darrell?” handing it to him.
He glanced at it and read:

“This is to certify that Mr. Eric Darrell has accomplished the work
for which I have employed him, in a thoroughly satisfactory
manner, and that I am well satisfied with his services.
L. L.
“New York, October 7, 188–”
Darrell smiled.
“A thousand thanks, madam.”
“It answers your purpose?” quietly.
“Yes, yes.”
Although he smiled Eric Darrell felt as though he could have wept
just then.
The one glance he had taken had revealed the fact that the capital L
made by Mrs. Leslie was very similar to the one which he had seen
signed to the note Paul Prescott had let fall.
It was a shock to the detective, even though he had in a measure
expected it.
That point gained he put the matter aside for the present and
continued to appear pleasant, though it was only with an effort he
could do so.
For a little he chatted with the lady, and endeavored to study her.
Darrell thought that if his suspicions were proven true, Lillian Leslie
must be the perfection of an actress—he had never seen two such
extremes meet in an individual—she was the incarnation of good
and evil.
“By the way, are you acquainted with a Mrs. Collingwood of your
street here?” he asked after a while, in a careless tone.
“Yes, I know her.”
She looked surprised, as though wondering where he could have
met her.
“Last night I made her acquaintance. She is accounted a rather
handsome woman, I believe.”
This was put out as a sort of feeler, for he had not even seen her
face.
Lillian answered in a manner that declared what little interest she
had in the lady:
“I believe so, but we were never friends, and I do not know much
about the lady.”
That ended it.
Darrell soon took his leave, having gained the point for which he had
come.
When he entered a car on the elevated road he found a corner to
himself, and then, unable to wait longer, proceeded to compare the
two notes.
Just as he thought, the writing was of the same order, and there was
much resemblance in the capital letters.
Still, Eric had seen enough to know that only an expert could decide
this question beyond all cavil.
Before now he had seen the chirography of two persons resemble
each other, and this was not to be accepted as conclusive evidence.
At the same time it was a point that would bear upon the final
result.
He kept it in mind.
Other threads must now be taken up in turn, until the main current
was reached which would sweep him on to the sea.
He put away the document just received from Mrs. Leslie.
While still looking at the other, some one sat down beside him.
Darrell’s thoughts were fixed upon the subject which occupied his
attention, and he did not even know there was some one in the next
seat until a hand clutched his arm.
“Where did you get that paper?” said a hoarse voice close by his ear.
Turning his head at this he was surprised to see Paul Prescott beside
him.
Fortune plays some queer tricks at times, and this was one of them.
What an odd chance that this man, of all in the great city, should sit
down in the same car, at the very moment Darrell had that fatal
paper in his hand.
The circumstances were indeed so singular that Eric could not but
start; but his excellent control over his nerves stood him in good
stead again.
He looked in the artist’s face—it was flushed and eager and angry—
evidently he had not missed the letter up to now.
“My dear sir, does it belong to you?” asked the detective, quietly.
“It does, sir.”
“Then take your property.”
“Very good, sir; but I have a right to ask, yes, demand of you, to
explain under what peculiar circumstances this document chanced to
come into your possession.”
“I do not question your right to ask that, and I shall readily tell
where and when I picked the letter up.”
So he gave the time and place to a dot, but did not say anything
about having seen it drop from the pocket of the owner. Prescott
remembered that this was when and where he had received the
message, and he had no doubt of the truth of the story.
At the same time he looked at the man by his side with a frown.
“You read this?” he asked.
“Naturally so—you could not blame me.”
“And had your curiosity aroused?”
“Well, yes, but that has nothing to do with it. Let us forget the
circumstance.”
“Willingly, since it concerns the private affairs of a very dear friend.”
No more was said.
Eric read his paper and the artist seemed occupied with his, but
every now and then he turned his eyes toward the detective as
though his curiosity was aroused.
When the artist arose at Fourteenth Street, to leave the car, Eric
handed him a card he had prepared for such occasions.
It gave his name and the address of his apartments—nothing more.
“If you should ever desire to see me, sir, you will find me there by
letter or person,” he said quietly.
At this Prescott smiled broadly.
“I hope you don’t think I dream of sending you a challenge for
finding my letter,” he said.
“Well, you looked as black as a thunder cloud, and I didn’t know but
what you might be meditating something of the sort.”
“It was rude in me to act that way, and I beg your pardon for it.”
Frankly said.
The detective liked him better for it, and there was something about
the other’s face quite attractive after all.
Somehow Eric did not seem to hate and despise him as he had done
before.
When the artist had gone he fell into a fit of musing again.
Various theories were built up, only to be discarded again as unequal
to the occasion.
He remembered that the letter had been given to Prescott by a
woman, who was evidently in the pay of the party signed L.
Whom could Lillian send?
He did not know the internal arrangements of the lovely little house
up town, and this was what was now on his mind.
The office of Joe Leslie was his destination, and he made his way
thither after leaving the elevated road.
Leslie was in John Street, and carried on a business in precious
stones, of which he was one of the best judges in the city.
He was a man of considerable wealth, but being of an
unostentatious nature he did not put on much style, preferring to
live quietly and well.
When Eric Darrell ascended the stairs of the large building in which
his friend had his offices, he was forcibly reminded of times gone by
when Joe was a bachelor, and the two had been warm friends,
passing through numerous scenes of pleasure in company.
CHAPTER XV
COMPARING NOTES

Joe was in.


When the detective sent his card by the same clerk whom he had
helped Joe rescue from the clutches of the gamblers, he was shown
to the private office.
Here he found his friend seated at his desk, and busily engaged with
a pile of letters that had come in the morning mail.
Joe greeted him cordially and begged him to wait a few minutes,
when he would be through with his task.
This suited the detective exactly, since it gave him a chance to watch
the other.
He was able to read a face pretty thoroughly and he saw very
quickly that Joe was still greatly troubled in his mind.
He had gotten rid of one burden, but another had come in its stead.
What could it mean?
There was little need for Eric to ask that.
He knew, beyond all doubt, that the old suspicions had arisen again
in Joe’s mind, perhaps strengthened by some circumstance.
Finally Joe leaned back in his chair and looked at his visitor.
“Eric, for a short time last night I was really and truly happy—it
seemed to me that the clouds had all rolled by. Then, by a strange
circumstance, they were brought forward again and now I am worse
off than ever.”
“That is too bad—I am sorry for it.”
“Before, it was my foolish habit that gave me sorrow—now it is a
dreadful thought that I endeavor to banish from my mind, but which
rises up again and again in all its hideous deformity until I almost
feel as though I am mad.
“Still I keep my thoughts clear, for I know that this awful question
must be grappled with, and fought to the death.
“My whole future is concerned in it, and I mean to lay the ghost
forever, or else know the very worst.”
“Spoken like a true man, Joe. Shake hands on that as a bargain. I
am, in this matter, hoping to prove your wife’s innocence, but I shall
take up any evidence that comes along, and apply it where it
belongs.”
“I want you to, old fellow, no matter what the pain it brings.”
“To begin with, you know all my hopes are to the end that Lillian
may prove to be as innocent as a babe.”
“Heaven grant it,” Joe groaned.
His tone betokened despair.
The detective judged from this that his friend must have made some
discovery since last they met.
“At the same time, Joe, you know as it looks at present, things are
decidedly against your wife.”
“I try to deceive myself, Eric, into the belief that it is not so, but I
cannot, I cannot. She shall have a fair trial—I will give her the
advantage of every doubt, and then—”
He could not finish the sentence.
Poor fellow! how Eric pitied him, and in that moment, believing
Lillian guilty, cursed the hour she ever crossed Joe’s path, to blight a
life devoted to her.
Never mind—the end was not yet.
“Tell me what you have discovered, Eric,” said Leslie, throwing off
the terrible feeling that almost overcame him.
“After you, my boy.”
“How do you know I have anything to tell?” in a surprised tone.
“Your looks give it away. Proceed.”
So, being encouraged to speak, Joe gave a brief account of the
charming scene that had taken place in his home on the preceding
night.
Eric was quite interested, and his reflections upon the little woman
were flattering to her.
Then came the climax.
The note with the charred edge was produced, and submitted to
examination.
Eric looked upon it as furnishing quite a link in the chain of evidence
—he believed the secret referred to must indeed mean that which
they were looking up.
When Joe heard the opinion of his friend, he was not much
encouraged—indeed, his spirits were reduced to a lower ebb; but he
shut his teeth and said nothing.
“Now I want your opinion, Joe, as a man of some legal acumen. It
is a little question I desire to have settled,” he said.
With that he took out the document he had received from Lillian.
“You recognize the handwriting?”
“I do—it is my wife’s,” with a shiver.
“I had another paper, but gave it up to the owner; but, as it
fortunately turned out, ere doing so I traced the signature with a
piece of tracing paper—see, here it is.”
He put the two together.
“Bend over, Joe.”
“What do you want me to decide?” nervously, as might a man who
feared lest his words might convict one he loved.
“Examine these signatures.”
“I am doing so.”
“If asked your opinion frankly as an outsider, would you incline to
the belief that one hand had made both of these?”
“You insist on a reply?”
“I think you had better give it.”
“Then, according to my humble opinion, those letters are alike, and
the chances are decidedly in favor of the same party having made
both.”
“My opinion exactly—I would not swear to it but I put the chances in
that way.”
Then he turned the paper over to Joe.
“What of this?” asked that worthy.
“There is nothing out of the way in it. I only had your wife write it
out and sign her initials in order to compare it with the other
document.”
“Tell me about the other.”
“I will reproduce it, word for word.”
Drawing some paper to him he wrote rapidly for a few minutes.
Then he placed the duplicate of Prescott’s letter in front of Joe.
“This looks like a deep conspiracy, Eric—you say this was signed that
way?”
“Yes.”
“Was it—in—her handwriting?”
“There were enough points of resemblance to make it striking.”
“Heavens!” and he beat his fist against his forehead in despair—then
recovered his calmness after an effort.
“You say you have lost this document?”
“I had compared them in the elevated coming down here, and was
still looking at that one when a hand was laid on my arm and a man
asked me what I was doing with his property. I saw he was the
owner and gave it over—we had a few words and separated.”
“Was that man known to me?”
“By sight, yes.”
Joe shuddered visibly, as though he understood the suggestive
words of the other.
“Then it was he?”
“Paul Prescott, the artist.”
“Curses on him for a meddler! Lillian has a weakness for art, and I
have often jokingly told her she should have married a painter.”
“That explains his power in a measure—he has fed her on art and
won her regard by posing as a hero.”
Joe struck the duplicate paper fiercely.
“Eric, you may think me crazy to doubt it, but unless Lillian declares
in my presence that this is the product of her pen I will never believe
it.”
“Joe, my friend, I honor you for such a feeling, and I hope as I
never hoped before in my life that this thing will prove a false
alarm. All the same I shall do my duty by you every time, as a true
friend.”
“A thousand thanks. I feel fifty years old to-day instead of thirty-six
—it is my birthday, you know, Eric,” with a sad smile.
“I wish you many happy returns, my dear fellow—just three years
younger than I am. I wish I had a gift to give you.”
“The best gift this world could give me would be the proof that my
wife is the true and faithful wife I have always believed her. Great
heavens! Eric, when I think of it all, a spasm comes over me—my
fingers twitch as though they would love to encircle the throat of
that arch-devil and choke his life out.”
Eric was surprised.
He had not believed this of Joe, looking upon the other as a sort of
good-natured giant whom any one could impose upon. Now he saw
him angry he made up his mind that if ever Paul Prescott and Joe
came in contact it would go hard with the artist.
“Well, I declare, you will make a modern Othello yet, Joe.”
“No, no,” with a shudder, “I might kill him, but I would never raise a
finger against her if she deceived me time and again. I couldn’t; I
must love her always.”
Eric shrugged his shoulders.
“Every man to his taste. Your character is one in a thousand, Joe.
As for me I confess I have more of the tiger about me, and if a man
or woman foully wrongs me I look forward with pleasure to
revenge.”
“Don’t let us talk about it—the worst I would do to her would be to
seek a separation—but for him,” and his face grew grandly dark and
gloomy, but he did not finish.
“I wish to ask you a few questions about your home, Joe.”
“Do so.”
“In the first place you have a girl.”
“Yes, two of them.”
“What positions do they occupy?”
“One is in the kitchen—the other a sort of upstairs girl, to take care
of the rooms, answer the door, and wait upon the table.”
“Describe the cook.”
Joe laughed.
“She is as fat as she is long, almost, and as good-natured as she is
fat.”
Eric made a gesture.
“That point is settled. Now the other.”
“Nanny is a woman too, but much smaller, and ladylike in her ways.
She came here from Chicago with us.”
“Ah! a favorite of your wife’s?”
“Yes. Nanny was in her mother’s employ as a girl. She is faithful to
us.”
“Ahem! Just the person, in fact, to be entrusted with a message of
a secret nature, that must be handed to a certain party?”
At this Joe turned red and white by turns. “I presume so,” he
admitted, uneasily.
“Is Nanny about my height, rather slim, and quick in her actions?”
“Yes.”
“Dresses in black?”
“My soul! man, you seem to drive the nails into my coffin with each
question.”
“Answer, Joe.”
“She does dress in black—most maids do in New York now.”
“Wear a white apron?”
“No,” with a gleam of hope.
“Neither did this girl. I knew she was a maid by the courtesy she
made when handing Prescott the letter, and also from the little white
cap she wore.”
Poor Joe’s last chance seemed gone—the other had knocked away
the pins upon which his house was built.
“That was probably Nanny, but I can not and will not believe Lillian
wrote that note. Some other party had hired Nanny to give it to that
man.”
Darrell knew Joe was hugging a phantom to his heart, but he could
not take pleasure in arguing with the deceived husband—besides,
Joe’s actions proved that he believed more than he would admit
either to himself or his confidential adviser, and if the blow did come
it would not be such a terrible shock as if he had received no
warning.
The end was not far away.
CHAPTER XVI
THE LOCKED SARATOGA

The detective was not yet through.


He wished to find out a few other things connected with the case,
through information which Joe alone could give.
When the latter had told his story concerning what had happened at
his house on the preceding night, he had touched lightly on the
incident of the closet.
The keen detective had however made a mental note of the
circumstance, and he was bound to know more of the matter.
“What sort of a closet is it?” he asked.
“Quite a roomy affair.”
“You keep what there?”
“A number of odds and ends, and I believe a large Saratoga trunk.”
“Ah! your wife’s?”
“She brought it from Chicago. On our little trips to Boston and
Washington we used my leather one.”
“Then this trunk has been there all the while?”
“Yes.”
“Think now—have you ever known Lillian to enter that closet for
anything since she came to you?”
Joe turned white.
“She might have done so dozens of times.”
“But have you known her to?”
“I have not.”
“Is there anything kept there she would want?”
“Not that I know of.”
“Under the circumstances does it not strike you as singular that she
should not only enter the closet but lock it and take the key
upstairs?”
“Eric, I have thought so myself,” sadly.
“Now, Joe, you are withholding something from me that has a
bearing on this matter.”
“How do you know it?” quickly.
“Well, perhaps a little bird told me, or else I read the secret in your
face. At any rate you have no business to keep it from me, I am
trying to do my duty—heaven knows if I could I would have your
wife as spotless as the noonday sky, and if she proves otherwise I
shall lose faith in all womankind forever; but I must be able to weigh
every particle of evidence for and against her.”
“I beg your pardon, Eric, but I felt so badly over the circumstance
that I hardly had the heart to relate it to you.”
“Then it is against her?”
“I am afraid so.”
“The sky grows very black—poor Joe—poor Lillian—my heart is in
sympathy with you.”
Joe had buried his face in his hands and seemed quite overcome.
The detective waited.
When his friend had in a measure recovered from the shock, he
spoke.
“Now tell me the circumstances.”
Joe’s voice was a little unsteady, but it gained strength as he
proceeded:
“When I came down this morning it was late for me, but I had not
slept well, and felt a raging headache.
“Lillian was in the library, and left me to go down to see if she could
not have a cup of strong tea made, which always acts as a sedative
with me when I have a headache.
“My thoughts had never gone from that closet and I had already
seen that the key was in the door for Lillian had brought it down.
“Hardly had she left the room than I was over there and had the
door open.
“I examined the interior but found it all as I had been in the habit of
seeing it.
“This surprised me. Could I after all have done her an injustice with
my suspicions?
“I was beginning to think so, when I suddenly noticed a little thing.
“The key of her Saratoga trunk was missing.
“I remembered seeing it in the trunk a few weeks before.
“Why should Lillian take it?
“Instinctively I tried the lid—it was fast—the trunk was locked.
“I left the closet, and was sitting in the library when the breakfast
bell rang, and Lillian came in to go down with me.
“She was full of spirits, while I felt as though I were about to attend
my own funeral.
“During the progress of the meal I spoke about there being a chance
of our soon going out to Chicago to pay a visit, and she seemed to
be very quiet over it, unusually so, I believed.
“‘By the way, I miss the key of your trunk—will you let me see if the
interior is in good condition?’ I said as steadily as I could, although I
felt my face turn red.
“She looked at me as though surprised.
“To-morrow you can do so, Joe—to-day you are in poor condition for
anything. Take your mind off everything that excites it. I wish you
would stay at home to-day and nurse your headache.’
“I professed to have business of unusual importance down town,
and shortly after left the house for my office.
“Now, Eric, give me your honest opinion—my mind is hardly in a fit
condition to see and judge for myself.”
The detective had listened intently.
He could grasp the threads and draw them into one compact cord.
The issue was before him.
“Joe, it is beyond all question that her secret lies in that trunk—if we
knew what it contains, nothing more would be needed.”
“Yes, I suppose so,” rather wearily.
“From the tenor of the letter Prescott received I am inclined to
believe some one is about to run away with him, or he with her,
rather.”
Joe groaned dismally.
“If in that trunk I should find some of her dresses and jewelry—well,
I should be strongly inclined to believe it meant flight.”
“No, no,” hoarsely, clutching his throbbing brow with both hands,
“not that. Lillian would never be guilty of that. She may have flirted
with the man—women are weak, I know—but that is the worst I will
believe of her—the worst.”
Darrell shrugged his shoulders.
“Very good, Joe, but you must permit me to place my own
estimation on things. My eyes are not blinded by love—I can weigh
things calmly, and place their right estimation upon them.”
“Eric, I said I would leave it all with you and I do, but until it is
proven beyond all doubt, do not ask me to believe in her guilt. It
will kill me if it is so.”
“Trust in me, Joe, old fellow, I will act for you as though you were
my brother.”
“And—whatever comes, Eric—be gentle with Lillian—let me be the
one to—oh! my God! I cannot believe it, and yet it seems as though
a burning iron were branding it on my brain.”
The detective was done for the present.
From Joe’s offices he went to his own.
Here he could sit down and review the situation in regular order.
Darrell generally made notes of his subject, so that he might ever
keep the circumstances before his mind.
He now jotted down a few more headings, and then surveyed the
case as seen through these spectacles which he had drawn on.
Looking over his shoulder we can also get a resume of the case by
reading what he filled up a page in his note book with.
They were arranged under heads in numerical order, beginning at
the start:
1—Paul Prescott, an artist, makes daily visits to Joe’s house when
Joe is down town.
2—Lillian Leslie has a secret from her husband.
3—The paper dropped by Prescott is in her writing, and seems to
promise an elopement. It is also signed L, her initial.
4—The girl who gave Prescott the letter corresponds with Lillian’s
faithful maid, who has been in the family for many years.
5—The fact of her having the closet key upstairs is significant in
itself.
6—Her trunk is locked and the key gone—she says she will produce
it when Joe has leisure to examine the trunk—there is no hurry—the
morrow will do—evidently something is to occur between now and
to-morrow.

This was the indictment.


Against it, on the opposite page, he had written the defense—it
came under one head:
“1—Lillian is my ideal of womanly perfection—if she prove guilty my
faith is gone forever. I have never yet been able to believe her guilty
while in her presence—it is only when away that these terrible facts
make me fear it is so.”
A peculiar case this. If Lillian could plead her own cause, she would
undoubtedly win it.
For a long time Eric Darrell sat and looked at his notes.
They covered about all of the case.
He could not but see how overwhelming the evidence was against
Lillian and how meager her defense.
Still he kept hoping for the best, trusting that something would turn
up to send the balance over to the other side. Had it been any one
other than Lillian, the detective must have declared that there could
be no hope—the case would be virtually closed. With such a client,
however, he had hope to the end, because all his sympathies were
enlisted in behalf of Joe and his wife.
He was not the man to waste time in useless speculation, and when
he had calmly reviewed the situation, he made up his mind what
ought to be done.
Would it be possible to save Lillian even though she were guilty?
He could not face her—his first thought had been to see her and
speak of the terrible nature of the indictment hanging over her like
the sword of Damocles, suspended by a single hair—perhaps she
was influenced by some strange power the artist possessed—
mesmerized, made a slave by some peculiar phase in a powerful
organization—Eric had known of such things, although he did not
pretend to understand them.
When he came to think it over, however, he concluded that he could
not muster up courage enough to say these things to her face.
He was certain that, strong-nerved man as he was, he would utterly
fail when he sat opposite those eyes, and felt them upon him.
Was there any other source to which he might apply?
He ran over the field.
What of Paul Prescott?
The thought seemed absurd at first but presently he began to realize
that there was a chance back of it.
The man was a character and might not be as bad as appearances
indicated.
Perhaps moral suasion might influence him, and in case that failed a
threat would possibly have the desired effect.
The more he thought over the matter the better he looked upon the
idea.
At last he determined to try it.
There could be no harm done.
At the same time he had a chance to accomplish a great work.
A new thought had entered Eric’s head.
Even if Lillian was guilty he might through some work, skillfully
arranged, so manage it that the disturbing element should be
removed, and their lives flow on smoothly again.
This was his highest hope.
That he would find Lillian innocent had ceased to enter into his
calculations.
He only hoped for a half way victory. It was noon when he went
out, and stopping in a restaurant he had dinner. His plan was
arranged.
If he could effect a meeting with the artist, the worst would soon be
known, and he would also discover what sort of man Prescott was.
He knew where the latter had his studio, and presently was bound
for Fourteenth Street to interview the artist. What would come of
that interview no one could tell, but Eric hoped for a favorable issue.
At any rate he did not think his case would be destroyed by what he
was now about to undertake.
At half past one he entered the building where Paul Prescott had his
studio.
A few minutes later he stood at the door and gave a loud knock.
CHAPTER XVII
THE ARTIST IS DEFIANT

“Come in!” said a voice.


Eric opened the door.
An odor of tobacco greeted him.
Prescott, in his studio dress, was before a painting, putting some
touches here and there.
So interested was he that he did not turn his head when the door
closed.
Darrell looked at the painting and was charmed—it was a glimpse of
the Delaware Water Gap, and so true to nature that one could
almost believe he was on the spot.
Finally the artist stepped back a pace. “There! that is done. I beg
your pardon—” and he wheeled around.
As he saw who his visitor was he uttered an exclamation of surprise.
“Ah! you, Mr.—Mr.—”
“Darrell.”
“To what do I owe the pleasure of this visit, Mr. Darrell?”
Evidently he was inclined to be a little suspicious of the man who
had had that letter in his possession.
At the same time his curiosity was aroused. Eric saw this. He was
ready to take advantage of it.
Coolly seating himself he said:
“Mr. Prescott, I have called to see you in reference to that paper
which you received from my hands this morning.”
“Ah! indeed,” with a frown.
Not at all dismayed the detective went on: “I believe you claimed it
as your property?”
“I did—have you any reason to doubt my word?”
“Not at all, sir. If it was your property, then the letter must have
been addressed to you.”
“It was.”
“Mr. Prescott, you are looked upon in society as an honorable man—
your name has never yet been tarnished. As a friend I beg of you to
pause ere you cross the Rubicon.”
“What’s all this about? It seems to me you are interesting yourself
in a business that does not concern you in the least,” coldly.
“That is where you are mistaken, sir—it does interest me greatly.”
“In what way?”
“I know the lady who wrote that note.”
The artist shrugged his shoulders.
“Well?”
Eric was somewhat surprised.
He had expected that the man would show signs of consternation.
On the contrary he maintained his self-possession, and even smiled.
“You contemplate a step that is bound to bring trouble.”
“It is not my fault,” with a sneer; “some people are so wrapped up in
themselves that they can see no one else. This lady—her name shall
not be mentioned, as I would not have it the subject of a quarrel—
prefers my company to that in which circumstances have thrown
her. She is restrained of her liberty, and I would give it to her. That
is all. Through the interference of some interloper, such as yourself,
we may be prevented from carrying our immediate plans into
execution, but the postponement can only be temporary. We must
triumph!”
Eric was more than ever amazed.
This man did not appear shamefaced—he even gloried in his foul
work.
Surely this was the acme of villainy.
How was he to meet it?
Could he cow the artist?
Already he had made up his mind that this was impossible, for the
man seemed to be as daring as he was bad.
What then?
There was nothing left but to let the game take its course.
If Joe and this man ever came into personal contact there would be
trouble, for the artist looked like a man who would back up his acts
with blows.
“You refuse to change your plans, then?”
“Most decidedly.”
“Well, you may rue it ere long.”
“See here, what makes it your business—there was no name
attached to this note—how do you know who wrote it—what in the
devil have you got to do with it, anyhow, and what is to hinder me
from giving you a sound thrashing on account of your confounded
impudence in the affair?”
His manner was threatening.
Eric did not wince.
“Mr. Prescott, listen to me, I am a man not acquainted with fear, nor
do I descend to fisticuffs. You see I am armed—now you can keep
your distance and talk reason or else take the consequences.”
At sight of the revolver the artist started.
He seemed to suspect for the first time what manner of man he was
dealing with.
“Are you a—detective?”
“I am, sir!”
“In his employ?”
“Mr.—the gentleman referred to is an old friend of mine. I have
sworn to see him through this trouble.”
“Were you following me when this paper fell?”
“Yes.”
“You saw it drop?”
“I did.”
“And the maid who gave it—perhaps you followed her home.”
“No matter—I believe I know all there is to be known of this affair,
sir. I am here to advise you to drop it before you get hurt.”
“Would you like to hear my opinion of you, sir?”
“It would in no wise alter the one I hold of you, Mr. Prescott. Still it
is not my plan to indulge in personalities. Remember that what I do
is done as a business and from friendship. I wrong no honest man
and deceive no trusting woman.”
“You make me out a scamp, which I am not, in my own estimation,”
he said hotly.
“That is another subject which we need not discuss, sir, since our
ideas would be sure to be at variance. You go your way and I go
mine; but at the last I wish to distinctly warn you that we are
prepared to give you your deserts if you persist in your course.”
“You can go to the man who employs you and tell him for me that
Paul Prescott defies him, and will fight him to the end!”
This was strange language—there was certainly nothing cringing
here.
“Very good. Your blood be on your own head. You are watched
when you least expect it.”
“Be careful you don’t go too far and get hurt.”
“Bah! I was in that opium den last night and saw the tragedy.”
Prescott started at this, and looked uneasy. “You there?”
“I was the man who took charge of the remains of that unfortunate
lady.”
“Is it possible—I never suspected I had seen you previous to our
meeting in the car. What did you accomplish?”
“The thing I desired. The world will never know that lady died
anywhere but in her own house.”
“Then you have done a good thing, sir.”

You might also like