Table of Contents......Page 4 The Book’s Structure......Page 46 Testing......Page 47 Advanced UI......Page 48 Data Storage and Retrieval......Page 49 Hardware and System Services......Page 50 Tuning Android Applications......Page 51 About the Updates......Page 52 Warescription......Page 53 About the APK Edition......Page 54 Book Bug Bounty......Page 55 Creative Commons and the Four-to-Free (42F) Guarantee......Page 56 Acknowledgments......Page 57 Programming Language......Page 60 Activities......Page 62 Widgets, Containers, and Resources......Page 63 Apps and Packages......Page 64 Types......Page 65 The Emulator......Page 66 OS Versions and API Levels......Page 67 Processes and Threads......Page 68 Don’t Be Scared......Page 69 Eclipse......Page 70 Yet Other Alternatives......Page 71 App Inventor......Page 72 App Generators......Page 73 But First, Some Notes About Android’s Emulator......Page 74 Step #1: Checking Your Hardware......Page 75 Step #3: Install Android Studio......Page 76 Step #4: Install the SDKs and Add-Ons......Page 78 Using SDK Manager and Updating Your Environment......Page 85 In Our Next Episode…......Page 86 Creating a New Project......Page 88 Copying a Project......Page 93 Starter Project Generators......Page 94 About Our Tutorial Project......Page 96 Step #1: Importing the Project......Page 97 Windows......Page 101 Step #3: Set Up the AVD......Page 102 Step #4: Set Up the Device......Page 109 Windows Update......Page 112 macOS and Linux......Page 113 Step #5: Running the Project......Page 114 In Our Next Episode…......Page 116 Navigating The Project Explorer......Page 118 Classic Project View......Page 119 Opening Files from the Explorer......Page 120 The Basics......Page 121 Build View......Page 122 SDK and AVD Managers......Page 124 Android Studio and Release Channels......Page 125 Visit the Trails!......Page 127 The Modules......Page 128 The Java......Page 129 The Build Instructions......Page 130 The App Directory......Page 131 The Source Sets......Page 132 What You Get Out Of It......Page 133 What is Gradle?......Page 134 What is Groovy?......Page 135 Direct Installation......Page 136 The gradlew Wrapper......Page 137 Versions of Gradle and the Android Gradle Plugin......Page 138 The Project-Level File......Page 139 buildscript......Page 140 The Module-Level Gradle File......Page 141 Introducing the Manifest......Page 142 Package Name and Application ID......Page 143 minSdkVersion and targetSdkVersion......Page 144 Other Gradle Items of Note......Page 145 An Application For Your Application......Page 146 Supporting Multiple Screens......Page 147 Learning More About Gradle......Page 148 Visit the Trails!......Page 149 Step #1: Supporting Screens......Page 150 Step #3: Ignoring Lint......Page 151 In Our Next Episode…......Page 153 String Theory......Page 154 Plain Strings......Page 155 Um, Wait, My Manifest Does Not Look Like That......Page 157 Styled Text......Page 158 Editing String Resources......Page 159 Multi-Locale Support......Page 160 Got the Picture?......Page 161 Some Notes About WebP......Page 163 Dimensions......Page 164 Editing Dimension Resources......Page 165 The Resource That Shall Not Be Named… Yet......Page 166 App Icons… And Everything Else......Page 168 Foreground Layer......Page 169 Legacy......Page 172 Using In Your Manifest......Page 174 Creating Other Icons with the Asset Studio......Page 175 Step #1: Changing the Name......Page 176 Step #2: Changing the Icon......Page 177 In Our Next Episode…......Page 182 What Are Widgets?......Page 184 What Are Containers?......Page 186 The Absolute Positioning Anti-Pattern......Page 187 The Theme of This Section: Themes......Page 188 In the Beginning, There Was “Theme”, And It Was Meh......Page 189 Holo, There!......Page 190 Doing More with Themes......Page 191 The Activity......Page 194 Dissecting the Activity......Page 195 What Is an XML-Based Layout?......Page 196 Why Use XML-Based Layouts?......Page 198 Using Layouts from Java......Page 199 Widgets and Attributes......Page 200 The Curious Case of the Cast......Page 201 Size......Page 202 Introducing the Graphical Layout Editor......Page 203 Palette......Page 204 Preview......Page 205 Blueprint......Page 206 Upper Left Toolbar......Page 207 Component Tree......Page 208 Attributes......Page 209 Text Tab......Page 211 And Now, Some Notes About the Book’s Sample Projects......Page 212 Assigning Labels......Page 213 Android Studio Graphical Layout Editor......Page 214 Editing the Text......Page 215 Editing the ID......Page 216 Notable TextView Attributes......Page 217 A Commanding Button......Page 218 Tracking Button Clicks......Page 220 Fleeting Images......Page 221 Android Studio Graphical Layout Editor......Page 222 Scaling Images......Page 224 Fields of Green. Or Other Colors.......Page 227 Notable EditText Attributes......Page 229 Padding......Page 230 Colors......Page 231 Useful Methods......Page 232 Visit the Trails!......Page 233 Debugging Your App......Page 234 Logcat in Android Studio......Page 235 The Case of the Confounding Class Cast......Page 236 Point Break......Page 237 Introducing the Sampler App......Page 238 RTL and Your Layouts......Page 239 Orientation......Page 240 Weight......Page 241 Android Studio Graphical Layout Editor......Page 242 The XML......Page 244 Android Studio Graphical Layout Editor......Page 246 Example: Stacked-Percent......Page 247 The XML......Page 248 Android Studio Graphical Layout Editor......Page 249 Example: URL Dialog......Page 250 The XML......Page 251 Android Studio Graphical Layout Editor......Page 253 The XML......Page 262 Android Studio Graphical Layout Editor......Page 264 The Problem......Page 265 Positions Relative to Container......Page 266 Positions Relative to Other Widgets......Page 267 Android Studio Graphical Layout Editor......Page 268 The XML......Page 269 Android Studio Graphical Layout Editor......Page 270 Example: URL Dialog......Page 272 Example: Overlap......Page 274 Putting Cells in Rows......Page 276 Non-Row Children of TableLayout......Page 277 Stretch, Shrink, and Collapse......Page 278 Example: A Bigger Form......Page 279 Example: URL Dialog......Page 281 Hey, What About ConstraintLayout?......Page 282 Turning Back to RTL......Page 283 Just a Box to Check......Page 286 Android Studio Graphical Layout Editor......Page 289 Don’t Like Checkboxes? How About Toggles or Switches?......Page 290 Turn the Radio Up......Page 294 Scrollwork......Page 297 Framing the Scene......Page 301 Visit the Trails!......Page 302 Step #1: Creating a New Layout Resource......Page 304 Step #2: Defining the UI......Page 305 In Our Next Episode…......Page 309 GUI Building, Continued......Page 310 Including Includes......Page 311 Preview of Coming Attractions......Page 313 AdapterViews and Adapters......Page 314 Using ArrayAdapter......Page 315 Lists of Naughty and Nice......Page 316 Choice Modes......Page 319 Clicks versus Selections, Revisited......Page 321 Spin Control......Page 322 Grid Your Lions (Or Something Like That…)......Page 326 Fields: Now With 35% Less Typing!......Page 331 Step #0: Get Things Set Up Simply......Page 336 Step #1: Design Your Row......Page 337 Step #3: Override the Constructor and getView()......Page 338 Optimizing with the ViewHolder Pattern......Page 340 Dealing with Multiple Row Layouts......Page 343 Visit the Trails!......Page 344 Role of WebView......Page 346 Adding the Widget......Page 347 Loading Content Via a URL......Page 348 Supporting JavaScript......Page 350 Alternatives for Loading Content......Page 351 Listening for Events......Page 352 Addressing the Link/Redirect Behavior......Page 355 Safe Browsing......Page 357 Visit the Trails!......Page 358 Styles: DIY DRY......Page 360 Where to Apply a Style......Page 362 Inheriting a Style......Page 363 The Possible Values......Page 364 What Happens If You Have No Theme......Page 365 Android Studio’s Theme Editor......Page 366 What’s a Dependency?......Page 372 Dependency Scopes......Page 373 Depending on a Local JAR......Page 374 Major Library Families from Google......Page 375 Configure the Repositories......Page 376 Identify the Dependencies and Versions......Page 377 Add the Dependencies......Page 378 About the Names......Page 379 Choosing a Version......Page 380 Attaching It To Your Project......Page 381 Step #1: Getting Rid of Existing Cruft......Page 384 Step #2: Requesting New Dependencies......Page 385 In Our Next Episode…......Page 387 Drag-and-Drop GUI Builders......Page 388 Performance......Page 389 Getting ConstraintLayout......Page 390 Using a ConstraintLayout......Page 391 The XML......Page 392 The Android Studio Graphical Layout Editor......Page 394 Basic Anchoring, Dual Axis......Page 400 Playing the Percentages......Page 403 Converting Existing Layouts......Page 405 Visit the Trails!......Page 407 AdapterView and its Discontents......Page 408 Enter RecyclerView......Page 409 The Dependency......Page 410 A RecyclerViewActivity......Page 411 The LayoutManager......Page 412 The Adapter......Page 413 The ViewHolder......Page 415 What’s Missing?......Page 417 CardView......Page 418 DividerItemDecoration......Page 421 DIY Decorators......Page 422 Handling Click Events......Page 425 Responding to Clicks......Page 426 Option #1: Translucent Selector on Top......Page 428 Option #2: Background Selector......Page 430 Option #3: Controlled Ripple Emanation Point......Page 431 Visit the Trails!......Page 432 Android 1.x/2.x......Page 434 Android 3.0-4.1, Tablets......Page 435 Android 4.0-4.4, Phones......Page 437 Android 4.2-4.4, Tablets......Page 438 Android 5.0+......Page 440 Your Action Bar Options......Page 441 A Quick Note About Toasts......Page 442 Setting the Target......Page 443 Defining the Resource......Page 445 A Quick Note About Android Studio......Page 446 Responding to Events......Page 447 The Rest of the Sample Activity......Page 448 MENU Key, We Hardly Knew Ye......Page 455 Action Bars, Live in Living Color!......Page 456 Color Resources......Page 457 Applying the Theme......Page 458 The Results......Page 459 Restoring the Icon (Sort Of)......Page 460 Action Bar Style Generator......Page 461 Designing the Scheme......Page 462 Implementing the Scheme......Page 464 Visit the Trails!......Page 466 Getting the Artwork......Page 468 Android Studio Vector Asset Wizard......Page 469 Using the Artwork......Page 472 VectorDrawableCompat......Page 473 Gradle Configuration......Page 474 Use in Java......Page 475 Step #1: Adding Some Icons......Page 480 Step #2: Defining Some Options......Page 482 Step #3: Loading and Responding to Our Options......Page 485 Step #4: Supporting Older Devices......Page 486 Step #5: Trying It Out......Page 488 In Our Next Episode…......Page 489 When Processes Are Created......Page 490 BACK, HOME, and Your Process......Page 491 Termination......Page 492 You and Your Heap......Page 493 Activities and Their Lifecycles......Page 494 Defining the Class and Resources......Page 495 Populating the Class and Resources......Page 497 Augmenting the Manifest......Page 498 Warning! Contains Explicit Intents!......Page 499 Using Implicit Intents......Page 501 Where Do We Get These Uri Values?......Page 506 Extra! Extra!......Page 507 Pondering Parcelable......Page 509 Schroedinger’s Activity......Page 510 onCreate() and onDestroy()......Page 511 onPause() and onResume()......Page 512 When Activities Die......Page 513 Walking Through the Lifecycle......Page 514 Recycling Activities......Page 517 Application: Transcending the Activity......Page 518 The Case of the Invisible Activity......Page 519 Step #1: Creating the Stub Activity Class and Manifest Entry......Page 522 Step #2: Launching Our Activity......Page 524 In Our Next Episode…......Page 525 What?......Page 526 WHY?!?!?......Page 527 Screen Sizes......Page 528 Where You Get Your Fragments From......Page 529 The Fragment Layout......Page 530 The Fragment Class......Page 531 The Activity Layout......Page 532 The Activity Class......Page 533 The Fragment Lifecycle Methods......Page 534 onAttach() Versus onAttach()......Page 535 The ListFragment Class......Page 536 The Activity Class......Page 537 Fragments and the Action Bar......Page 539 Step #1: Create a SimpleContentFragment......Page 542 Step #2: Examining SimpleContentFragment......Page 544 In Our Next Episode…......Page 545 Pieces of a Pager......Page 546 The Activity......Page 547 The PagerAdapter......Page 548 The Fragment......Page 549 The Result......Page 551 Paging Other Stuff......Page 552 PagerTitleStrip and PagerTabStrip......Page 553 Revisiting the Containers Sampler......Page 556 The Data......Page 557 The Fragment......Page 559 The Activity......Page 560 The PagerAdapter......Page 561 Visit the Trails!......Page 562 Step #1: Add a ViewPager to the Layout......Page 564 Step #2: Creating a ContentsAdapter......Page 565 Step #3: Setting Up the ViewPager......Page 566 In Our Next Episode…......Page 569 What’s a Configuration? And How Do They Change?......Page 570 Configurations and Resource Sets......Page 571 Screen Size and Orientation......Page 572 The Modern: Developer-Defined Buckets......Page 573 About That API Level......Page 574 Coping with Complexity......Page 575 Choosing The Right Resource......Page 576 Scenario #2: Disparate Resource Set Categories......Page 577 Scenario #5: Screen Density......Page 578 Scenario #6: Screen Sizes......Page 579 Use Case: Themes by API Level......Page 580 Destroy and Recreate the Activity......Page 582 Retain Some Widget State......Page 583 State Saving Scenarios......Page 584 Retain Your Fragments......Page 585 Add to the Bundle......Page 592 Fragments and a Bundle......Page 594 onRetainNonConfigurationInstance()......Page 595 DIY......Page 597 Blocking Rotations......Page 599 And Now, a Word From the Android Project View......Page 600 Configuration Challenges......Page 601 Multi-Locale Support......Page 602 Screen Zoom/Dynamic Density......Page 603 Basic Tinting Options......Page 608 Official Google-Approved Colors......Page 613 The Main Application Thread......Page 614 The Theory......Page 616 The Stages of AsyncTask......Page 617 A Sample Task......Page 618 The Fragment and its AsyncTask......Page 619 The Activity and the Results......Page 623 Where Not to Use AsyncTask......Page 624 About the AsyncTask Thread Pool......Page 625 Alternatives to AsyncTask......Page 626 Event Buses......Page 628 What Is an Event Bus?......Page 629 Requesting the Artifact......Page 630 Defining Events......Page 631 Posting Events......Page 632 Receiving Events......Page 633 The Activity......Page 635 Visit the Trails!......Page 637 Requesting Permissions......Page 638 What Are Some Common Permissions, and What Do They Defend?......Page 639 How Do I Request a Permission?......Page 640 Installing Through SDK Tools......Page 641 Installing from the Play Store, Android 6.0+......Page 642 Installing by Other Means, Android 5.1 and Older......Page 643 Name......Page 644 Normal......Page 645 Maximum SDK Version......Page 646 New Permissions in Old Applications......Page 647 What Permissions Are Affected By This?......Page 648 How Do I Know If I Have Permission?......Page 649 How Do I Ask the User For Permission?......Page 652 When Do I Not Ask the User For Permission?......Page 656 What Do I Do If the User Says “No”?......Page 657 What Do I Do If the User Says “No, And Please Stop Asking”?......Page 658 What Happens When My App Has a Lower Target SDK Version?......Page 659 Should I Be Using PermissionChecker?......Page 660 Examining the Protocol......Page 661 Requesting the Permission......Page 662 Handling the Result......Page 663 Dealing with (Configuration) Change......Page 664 Raw Resources......Page 666 Assets......Page 667 Internal vs. External......Page 668 Working with Internal Storage......Page 669 Where to Write......Page 677 Relevant Permissions......Page 678 Letting the User See Your Files......Page 681 Removable Storage......Page 683 Multiple User Accounts......Page 684 Linux Filesystems: You Sync, You Win......Page 685 StrictMode: Avoiding Janky Code......Page 687 Mounting as a Drive......Page 689 Browsing Files Via Android Studio......Page 690 Push and Pull for External Storage......Page 694 Run-As for Internal Storage......Page 695 XML Parsing Options......Page 696 Using Files with Implicit Intents......Page 697 Visit the Trails!......Page 698 Step #1: Adding Some Content......Page 700 Step #2: Using SimpleContentFragment......Page 701 Step #3: Launching Our Activities, For Real This Time......Page 702 Step #4: Getting a Bit More Material......Page 703 Step #5: Seeing the Results......Page 705 In Our Next Episode…......Page 707 Step #1: Adding a Book......Page 708 Step #2: Creating a ModelFragment......Page 709 Step #3: Defining Our Model......Page 710 Step #5: Defining Our Event......Page 712 Step #6: Loading Our Model......Page 713 Step #7: Registering for Events......Page 716 Step #8: Adapting the Content......Page 717 Step #9: Showing the Content When Loaded......Page 718 Step #10: Attaching our ModelFragment......Page 719 Step #11: Showing the Content After a Configuration Change......Page 720 Step #12: Setting Up StrictMode......Page 721 In Our Next Episode…......Page 722 Getting What You Want......Page 724 Collecting Preferences with PreferenceFragment......Page 725 Showing the Current Values......Page 726 Defining Your Preferences......Page 729 Creating Your PreferenceFragment......Page 730 The Results......Page 731 Types of Preferences......Page 734 EditTextPreference......Page 735 RingtonePreference......Page 736 ListPreference and MultiSelectListPreference......Page 737 Step #1: Defining the Preference XML Files......Page 742 Step #2: Creating Our Preference Activity......Page 746 Step #4: Launching the Preference Activity......Page 747 Step #5: Loading the Preferences......Page 750 Step #6: Saving the Last-Read Position......Page 754 Step #7: Restoring the Last-Read Position......Page 755 Step #8: Keeping the Screen On......Page 756 In Our Next Episode…......Page 758 Introducing SQLite......Page 760 Start with a Helper......Page 761 Employing Your Helper......Page 765 Getting Data Out......Page 766 Your Query Options......Page 767 Using the Cursor Manually......Page 768 Introducing CursorAdapter......Page 769 Getting Data Out, Asynchronously......Page 771 The Primary Option: execSQL()......Page 773 Alternative Options......Page 774 Asynchronous CRUD and UI Updates......Page 775 Setting Transaction Bounds......Page 778 Visit the Trails!......Page 779 Step #1: Adding a DatabaseHelper......Page 782 Step #2: Examining DatabaseHelper......Page 783 Step #3: Creating a NoteFragment......Page 784 Step #5: Creating the NoteActivity......Page 785 Step #6: Examining NoteActivity......Page 786 Step #7: Add Notes to the Action Bar......Page 787 Step #8: Defining a NoteLoadedEvent......Page 789 Step #9: Loading a Note from the Database......Page 790 Step #10: Loading the Note Into the Fragment......Page 791 Step #11: Updating the Database......Page 792 Step #12: Saving the Note......Page 793 Step #13: Adding a Delete Action Bar Item......Page 795 Step #14: Closing the NoteFragment When Deleted......Page 797 In Our Next Episode…......Page 803 DIY HTTP......Page 804 Creating Your Data Model......Page 805 A Thread for Loading......Page 807 A Fragment for Questions......Page 809 An Activity for Orchestration......Page 812 What Android Brings to the Table......Page 813 What About HttpClient?......Page 814 Using Third-Party Libraries......Page 815 Using HTTP Client Libraries......Page 816 OkHttp......Page 817 Retrofit......Page 819 Creating the Interface......Page 820 Making the Request......Page 822 Downloading and Installing Picasso......Page 824 Updating the Model......Page 825 Requesting the Images......Page 826 The Rest of the Story......Page 827 Volley......Page 828 Making a Manager......Page 829 Requesting JSON......Page 831 Requesting Images......Page 834 Visit the Trails......Page 837 Pieces of Intents......Page 838 Intent Routing......Page 839 Responding to Implicit Intents......Page 840 Requesting Implicit Intents......Page 843 One Match......Page 844 The Chooser Override......Page 845 Direct Share Targets......Page 847 ShareActionProvider......Page 848 Practice Safe Content Resolution......Page 851 Receiving a Broadcast: In an Activity......Page 854 Receiving a Broadcast: Via the Manifest......Page 855 Getting Into the Stopped State......Page 856 At Boot Time......Page 857 On Battery State Changes......Page 859 Sticky Broadcasts and the Battery......Page 864 Battery and the Emulator......Page 865 The Order of Things......Page 866 Keeping It Local......Page 867 Visit the Trails!......Page 868 Step #1: Adding a ShareActionProvider......Page 870 Step #2: Sharing the Note......Page 871 Step #3: Testing the Result......Page 874 In Our Next Episode…......Page 877 Why Services?......Page 878 The Service Class......Page 879 Lifecycle Methods......Page 880 Manifest Entry......Page 881 Sending Commands with startService()......Page 882 Binding to Services......Page 883 The Service Implementation......Page 884 Using the Service......Page 886 The Power of the PendingIntent......Page 887 How Do We Execute the PendingIntent?......Page 888 Why Not Just Pass the Intent?......Page 889 Broadcast Intents......Page 890 Messenger......Page 891 The Design......Page 892 Using the Service......Page 893 The Service Implementation......Page 895 Receiving the Broadcast......Page 897 Limitation: Time......Page 898 JobIntentService......Page 899 Services and Configuration Changes......Page 903 Scenario #2: Android Terminates Your Process......Page 904 Background Service Limitations......Page 905 What Happens?......Page 906 What Are the Alternatives?......Page 908 Step #1: Adding a Stub DownloadCheckService......Page 910 Step #2: Tying the Service Into the Action Bar......Page 911 Step #4: Defining Our JSON......Page 913 Step #5: Defining Our Retrofit Interface......Page 914 Step #6: Retrieving Our JSON Via Retrofit......Page 915 Step #7: Downloading the Update......Page 917 Step #8: Unpacking the Update......Page 918 Step #9: Using the Update......Page 921 In Our Next Episode…......Page 926 Step #1: Creating Our Layouts......Page 928 Step #3: Opening the Sidebar......Page 933 Step #4: Loading Content Into the Sidebar......Page 934 Step #5: Removing Content From the Sidebar......Page 937 Think Forwards, Not Backwards......Page 944 A Target-Rich Environment......Page 946 Lint: It’s Not Just For Belly Buttons......Page 947 Avoid the New on the Old......Page 948 @TargetAPI......Page 949 Resources......Page 950 Components......Page 951 Keeping Track of Changes......Page 952 What is a System Service?......Page 954 What System Services Are There?......Page 955 …From the Standpoint of Developers?......Page 958 Android Pay / Google Wallet......Page 959 Google Analytics......Page 960 Google Cast......Page 961 Google Cloud Messaging......Page 962 Google Location Services......Page 963 Google Mobile Vision......Page 964 Adding Play Services to Your Project......Page 965 Dealing with Runtime Permissions......Page 966 Detecting If We Have Permission......Page 967 Requesting Permissions......Page 968 Dealing with Configuration Changes......Page 969 Initializing the GoogleApiClient......Page 970 Connecting and Disconnecting......Page 972 Questions. Sometimes, With Answers.......Page 976 Heading to the Source......Page 977 Creating a Library Module......Page 980 Library Modules and the Manifest......Page 983 Library Modules and Transitive Dependencies......Page 984 Limitations of Library Modules......Page 985 Key Build-Related Tasks......Page 986 Results......Page 989 Objectives of the Project Structure......Page 990 Source Sets......Page 991 Product Flavors......Page 992 Build Variants......Page 993 Source Sets......Page 994 build.gradle Settings......Page 995 Order of Precedence......Page 997 Adding Product Flavors and Getting Build Variants......Page 998 Scoping Your Splits......Page 1000 Requesting Density Splits......Page 1001 The Build Variants View......Page 1002 The Android Project View......Page 1003 Flavors, Build Types, and the Project Structure Dialog......Page 1004 Manifest Scenarios......Page 1006 App Manifest and Product Flavors......Page 1007 Markers and Selectors......Page 1008 Placeholders......Page 1009 Viewing Merged Manifests in Android Studio......Page 1010 Basic Merger Rules......Page 1011 Example #1: Manifest Attributes......Page 1012 Example #2: Additional Permissions......Page 1014 Example #3: Additional Components......Page 1015 Example #4: Intent Filter......Page 1016 uses-sdk......Page 1017 Markers and Selectors......Page 1018 Employing Placeholders......Page 1021 Role of Code Signing......Page 1026 Synchronizing Your Debug Signing Key......Page 1027 Manually......Page 1028 Android Studio......Page 1030 Gradle......Page 1033 Two Types of Key Security......Page 1035 Versioning......Page 1036 Logging......Page 1037 Testing......Page 1038 EULA......Page 1039 Customizing a Gradle Build......Page 1042 Code Generation......Page 1043 Create a Java Module......Page 1044 Apply the Groovy Plugin......Page 1046 Add a Groovy Source Directory and Package......Page 1047 Implement a Plugin......Page 1048 Using Both Groovy and Java......Page 1049 Adding Plugin Metadata......Page 1050 Distributing the Plugin......Page 1051 Using the Plugin......Page 1054 Trying the Sample App......Page 1056 Integrating with the Android Plugin......Page 1057 Validating the Environment......Page 1058 Splicing Your Task Into the Build......Page 1059 Configuring the Plugin......Page 1061 What Drives the Custom Code?......Page 1062 Other Project Files......Page 1063 Java as Poetry......Page 1064 Writing a Code Generation Plugin......Page 1065 Designing the Output......Page 1066 Crafting the Plugin......Page 1067 Writing the Task......Page 1070 Generating Java Code......Page 1072 Using the Generated Code......Page 1076 It’s build.gradle All The Way Down......Page 1078 gradle.properties......Page 1079 Custom Properties Files......Page 1080 Auto-Incrementing the versionCode......Page 1081 Adding to BuildConfig......Page 1083 Instrumentation Tests and Unit Tests......Page 1088 Where Your Test Code Runs......Page 1089 The Test Methods......Page 1090 Setup and Teardown......Page 1091 Testing Activities......Page 1093 Testing Context-Dependent Code......Page 1094 The Test Runner......Page 1096 Android Studio Ad-Hoc Test Runs......Page 1097 Android Studio Run Configuration......Page 1098 Examining the Test Results......Page 1102 Testing and Runtime Permissions......Page 1103 The Android Test Orchestrator......Page 1104 Adding a Shot of Espresso......Page 1108 Finding Widgets via Hamcrest Matchers......Page 1111 Performing Actions......Page 1112 Stock Assertions......Page 1113 Custom Assertions......Page 1114 Starting and Recording......Page 1117 What You Get......Page 1120 Testing AdapterView......Page 1121 Testing RecyclerView......Page 1122 Intent Testing......Page 1125 Testing Activity Re-Creation and Configuration Changes......Page 1128 Waiting for the World to Change......Page 1131 What’s an IdlingResource?......Page 1132 Using an IdlingResource......Page 1133 Implementing a Custom IdlingResource......Page 1136 Why Choose UI Automator Over Alternatives?......Page 1138 Gradle and Android Studio Settings......Page 1139 Performing Device-Level Actions......Page 1140 Getting Proxies for Widgets and Containers......Page 1141 UiObject, UiCollection, and UiScrollable......Page 1142 Asserting Conditions......Page 1144 Using the UI Automator Viewer......Page 1145 Who Tests the Testers?......Page 1148 Branch Coverage......Page 1149 Loop Coverage......Page 1150 Coverage and Your Instrumentation Tests......Page 1151 Prerequisites......Page 1156 Scenario: Clean Architecture......Page 1157 Setting Up Unit Testing......Page 1158 Adding the Test JUnit Dependency......Page 1159 Adding the Test Package......Page 1160 Writing a Test Case......Page 1161 Running Unit Tests......Page 1164 From Android Studio......Page 1165 From the Command Line......Page 1167 Why Are We Being Mean to Android?......Page 1168 Why Mockito?......Page 1169 Setting Up Mockito......Page 1170 Using Mockito in Unit Tests......Page 1171 Robolectric......Page 1176 Choosing an API Level......Page 1177 Writing Robolectric Tests......Page 1178 Running Robolectric Tests......Page 1180 OK, So, Why Bother?......Page 1181 Writing a MonkeyRunner Script......Page 1182 Monkeying Around......Page 1184 Prerequisites......Page 1190 The Basic Idea......Page 1191 Using Lambda Expressions......Page 1192 Replacing Listeners with Lambdas......Page 1193 Alternative: Method References......Page 1196 Life is But a Stream......Page 1198 Action and Reaction......Page 1199 A Rx For What Ails You......Page 1201 Adding the Dependency......Page 1202 Replacing the Loop......Page 1203 Be Your Own Stream......Page 1204 Removing the AsyncTask......Page 1206 observeOn()......Page 1210 subscribe()......Page 1211 Lambdas and Lifetimes......Page 1212 Streaming from a Resource......Page 1213 Error Handling......Page 1214 Transmogrification......Page 1215 Rx-Enabled Libraries......Page 1216 What About LiveData?......Page 1218 What’s a Notification?......Page 1222 Notifications and Channels......Page 1225 Showing a Simple Notification......Page 1226 The Activity-Or-Notification Scenario......Page 1231 Big (and Rich) Notifications......Page 1232 The Styles......Page 1233 The Sample......Page 1235 The Results......Page 1238 Isn’t “Foreground Service” an Oxymoron?......Page 1239 Putting Your Service in the Foreground......Page 1240 Disabled Notifications......Page 1242 Android 4.x......Page 1243 Android 5.0+......Page 1244 Android 8.0+......Page 1245 Being a Good Citizen......Page 1246 What You Do with Channels......Page 1247 Creating Channel Groups......Page 1248 Creating Channels......Page 1249 What the User Sees......Page