O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Android High Performance Programming

Book Description

Build fast and efficient Android apps that run as reliably as clockwork in a multi-device world

About This Book

  • Wide coverage of various topics that help in developing optimal applications
  • Explore the concepts of Advanced Native Coding in depth
  • A must-have for professional-standard Android developers for whom performance failures and the sloppy use of resources are simply unacceptable

Who This Book Is For

This book is aimed at developers with an advanced knowledge of Android and who want to test their skills and learn new techniques to increase the performance of their applications. We assume they are comfortable working with the entire Android SDK, and have been doing it for a few years. They need to be familiar with frameworks such as NDK to use native code, which is crucial for app performance

What You Will Learn

  • Create Android applications that squeeze the most from the limited resource capacity of devices
  • Swap code that isn’t performing
  • Efficient memory management by identifying problems such as leaks
  • Reap the benefits of multithreaded and asynchronous programming
  • Maximize the security and encryption mechanisms natively provided by Android
  • Perform efficient network operations and techniques to retrieve data from servers
  • Master the NDK to write native code that can perform faster operations

In Detail

Performant applications are one of the key drivers of success in the mobile world. Users may abandon an app if it runs slowly. Learning how to build applications that balance speed and performance with functionality and UX can be a challenge; however, it's now more important than ever to get that balance right.

Android High Performance will start you thinking about how to wring the most from any hardware your app is installed on, so you can increase your reach and engagement. The book begins by providing an introduction to state–of-the-art Android techniques and the importance of performance in an Android application. Then, we will explain the Android SDK tools regularly used to debug and profile Android applications. We will also learn about some advanced topics such as building layouts, multithreading, networking, and security. Battery life is one of the biggest bottlenecks in applications; and this book will show typical examples of code that exhausts battery life, how to prevent this, and how to measure battery consumption from an application in every kind of situation to ensure your apps don’t drain more than they should.

This book explains techniques for building optimized and efficient systems that do not drain the battery, cause memory leaks, or slow down with time.

Style and approach

The book follows a tutorial-based approach to take the reader from the basic fundamentals of debugging to advanced performance-improvement concepts.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Android High Performance Programming
    1. Table of Contents
    2. Android High Performance Programming
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Introduction: Why High Performance?
      1. Why does the performance of an application mean so much to so many?
      2. Manual testing and automatic testing
      3. ANR and delays in software
        1. What triggers ANRs and how can I avoid them?
      4. Android architecture
      5. Dalvik Virtual Machine
      6. Memory management
      7. Energy consumption
      8. Java language
      9. Native Development Kit or how to develop with native code when needed
      10. Three limits in application responsiveness
      11. Business value of software quality
      12. Summary
    9. 2. Efficient Debugging
      1. Android Debug Bridge
      2. Dalvik Debug Monitor Server
      3. Capturing and analyzing thread information
      4. Heap analysis and visualization
      5. Allocation tracker
      6. Network usage
      7. Emulator Control
      8. System status
      9. Debugging the UI
      10. Profiling with Hierarchy Viewer
      11. Systrace
      12. Android device debug options
      13. Android Instant Run
      14. GPU profiler
      15. Running a trace
      16. ClassyShark
      17. Getting started
      18. Summary
    10. 3. Building Layouts
      1. Walkthrough
        1. Rendering performance
        2. Screen tearing and VSYNC
          1. Hardware acceleration
          2. Overdraw
        3. Multi-window mode
          1. Overview
          2. Configuration
          3. Management
          4. Drag and drop
          5. Performance impact
      2. Best practices
        1. Provided layout overview
        2. Hierarchical layout management
        3. Reusing layouts
        4. ViewStub
        5. AdapterViews and view recycling
          1. The ViewHolder pattern
        6. Custom views and layouts
        7. Screen zoom
      3. Debugging tools
        1. The Design view
        2. Hierarchy Viewer
          1. Tree View
          2. View properties
          3. Tree overview
          4. Layout View
        3. On device tools
          1. Debugging GPU overdraw
          2. Profile GPU rendering
        4. Systrace
      4. Summary
    11. 4. Memory
      1. Walkthrough
        1. How memory works
        2. Garbage collection
        3. Shared memory
        4. Runtime
        5. Android N JIT compiler
        6. Memory leak
        7. Memory churn
        8. References
        9. Memory-side projects
          1. Project Svelte and Android N
      2. Best practices
        1. Data types
          1. Autoboxing
          2. Sparse array family
          3. ArrayMap
        2. Syntax
          1. Collections
          2. Enumerations
        3. Constants
        4. Object management
          1. Strings
          2. String concatenation
          3. Local variables
          4. Arrays versus collections
          5. Streams
        5. Memory patterns
          1. The object pool pattern
          2. The FlyWeight pattern
        6. Android component leaks
          1. Activities
            1. Static fields
            2. Non-static inner classes
            3. Singletons
            4. Anonymous inner classes
            5. Handlers
          2. Services
        7. Processes
        8. The memory API
        9. Main components and memory management
      3. Debugging tools
        1. LogCat
          1. Dalvik
          2. ART
        2. The ActivityManager API
        3. StrictMode
        4. Dumpsys
          1. Meminfo
          2. ProcStats
      4. Summary
    12. 5. Multithreading
      1. Walkthrough
        1. Threading basics
          1. Multicore CPUs
          2. Threads
          3. Multithreaded applications
          4. Thread safety
        2. Android multithreading environment
          1. Processes
        3. Android application thread
          1. The UI thread
          2. Worker threads
          3. The binder thread
        4. Android thread messaging
      2. Best practices
        1. Threads
        2. HandlerThread
          1. When to use
        3. AsyncTask
          1. Methods
          2. Generics parameters
          3. States management
          4. Executor
          5. When to use
        4. Loaders
          1. LoaderManager
          2. LoaderCallbacks<D>
          3. Provided loaders
            1. AsyncTaskLoader
            2. CursorLoader
          4. When to use
        5. Services
          1. Life cycle
          2. Started Service
            1. When to use
          3. Bound Service
            1. When to use
          4. IntentService
            1. When to use
        6. Inter-process communication
          1. Remote Procedure Call
          2. AIDL
          3. Messenger
        7. Advanced techniques
          1. BroadcastReceiver asynchronous techniques
          2. ContentProvider asynchronous techniques
          3. Repeating tasks
            1. Timer
            2. ScheduledExecutorService
            3. AlarmManager
      3. Debugging tools
        1. StrictMode
      4. Summary
    13. 6. Networking
      1. Walkthrough
        1. Protocols
        2. Methods
        3. Headers
        4. Timeout
        5. Content
          1. Compression
        6. Response code
        7. Connection types
      2. Best practices
        1. Latency gauging
        2. Batching connections
          1. Prefetching
          2. Queuing connections
        3. Caching responses
          1. Cache control
          2. Last-Modified
          3. If-Modified-Since
        4. Exponential back-off
        5. Polling versus pushing
        6. Provided APIs
          1. SyncManager
            1. Authenticator
            2. SyncAdapter
        7. Android N changes
          1. Data Saver
          2. Background optimization
          3. GcmNetworkManager
            1. The service implementation
            2. Task scheduling
            3. Task features
            4. Task
            5. OneoffTask
            6. PeriodicTask
      3. Debugging tools
        1. Android N Networking ADB tool
        2. Fiddler
        3. Wireshark
        4. Application Resource Optimizer
        5. Network attenuation
        6. Speed and delay emulation
          1. Fiddler
          2. Network Link Conditioner
          3. Network Attenuator
      4. Summary
    14. 7. Security
      1. WhatsApp – the eternal showcase of "no-gos"
      2. Going deeper into the code
      3. Capturing an APK file
        1. Pulling a file from the device
        2. Capturing an APK using Wireshark
        3. Using external websites
      4. Autopsy of an APK file
      5. Code injection
        1. Opcodes
        2. Injecting new code
        3. Signing and rebuilding the application
        4. Protecting our application
      6. Insecure storage
        1. SharedPreferences
        2. InternalStorage
        3. ExternalStorage
        4. Deleting files
        5. Using external or internal storage
        6. Databases
        7. Performance in databases
        8. SQL injections
        9. ORM frameworks
        10. OrmLite
        11. SugarORM
        12. GreenDAO
        13. Realm
        14. Network
      7. Encrypted communication
        1. Sniffing
      8. Summary
    15. 8. Optimizing Battery Consumption
      1. Analysis
        1. Monitoring battery level and charging status
        2. How to identify changes in the charging status
        3. Determining and reacting to changes in the battery level
      2. Doze feature and App Standby
        1. Understanding Doze
      3. Avoiding useless network requests
      4. Dealing with BroadcastReceivers on demand
      5. Networking
      6. Summary
    16. 9. Native Coding in Android
      1. Getting started – setting up NDK in our system
      2. JNI
      3. Initial concepts
      4. Creating our first HelloWorld-JNI
      5. Creating a native activity with Android NDK
      6. Debugging NDK
      7. Android.mk
      8. More variables in NDK
        1. TARGET_PLATFORM
        2. TARGET_ARCH
        3. TARGET_ABI
      9. NDK macros
      10. Application.mk
      11. Including existing libraries
      12. Exporting header files
      13. Summary
    17. 10. Performance Tips
      1. Bitmaps
        1. Loading
        2. Processing
        3. Caching
        4. Displaying
        5. Managing memory
      2. Image optimization
        1. Resolution
        2. Compression
      3. Serialization
        1. JSON improvements
        2. JSON alternatives
          1. Protocol buffers
          2. Flat buffers
        3. Local serialization
      4. Code improvements
        1. Getters and setters
        2. Inner classes
      5. Java 8 in Android N
        1. Setup
        2. Features
          1. Default interface methods
          2. Static interface methods
          3. Lambda expression
          4. Repeating annotations
        3. Jack toolchain
      6. APK optimizations
        1. Removing unused code
        2. Removing unused resources
      7. Summary
    18. Index