SlideShare a Scribd company logo
1 of 53
Download to read offline
Android App Mistakes:
Avoiding the Anti-Patterns
   by Mark Murphy, CommonsWare
Introduction
• Anti-Patterns
 • Code pollution
 • Fatal assumptions
• Goal: help you avoid these anti-patterns
 • Developers
 • OEMs
Everlasting Services

• What is the Anti-Pattern?
 • Trying to have a service “always running”,
    instead of periodically running or running
    when your activity is on-screen
  • startService() without a stopService() or
    stopSelf()
Everlasting Services
• Why Is This Important?
 • Android kills off old services to reclaim
    RAM
  • Users kill off services they do not
    understand
    • Task killer
    • Settings > Applications > Services
Everlasting Services
• What is the Right Answer?
 • Rare use cases for everlasting services
   • VOIP app
 • Otherwise, aim to keep the service out
    of RAM when not actively doing work
   • AlarmManager
Background Control

• What is the Anti-Pattern?
 • Implementing a polling mechanism
    without any sort of user control
 • Implementing an everlasting service
    without user option to not have it
Background Control
• Why Is This Important?
 • Users may blame your app
   • Battery drain
   • RAM consumption
 • Users may kill or uninstall your app to
    regain control
Background Control

• What is the Right Answer?
 • If polling, let user choose period
    (including “never”)
  • If everlasting service, let user say “no”
   • VOIP app...but only for outgoing calls
Background Becomes Foreground


• What is the Anti-Pattern?
 • Doing significant processing in
    background callbacks
   • onReceive()
   • onCreate() / onStart() / onDestroy()
Background Becomes Foreground

• Why Is This Important?
 • Normally, background processing is CPU-
    capped at ~10%
  • During those callbacks, priority changes
    to normal foreground priority
  • May impact real foreground (e.g., game)
Background Becomes Foreground

• What is the Right Answer?
 • BroadcastReceiver delegates to
    IntentService
  • IntentService does everything in
    onHandleIntent()
  • Or other means of using background
    threads instead of work in callbacks
Boot-Time Services

• What is the Anti-Pattern?
 • Configuring a BroadcastReceiver to
    respond to the BOOT_COMPLETED
    broadcast Intent
Boot-Time Services
• Why Is This Important?
 • Too many boot-time services slows the
    boot down for user
   • Lots of process churn
 • May cause those broadcasts to be
    delayed for everyone
Boot-Time Services

• What is the Right Answer?
 • Use it only if truly necessary (e.g., restore
    AlarmManager alarms)
  • Do not do significant work right away
  • Wait for later trigger or user interaction
Using Multiple Processes

• What is the Anti-Pattern?
 • Forcing Android to use more than one
    process for your application
   • Remote services for internal use
   • Process attributes in manifest
   • Forking processes from JVM
Using Multiple Processes

• Why Is This Important?
 • Each process takes up a significant chunk
    of RAM
 • More active processes = more likely
    other things get kicked out of RAM
 • If user feels RAM is over-utilized, more
    likely to come after you with task killer
Using Multiple Processes

• What is the Right Answer?
 • Just avoid multiple processes!
 • Remote services/AIDL are for third-party
    use, not your own
 • Be careful on forking system processes
    (e.g., interpreters, built-in binaries)
Use Mutable Statics

• What is the Anti-Pattern?
 • Using static data members for more than
    final “constant” values
   • Caching
   • Cross-component communication
Use Mutable Statics
• Why Is This Important?
 • Android can close components to
    reclaim memory...if memory can be GC’d
 • Static references inhibit GC
 • Static references to things that hold onto
    components defeats Android’s intentions
Use Mutable Statics
• What is the Right Answer?
 • Avoid mutable statics where possible
 • Be very careful with the ones you need
   • Ensure they do not hold references
      (direct or indirect) to activities,
      services, etc.
Leak Like a Sieve

• What is the Anti-Pattern?
 • Register...and do not unregister
 • Acquire...and do not release
 • Assign to static...and not null out
    reference later
Leak Like a Sieve
• Why Is This Important?
 • Once again, the issue is GC
 • Registered listeners, etc. are held in static
    references inside of Android
  • Must get those released
   • Free up RAM
   • Stop events from continuing to fire
Leak Like a Sieve
• What is the Right Answer?
 • Aim to use lifecycle pairs
   • Register in onCreate(), release in
      onDestroy(), etc.
  • Use onDestroy() as “backstop”, releasing
    everything registered or acquired
  • Exception handling and finally {} block
Force Wrong UI

• What is the Anti-Pattern?
 • Adding an Exit menu choice or button
 • Trying to create a blocking modal dialog
 • Otherwise attempting to pretend
    Android is some other OS
Force Wrong UI
• Why Is This Important?
 • Android has a style all its own
   • Different, not necessarily bad
 • Breaking that style will cause some user
    confusion...or developer confusion when
    users do not behave as expected
Force Wrong UI
• What is the Right Answer?
 • Use Android navigation and APIs
    naturally, not trying to emulate some
    other model
 • Navigation: pretend you are writing a
    Web application
   • Flow is much like a Web browser
Ignore Screen Sizes
• What is the Anti-Pattern?
 • Focusing on one screen size
 • Focusing on customizing for individual
    screen sizes
   • Instead of coming up with layouts, etc.
      that work across sizes
Ignore Screen Sizes
• Why Is This Important?
 • Your app may not work well on existing
    devices
 • Your app may not work well on future
    devices
   • Many new screen sizes are coming
Ignore Screen Sizes
• What is the Right Answer?
 • Write to be resolution-independent
   • RelativeLayout
   • Don’t use pixel dimensions
 • Test on a wide range of layouts to
    confirm your independence actually
    works
Assume Touch

• What is the Anti-Pattern?
 • Writing an application that assumes the
    existence of a touchscreen and does not
    mandate it via the manifest
 • Not testing an application via the D-pad
    and keyboard alone
Assume Touch
• Why Is This Important?
 • Not all Android devices will be
    touchscreens
   • Television set-top boxes
   • Low-end smartphones / high-end
      “feature phones”
Assume Touch
• What is the Right Answer?
 • If your app only makes sense with
    touchscreen, require it via the manifest
 • If your app could be used by D-pad or
    equivalent, test it out, make sure it makes
    sense
   • “Tab order” issues
Make Busy App Widgets

• What is the Anti-Pattern?
 • Trying to update an app widget every
    second...from your code
   • Rolling your own timer
   • Faking an animation
Make Busy App Widgets
• Why Is This Important?
 • App widgets cross the process boundary
    on every update
   • Your code -> home screen process
 • IPC is expensive in terms of CPU time
  • Hence, expensive for battery
Make Busy App Widgets
• What is the Right Answer?
 • App widgets are largely static or driven
    by user input
  • Update the app widgets on user demand
    or slow timers
    • 30 minute minimum for
      updatePeriodMillis
Assume Unlimited Data
• What is the Anti-Pattern?
 • Assuming users have unlimited SMS plans
   • Using SMS for chatty app protocols
 • Assuming users have unlimited data plans
   • Constantly refreshing or downloading
      data
Assume Unlimited Data
• Why Is This Important?
 • “Unlimited” SMS or data only available in
    some markets to some users
   • Everybody else pays by the message or
      MB
 • Even “unlimited” usually comes with caps
 • Net: costs users money or capability
Assume Unlimited Data
• What is the Right Answer?
 • Allow user controls over chattiness or
    refresh periods
 • Pay attention to WiFi vs. mobile data
    connections
   • WiFi is less prone to costing users
      money
Reach Past the SDK
• What is the Anti-Pattern?
 • Using reflection to access public classes,
    etc. that are not part of the SDK
  • Accessing undocumented content
    providers
  • Assuming existence of certain binaries
Reach Past the SDK
• Why Is This Important?
 • Your app may break on today’s or
    tomorrow’s phones
 • Broken apps = unhappy users
  • Will they think ill of you...or Android?
 • Weakens arguments against OEMs
Reach Past the SDK
• What is the Right Answer?
 • Stick to the SDK
 • Work with the core Android team to
    figure out how to open more up
 • Coordinate SDK-hack wrappers
  • Strength in numbers
Anti-Patterns, OEM Style

• Plenty of anti-patterns for creating Android
  devices
• Impacts ability of developers to deliver
  quality apps for your device
• Impacts user satisfaction with apps, your
  device, and Android overall
Breaking the SDK
• What is the Anti-Pattern?
 • Changing Android and breaking API
 • Changing Android and changing behavior
    of documented resources
 • Inadequately setting expectations
  • Example: contacts and Facebook
Breaking the SDK
• Why Is This Important?
 • Broken SDK means some apps break
   when run on your device
   • Until developers spend extra effort
     trying to work around your errors
 • Broken expectations cause angst for
   users and developers alike
Breaking the SDK
• What is the Right Answer?
 • Test, test, test!
   • CTS is nowhere near sufficient
 • Developer device seeding program
   • Direct
   • Through services (DeviceAnywhere)
Hamper Developers

• What is the Anti-Pattern?
 • Break access to DDMS
 • Block non-Market app installs
 • Innovate without adequate explanation
   • Example: CDMA on Android 1.5
Hamper Developers
• Why Is This Important?
 • Users buy these devices for the apps
 • Important for apps to work well on your
    device
   • Users will think poorly of your device
   • Users will think poorly of Android
Hamper Developers
• What is the Right Answer?
 • End users are not your only user base
    you need to think about
 • Have developer programs for
    communication and support
 • Don’t screw up the firmware too bad
Block Replacement Firmware


• What is the Anti-Pattern?
 • Using cryptographic signing to prevent
    people from replacing firmware
Block Replacement Firmware

• Why Is This Important?
 • Segment of your user base values this
   • Can result in bad PR
 • Never know when a replacement
    firmware may increase the value of your
    device, or give you access to firmware
    talent
Block Replacement Firmware

• What is the Right Answer?
 • Best = support replacement firmware
   • ADP1, ADP2
 • Great = endorse replacement firmware
   • Nexus One
 • OK = “fig leaf” on firmware issue
Summary

• Avoid anti-patterns
 • Better for your apps
 • Better for your devices
 • Better for Android overall
Contact Info

• http://commonsware.com
• mmurphy@commonsware.com
• http://commonsware.com/blog
• Twitter: commonsguy
• +1.484.350.4004

More Related Content

More from CommonsWare

Android Security: Defending Your Users
Android Security: Defending Your UsersAndroid Security: Defending Your Users
Android Security: Defending Your UsersCommonsWare
 
Secondary Screen Support Using DisplayManager
Secondary Screen Support Using DisplayManagerSecondary Screen Support Using DisplayManager
Secondary Screen Support Using DisplayManagerCommonsWare
 
Mastering the Master Detail Pattern
Mastering the Master Detail PatternMastering the Master Detail Pattern
Mastering the Master Detail PatternCommonsWare
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful ThreadingCommonsWare
 
Android Development: The 20,000-Foot View
Android Development: The 20,000-Foot ViewAndroid Development: The 20,000-Foot View
Android Development: The 20,000-Foot ViewCommonsWare
 
Maps V2... And You!
Maps V2... And You!Maps V2... And You!
Maps V2... And You!CommonsWare
 
A Deep Dive Into ViewPager
A Deep Dive Into ViewPagerA Deep Dive Into ViewPager
A Deep Dive Into ViewPagerCommonsWare
 
Second-Screen Support in Android 4.2
Second-Screen Support in Android 4.2Second-Screen Support in Android 4.2
Second-Screen Support in Android 4.2CommonsWare
 
Integrate Android Apps and Web Apps
Integrate Android Apps and Web AppsIntegrate Android Apps and Web Apps
Integrate Android Apps and Web AppsCommonsWare
 
From Android to the Mobile Web
From Android to the Mobile WebFrom Android to the Mobile Web
From Android to the Mobile WebCommonsWare
 
The Wonderful World of Wearables
The Wonderful World of WearablesThe Wonderful World of Wearables
The Wonderful World of WearablesCommonsWare
 
Securing User Data with SQLCipher
Securing User Data with SQLCipherSecuring User Data with SQLCipher
Securing User Data with SQLCipherCommonsWare
 
Beaming Data to Devices with NFC
Beaming Data to Devices with NFCBeaming Data to Devices with NFC
Beaming Data to Devices with NFCCommonsWare
 
What's New in Jelly Bean
What's New in Jelly BeanWhat's New in Jelly Bean
What's New in Jelly BeanCommonsWare
 
Making Money at Mobile: 60 Business Models
Making Money at Mobile: 60 Business ModelsMaking Money at Mobile: 60 Business Models
Making Money at Mobile: 60 Business ModelsCommonsWare
 
AppsWorld Keynote
AppsWorld KeynoteAppsWorld Keynote
AppsWorld KeynoteCommonsWare
 
App Integration (Revised and Updated)
App Integration (Revised and Updated)App Integration (Revised and Updated)
App Integration (Revised and Updated)CommonsWare
 
Rich Text Editing and Beyond
Rich Text Editing and BeyondRich Text Editing and Beyond
Rich Text Editing and BeyondCommonsWare
 
App integration: Strategies and Tactics
App integration: Strategies and TacticsApp integration: Strategies and Tactics
App integration: Strategies and TacticsCommonsWare
 

More from CommonsWare (20)

Android Security: Defending Your Users
Android Security: Defending Your UsersAndroid Security: Defending Your Users
Android Security: Defending Your Users
 
Secondary Screen Support Using DisplayManager
Secondary Screen Support Using DisplayManagerSecondary Screen Support Using DisplayManager
Secondary Screen Support Using DisplayManager
 
Mastering the Master Detail Pattern
Mastering the Master Detail PatternMastering the Master Detail Pattern
Mastering the Master Detail Pattern
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful Threading
 
Android Development: The 20,000-Foot View
Android Development: The 20,000-Foot ViewAndroid Development: The 20,000-Foot View
Android Development: The 20,000-Foot View
 
Maps V2... And You!
Maps V2... And You!Maps V2... And You!
Maps V2... And You!
 
A Deep Dive Into ViewPager
A Deep Dive Into ViewPagerA Deep Dive Into ViewPager
A Deep Dive Into ViewPager
 
Second-Screen Support in Android 4.2
Second-Screen Support in Android 4.2Second-Screen Support in Android 4.2
Second-Screen Support in Android 4.2
 
Integrate Android Apps and Web Apps
Integrate Android Apps and Web AppsIntegrate Android Apps and Web Apps
Integrate Android Apps and Web Apps
 
From Android to the Mobile Web
From Android to the Mobile WebFrom Android to the Mobile Web
From Android to the Mobile Web
 
X Means Y
X Means YX Means Y
X Means Y
 
The Wonderful World of Wearables
The Wonderful World of WearablesThe Wonderful World of Wearables
The Wonderful World of Wearables
 
Securing User Data with SQLCipher
Securing User Data with SQLCipherSecuring User Data with SQLCipher
Securing User Data with SQLCipher
 
Beaming Data to Devices with NFC
Beaming Data to Devices with NFCBeaming Data to Devices with NFC
Beaming Data to Devices with NFC
 
What's New in Jelly Bean
What's New in Jelly BeanWhat's New in Jelly Bean
What's New in Jelly Bean
 
Making Money at Mobile: 60 Business Models
Making Money at Mobile: 60 Business ModelsMaking Money at Mobile: 60 Business Models
Making Money at Mobile: 60 Business Models
 
AppsWorld Keynote
AppsWorld KeynoteAppsWorld Keynote
AppsWorld Keynote
 
App Integration (Revised and Updated)
App Integration (Revised and Updated)App Integration (Revised and Updated)
App Integration (Revised and Updated)
 
Rich Text Editing and Beyond
Rich Text Editing and BeyondRich Text Editing and Beyond
Rich Text Editing and Beyond
 
App integration: Strategies and Tactics
App integration: Strategies and TacticsApp integration: Strategies and Tactics
App integration: Strategies and Tactics
 

Recently uploaded

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Android App Mistakes: Avoiding the Anti-Patterns

  • 1. Android App Mistakes: Avoiding the Anti-Patterns by Mark Murphy, CommonsWare
  • 2. Introduction • Anti-Patterns • Code pollution • Fatal assumptions • Goal: help you avoid these anti-patterns • Developers • OEMs
  • 3. Everlasting Services • What is the Anti-Pattern? • Trying to have a service “always running”, instead of periodically running or running when your activity is on-screen • startService() without a stopService() or stopSelf()
  • 4. Everlasting Services • Why Is This Important? • Android kills off old services to reclaim RAM • Users kill off services they do not understand • Task killer • Settings > Applications > Services
  • 5. Everlasting Services • What is the Right Answer? • Rare use cases for everlasting services • VOIP app • Otherwise, aim to keep the service out of RAM when not actively doing work • AlarmManager
  • 6. Background Control • What is the Anti-Pattern? • Implementing a polling mechanism without any sort of user control • Implementing an everlasting service without user option to not have it
  • 7. Background Control • Why Is This Important? • Users may blame your app • Battery drain • RAM consumption • Users may kill or uninstall your app to regain control
  • 8. Background Control • What is the Right Answer? • If polling, let user choose period (including “never”) • If everlasting service, let user say “no” • VOIP app...but only for outgoing calls
  • 9. Background Becomes Foreground • What is the Anti-Pattern? • Doing significant processing in background callbacks • onReceive() • onCreate() / onStart() / onDestroy()
  • 10. Background Becomes Foreground • Why Is This Important? • Normally, background processing is CPU- capped at ~10% • During those callbacks, priority changes to normal foreground priority • May impact real foreground (e.g., game)
  • 11. Background Becomes Foreground • What is the Right Answer? • BroadcastReceiver delegates to IntentService • IntentService does everything in onHandleIntent() • Or other means of using background threads instead of work in callbacks
  • 12. Boot-Time Services • What is the Anti-Pattern? • Configuring a BroadcastReceiver to respond to the BOOT_COMPLETED broadcast Intent
  • 13. Boot-Time Services • Why Is This Important? • Too many boot-time services slows the boot down for user • Lots of process churn • May cause those broadcasts to be delayed for everyone
  • 14. Boot-Time Services • What is the Right Answer? • Use it only if truly necessary (e.g., restore AlarmManager alarms) • Do not do significant work right away • Wait for later trigger or user interaction
  • 15. Using Multiple Processes • What is the Anti-Pattern? • Forcing Android to use more than one process for your application • Remote services for internal use • Process attributes in manifest • Forking processes from JVM
  • 16. Using Multiple Processes • Why Is This Important? • Each process takes up a significant chunk of RAM • More active processes = more likely other things get kicked out of RAM • If user feels RAM is over-utilized, more likely to come after you with task killer
  • 17. Using Multiple Processes • What is the Right Answer? • Just avoid multiple processes! • Remote services/AIDL are for third-party use, not your own • Be careful on forking system processes (e.g., interpreters, built-in binaries)
  • 18. Use Mutable Statics • What is the Anti-Pattern? • Using static data members for more than final “constant” values • Caching • Cross-component communication
  • 19. Use Mutable Statics • Why Is This Important? • Android can close components to reclaim memory...if memory can be GC’d • Static references inhibit GC • Static references to things that hold onto components defeats Android’s intentions
  • 20. Use Mutable Statics • What is the Right Answer? • Avoid mutable statics where possible • Be very careful with the ones you need • Ensure they do not hold references (direct or indirect) to activities, services, etc.
  • 21. Leak Like a Sieve • What is the Anti-Pattern? • Register...and do not unregister • Acquire...and do not release • Assign to static...and not null out reference later
  • 22. Leak Like a Sieve • Why Is This Important? • Once again, the issue is GC • Registered listeners, etc. are held in static references inside of Android • Must get those released • Free up RAM • Stop events from continuing to fire
  • 23. Leak Like a Sieve • What is the Right Answer? • Aim to use lifecycle pairs • Register in onCreate(), release in onDestroy(), etc. • Use onDestroy() as “backstop”, releasing everything registered or acquired • Exception handling and finally {} block
  • 24. Force Wrong UI • What is the Anti-Pattern? • Adding an Exit menu choice or button • Trying to create a blocking modal dialog • Otherwise attempting to pretend Android is some other OS
  • 25. Force Wrong UI • Why Is This Important? • Android has a style all its own • Different, not necessarily bad • Breaking that style will cause some user confusion...or developer confusion when users do not behave as expected
  • 26. Force Wrong UI • What is the Right Answer? • Use Android navigation and APIs naturally, not trying to emulate some other model • Navigation: pretend you are writing a Web application • Flow is much like a Web browser
  • 27. Ignore Screen Sizes • What is the Anti-Pattern? • Focusing on one screen size • Focusing on customizing for individual screen sizes • Instead of coming up with layouts, etc. that work across sizes
  • 28. Ignore Screen Sizes • Why Is This Important? • Your app may not work well on existing devices • Your app may not work well on future devices • Many new screen sizes are coming
  • 29. Ignore Screen Sizes • What is the Right Answer? • Write to be resolution-independent • RelativeLayout • Don’t use pixel dimensions • Test on a wide range of layouts to confirm your independence actually works
  • 30. Assume Touch • What is the Anti-Pattern? • Writing an application that assumes the existence of a touchscreen and does not mandate it via the manifest • Not testing an application via the D-pad and keyboard alone
  • 31. Assume Touch • Why Is This Important? • Not all Android devices will be touchscreens • Television set-top boxes • Low-end smartphones / high-end “feature phones”
  • 32. Assume Touch • What is the Right Answer? • If your app only makes sense with touchscreen, require it via the manifest • If your app could be used by D-pad or equivalent, test it out, make sure it makes sense • “Tab order” issues
  • 33. Make Busy App Widgets • What is the Anti-Pattern? • Trying to update an app widget every second...from your code • Rolling your own timer • Faking an animation
  • 34. Make Busy App Widgets • Why Is This Important? • App widgets cross the process boundary on every update • Your code -> home screen process • IPC is expensive in terms of CPU time • Hence, expensive for battery
  • 35. Make Busy App Widgets • What is the Right Answer? • App widgets are largely static or driven by user input • Update the app widgets on user demand or slow timers • 30 minute minimum for updatePeriodMillis
  • 36. Assume Unlimited Data • What is the Anti-Pattern? • Assuming users have unlimited SMS plans • Using SMS for chatty app protocols • Assuming users have unlimited data plans • Constantly refreshing or downloading data
  • 37. Assume Unlimited Data • Why Is This Important? • “Unlimited” SMS or data only available in some markets to some users • Everybody else pays by the message or MB • Even “unlimited” usually comes with caps • Net: costs users money or capability
  • 38. Assume Unlimited Data • What is the Right Answer? • Allow user controls over chattiness or refresh periods • Pay attention to WiFi vs. mobile data connections • WiFi is less prone to costing users money
  • 39. Reach Past the SDK • What is the Anti-Pattern? • Using reflection to access public classes, etc. that are not part of the SDK • Accessing undocumented content providers • Assuming existence of certain binaries
  • 40. Reach Past the SDK • Why Is This Important? • Your app may break on today’s or tomorrow’s phones • Broken apps = unhappy users • Will they think ill of you...or Android? • Weakens arguments against OEMs
  • 41. Reach Past the SDK • What is the Right Answer? • Stick to the SDK • Work with the core Android team to figure out how to open more up • Coordinate SDK-hack wrappers • Strength in numbers
  • 42. Anti-Patterns, OEM Style • Plenty of anti-patterns for creating Android devices • Impacts ability of developers to deliver quality apps for your device • Impacts user satisfaction with apps, your device, and Android overall
  • 43. Breaking the SDK • What is the Anti-Pattern? • Changing Android and breaking API • Changing Android and changing behavior of documented resources • Inadequately setting expectations • Example: contacts and Facebook
  • 44. Breaking the SDK • Why Is This Important? • Broken SDK means some apps break when run on your device • Until developers spend extra effort trying to work around your errors • Broken expectations cause angst for users and developers alike
  • 45. Breaking the SDK • What is the Right Answer? • Test, test, test! • CTS is nowhere near sufficient • Developer device seeding program • Direct • Through services (DeviceAnywhere)
  • 46. Hamper Developers • What is the Anti-Pattern? • Break access to DDMS • Block non-Market app installs • Innovate without adequate explanation • Example: CDMA on Android 1.5
  • 47. Hamper Developers • Why Is This Important? • Users buy these devices for the apps • Important for apps to work well on your device • Users will think poorly of your device • Users will think poorly of Android
  • 48. Hamper Developers • What is the Right Answer? • End users are not your only user base you need to think about • Have developer programs for communication and support • Don’t screw up the firmware too bad
  • 49. Block Replacement Firmware • What is the Anti-Pattern? • Using cryptographic signing to prevent people from replacing firmware
  • 50. Block Replacement Firmware • Why Is This Important? • Segment of your user base values this • Can result in bad PR • Never know when a replacement firmware may increase the value of your device, or give you access to firmware talent
  • 51. Block Replacement Firmware • What is the Right Answer? • Best = support replacement firmware • ADP1, ADP2 • Great = endorse replacement firmware • Nexus One • OK = “fig leaf” on firmware issue
  • 52. Summary • Avoid anti-patterns • Better for your apps • Better for your devices • Better for Android overall
  • 53. Contact Info • http://commonsware.com • mmurphy@commonsware.com • http://commonsware.com/blog • Twitter: commonsguy • +1.484.350.4004