SlideShare a Scribd company logo
AnDevCon III

                     Backwards
                   Compatibility:
                   Strategies and
                       Tactics
Copyright © 2012 CommonsWare, LLC
Device Mix




                        image Copyright © 2012 Google, Inc. – reprinted with permission




Copyright © 2012 CommonsWare, LLC
Device Mix
    ●   The Market Is Not the World
         ●   Kindle Fire
         ●   NOOK Tablet
         ●   Wearables (WIMM, I'm Watch, etc.)
         ●   Miscellaneous non-Market devices
    ●   If You Distribute to These, Take Their Versions Into
        Account!



Copyright © 2012 CommonsWare, LLC
Device Mix
    ●   Predictions Sure To Go Wrong
         ●   November 2012: ICS and newer reaches majority status
               –   New device sales
               –   Upgrades for existing devices
         ●   December 2012: Mayan Apocalypse
         ●   April 2013: Android 2.x below 20%
         ●   September 2013: Android 2.x below 5%
         ●   December 2013: Ragnarök


Copyright © 2012 CommonsWare, LLC
Competing Strategies
    ●   Old API-Centric
         ●   Lowest common denominator
         ●   Today: Android 2.3, maybe 2.2
         ●   Only deal with newer things when unavoidable
    ●   New API-Centric
         ●   More aggressively support newer capabilities
              –   Particularly with respect to UI
         ●   Today: ICS
         ●   Gracefully degrade for older devices

Copyright © 2012 CommonsWare, LLC
Forwards, Not Backwards
    ●   Older Ain't Gettin' Any Bigger
         ●   Aim for the increasing market share, not the decreasing
             share
    ●   Sneezers' Devices
         ●   Bloggers, media, etc.
         ●   Tend towards newer devices
         ●   If you look stale, may impact their interest in you



Copyright © 2012 CommonsWare, LLC
Forwards, Not Backwards
    ●   Distinctive, Not Decomposing
         ●   Are there new features that can give you a competitive
             edge?
               –   Even if not all the users can take advantage of them
    ●   It's the Way the (Web) World Works
         ●   IE6-only versus graceful degradation
         ●   Well-trod path, needing Android-specific tactics




Copyright © 2012 CommonsWare, LLC
Targets and Strategies
    ●   Old API-Centric
         ●   Build target = min SDK version = lowest common
             denominator
         ●   Target SDK version no higher than min SDK version
    ●   New API-Centric
         ●   Build target = oldest version that has everything you are
             using directly (or via library)
         ●   Min SDK version = oldest you are supporting
         ●   Target SDK version = current-ish Android version
Copyright © 2012 CommonsWare, LLC
Validating Compatibility
    ●   Compile-Time: Lint
         ●   Keep your Android SDK tools/ADT updated!
         ●   Will warn you when you try using newer stuff on older
             minSdkVersion
               –   Requires manual lint run from Eclipse menu
         ●   Use annotations to suppress warnings
               –   @TargetApi(NN)




Copyright © 2012 CommonsWare, LLC
Validating Compatibility
    ●   Testing
         ●   Directly via emulator
         ●   Directly via available hardware
         ●   Bulk-testing Services
               –   TestDroid Cloud
               –   LessPainful
               –   Apkudo
               –   Etc.



Copyright © 2012 CommonsWare, LLC
Libraries for Compatibility
    ●   Android Support Package
         ●   Fragments
              –   Large subset, but not everything from native API Level 11+
         ●   Loaders
         ●   GridLayout
         ●   *Compat Classes
              –   Access to newer constants
              –   Helper methods to get at newer capabilities while gracefully
                  degrading to no-ops/defaults
         ●   Separate Implementations

Copyright © 2012 CommonsWare, LLC
Libraries for Compatibility
    ●   Action Bar Sherlock
         ●   Implementation of ActionBar for Android 2.x
         ●   Same API as native API Level 11+
    ●   Nine Old Androids
         ●   Implementation of android.animation framework
             for Android 1.x/2.x




Copyright © 2012 CommonsWare, LLC
Ad-Hoc Backports
    ●   Find AOSP Code, Clone, Fix
         ●   New widgets (e.g., Switch)
         ●   Other stuff largely separable from firmware
         ●   Challenges
               –   Finding resources
               –   Dealing with package-private methods




Copyright © 2012 CommonsWare, LLC
Library/Backport Strategy
    ●   Use as Temporary Scaffolding
         ●   Know which API levels a given library or backport is
             addressing
         ●   May vary by what you are using
    ●   Remove In Time
         ●   Once you no longer are supporting the older API levels,
             drop the scaffold and re-test




Copyright © 2012 CommonsWare, LLC
Java Version Guards
    ●   Android 2.x+
         ●   Wrap any references to items from newer API level in a
             test of the API level

 if (Build.VERSION.SDK_INT>Build.VERSION_CODES.GINGERBREAD) {
     // do something
 }




Copyright © 2012 CommonsWare, LLC
Java Version Guards
    ●   Android 2.x+
         ●   Wrap any references to items from newer API level in a
             test of the API level

 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD) {
     @TargetApi(11)
     // do something needing API Level 11+
 }




Copyright © 2012 CommonsWare, LLC
Java Version Guards
    ●   Scenario: AsyncTask
         ●   Serialized if targetSdkVersion >= 14
         ●   Use executeOnExecutor() to force to classic thread
             pool model
         ●   Problem: executeOnExecutor() new to API Level 11
         ●   Solution: version guard block




Copyright © 2012 CommonsWare, LLC
Java Version Guards
    ●   Android 1.x
         ●   Problem: Dalvik complains if you load a class that refers
             to items from a newer API level
         ●   Solution
               –   Isolate newer-API items in a separate helper class
               –   Only refer to that helper class within a version guard block




Copyright © 2012 CommonsWare, LLC
Java Version Guards
    ●   The Old API-Centric Approach
         ●   Use version guard to detect newer API level
         ●   Use reflection to access items that only exist at that API
             level
         ●   Benefit: can leave build target on older API level
               –   Build target only affects what you directly reference




Copyright © 2012 CommonsWare, LLC
Versioned Resources
    ●   Use -vNN Resource Set Suffix
         ●   Apply to resources valid for API Level NN and higher
         ●   Scenario
               –   res/values/ = good for anything
               –   res/values-v11/ = good for Honeycomb and newer
         ●   Example
               –   Style in -v11 that references Theme.Holo
               –   Style with same name in default directory that does not
                   reference Theme.Holo


Copyright © 2012 CommonsWare, LLC
Versioned Components
    ●   Components Valid for Certain API Levels
         ●   Activity only needed on Android 3.1+
         ●   Different app widgets for API Level 11, older devices
    ●   Technique
         ●   Boolean resource, contained in two resource sets
               –   Default plus -vNN set
         ●   Use boolean resource in android:enabled attribute in
             manifest
               –   Disabled components do not show up anywhere

Copyright © 2012 CommonsWare, LLC
Scenario: Preferences
   ●   Goals
        ●   Want to use PreferenceFragments and headers on API
            Level 11 and higher
        ●   Want preferences to still work on Android 2.x
   ●   Option #1: Different PreferenceActivity Classes
        ●   Use versioned components trick to dictate which one is
            available
        ●   Use action, not component, in launching Intent
        ●   Reuse preference XML as best you can


Copyright © 2012 CommonsWare, LLC
Scenario: Preferences
    ●   Option #2: Version Guards
         ●   One PreferenceActivity
         ●   onBuildHeaders() only called on API Level 11+
         ●   Reuse preference XML as best you can
    ●   Option #3: Backport
         ●   Find current implementation (source, resources)
         ●   Convert to use Android Support fragments
         ●   Most difficulty, but consistent implementation,
             look-and-feel
Copyright © 2012 CommonsWare, LLC
QOTD
  “You’re a Web developer in Java” is the first thing I tell every person
  mentioning fragmentation. There are no tablets. There are no
  phones. There’s no Google TV. There is only an unlimited set of
  configurations of every conceivable feature. Write your application
  in a dynamic, progressively enhancing manner that follows well-
  documented patterns and you will be just fine.
                                                            Jake Wharton




Copyright © 2012 CommonsWare, LLC

More Related Content

PDF
Android App Development Intro at ESC SV 2012
PDF
Introduction to android
PDF
Slides bootcamp21
PDF
Android dev o_auth
PPT
Introduction to android sessions new
PDF
Android Development Tutorial V3
PPTX
Build Trust in Your Build-to-Deployment Flow!
PDF
Android chapter02-setup2-emulator
Android App Development Intro at ESC SV 2012
Introduction to android
Slides bootcamp21
Android dev o_auth
Introduction to android sessions new
Android Development Tutorial V3
Build Trust in Your Build-to-Deployment Flow!
Android chapter02-setup2-emulator

What's hot (19)

PPT
Android Programming Basic
PDF
Android Programming
PPTX
Android session-1-sajib
PPT
9780134433646 annuzzi ch02 (1)
PDF
Developing and-benchmarking-native-linux-applications-on-android
PPT
Mobile Java
PPTX
Ii 1300-java essentials for android
PPTX
Introduction to Android Development Part 1
PDF
Android and its feature
PPTX
Ii 1500-publishing your android application
ODP
Java Meetup - 12-03-15 - Android Development Workshop
PDF
Ad102 - Extreme Makeover -- LotusScript and Java Editor Edition
PDF
Rationalize Android Development with StAnD - Clement Escoffier, akquinet
PPT
Introduction to Android Development
PDF
AD201 - IBM Domino Application Development Today And Tomorrow
PDF
Android fundamentals and tutorial for beginners
PPT
MTJ Taking Mobile Java Developers to the Next Level
PDF
Android Development Workshop
PDF
Ad103 - Have it Your Way: Extending IBM Lotus Domino Designer
Android Programming Basic
Android Programming
Android session-1-sajib
9780134433646 annuzzi ch02 (1)
Developing and-benchmarking-native-linux-applications-on-android
Mobile Java
Ii 1300-java essentials for android
Introduction to Android Development Part 1
Android and its feature
Ii 1500-publishing your android application
Java Meetup - 12-03-15 - Android Development Workshop
Ad102 - Extreme Makeover -- LotusScript and Java Editor Edition
Rationalize Android Development with StAnD - Clement Escoffier, akquinet
Introduction to Android Development
AD201 - IBM Domino Application Development Today And Tomorrow
Android fundamentals and tutorial for beginners
MTJ Taking Mobile Java Developers to the Next Level
Android Development Workshop
Ad103 - Have it Your Way: Extending IBM Lotus Domino Designer
Ad

Similar to Backwards Compatibility: Strategies and Tactics (20)

PDF
Securing User Data with SQLCipher
PDF
App Integration (Revised and Updated)
PDF
Gradle and Your Android Wearable Projects
PDF
App integration: Strategies and Tactics
PDF
Android workshop material
PDF
From Android to the Mobile Web
PDF
What's New in Jelly Bean
PDF
Gl android platform
PPT
Android My Seminar
PDF
Continuous integration for androids
PPTX
Seminar on android app development
PPTX
Android studio&Gradle&Autotest
PDF
Android Attacks
PDF
MAUI vs Flutter vs React vs Avalonia_ A Detailed Comparison.pdf
PDF
Android Platform Architecture
DOCX
Android_Studio_Structure.docx
PDF
Android Development in a Nutshell
PPTX
PPTX
PPT
Android Application Development Using Java
Securing User Data with SQLCipher
App Integration (Revised and Updated)
Gradle and Your Android Wearable Projects
App integration: Strategies and Tactics
Android workshop material
From Android to the Mobile Web
What's New in Jelly Bean
Gl android platform
Android My Seminar
Continuous integration for androids
Seminar on android app development
Android studio&Gradle&Autotest
Android Attacks
MAUI vs Flutter vs React vs Avalonia_ A Detailed Comparison.pdf
Android Platform Architecture
Android_Studio_Structure.docx
Android Development in a Nutshell
Android Application Development Using Java
Ad

More from CommonsWare (20)

PDF
Getting Android Developers for Your Wearables
PDF
When Microwatts Are Precious: Battery Tips for Wearable Apps
PDF
The Action Bar: Front to Back
PDF
Android Security: Defending Your Users
PDF
Secondary Screen Support Using DisplayManager
PDF
Mastering the Master Detail Pattern
PDF
Not Quite As Painful Threading
PDF
Android Development: The 20,000-Foot View
PDF
Maps V2... And You!
PDF
A Deep Dive Into ViewPager
PDF
Second-Screen Support in Android 4.2
PDF
Integrate Android Apps and Web Apps
PDF
X Means Y
PDF
The Wonderful World of Wearables
PDF
Beaming Data to Devices with NFC
PDF
Making Money at Mobile: 60 Business Models
PDF
AppsWorld Keynote
PDF
Rich Text Editing and Beyond
PDF
Android Hardware That's A Little Bit... Odd
PDF
Google TV For Fun
Getting Android Developers for Your Wearables
When Microwatts Are Precious: Battery Tips for Wearable Apps
The Action Bar: Front to Back
Android Security: Defending Your Users
Secondary Screen Support Using DisplayManager
Mastering the Master Detail Pattern
Not Quite As Painful Threading
Android Development: The 20,000-Foot View
Maps V2... And You!
A Deep Dive Into ViewPager
Second-Screen Support in Android 4.2
Integrate Android Apps and Web Apps
X Means Y
The Wonderful World of Wearables
Beaming Data to Devices with NFC
Making Money at Mobile: 60 Business Models
AppsWorld Keynote
Rich Text Editing and Beyond
Android Hardware That's A Little Bit... Odd
Google TV For Fun

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Cloud computing and distributed systems.
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
PDF
KodekX | Application Modernization Development
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
Cloud computing and distributed systems.
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
KodekX | Application Modernization Development
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf

Backwards Compatibility: Strategies and Tactics

  • 1. AnDevCon III Backwards Compatibility: Strategies and Tactics Copyright © 2012 CommonsWare, LLC
  • 2. Device Mix image Copyright © 2012 Google, Inc. – reprinted with permission Copyright © 2012 CommonsWare, LLC
  • 3. Device Mix ● The Market Is Not the World ● Kindle Fire ● NOOK Tablet ● Wearables (WIMM, I'm Watch, etc.) ● Miscellaneous non-Market devices ● If You Distribute to These, Take Their Versions Into Account! Copyright © 2012 CommonsWare, LLC
  • 4. Device Mix ● Predictions Sure To Go Wrong ● November 2012: ICS and newer reaches majority status – New device sales – Upgrades for existing devices ● December 2012: Mayan Apocalypse ● April 2013: Android 2.x below 20% ● September 2013: Android 2.x below 5% ● December 2013: Ragnarök Copyright © 2012 CommonsWare, LLC
  • 5. Competing Strategies ● Old API-Centric ● Lowest common denominator ● Today: Android 2.3, maybe 2.2 ● Only deal with newer things when unavoidable ● New API-Centric ● More aggressively support newer capabilities – Particularly with respect to UI ● Today: ICS ● Gracefully degrade for older devices Copyright © 2012 CommonsWare, LLC
  • 6. Forwards, Not Backwards ● Older Ain't Gettin' Any Bigger ● Aim for the increasing market share, not the decreasing share ● Sneezers' Devices ● Bloggers, media, etc. ● Tend towards newer devices ● If you look stale, may impact their interest in you Copyright © 2012 CommonsWare, LLC
  • 7. Forwards, Not Backwards ● Distinctive, Not Decomposing ● Are there new features that can give you a competitive edge? – Even if not all the users can take advantage of them ● It's the Way the (Web) World Works ● IE6-only versus graceful degradation ● Well-trod path, needing Android-specific tactics Copyright © 2012 CommonsWare, LLC
  • 8. Targets and Strategies ● Old API-Centric ● Build target = min SDK version = lowest common denominator ● Target SDK version no higher than min SDK version ● New API-Centric ● Build target = oldest version that has everything you are using directly (or via library) ● Min SDK version = oldest you are supporting ● Target SDK version = current-ish Android version Copyright © 2012 CommonsWare, LLC
  • 9. Validating Compatibility ● Compile-Time: Lint ● Keep your Android SDK tools/ADT updated! ● Will warn you when you try using newer stuff on older minSdkVersion – Requires manual lint run from Eclipse menu ● Use annotations to suppress warnings – @TargetApi(NN) Copyright © 2012 CommonsWare, LLC
  • 10. Validating Compatibility ● Testing ● Directly via emulator ● Directly via available hardware ● Bulk-testing Services – TestDroid Cloud – LessPainful – Apkudo – Etc. Copyright © 2012 CommonsWare, LLC
  • 11. Libraries for Compatibility ● Android Support Package ● Fragments – Large subset, but not everything from native API Level 11+ ● Loaders ● GridLayout ● *Compat Classes – Access to newer constants – Helper methods to get at newer capabilities while gracefully degrading to no-ops/defaults ● Separate Implementations Copyright © 2012 CommonsWare, LLC
  • 12. Libraries for Compatibility ● Action Bar Sherlock ● Implementation of ActionBar for Android 2.x ● Same API as native API Level 11+ ● Nine Old Androids ● Implementation of android.animation framework for Android 1.x/2.x Copyright © 2012 CommonsWare, LLC
  • 13. Ad-Hoc Backports ● Find AOSP Code, Clone, Fix ● New widgets (e.g., Switch) ● Other stuff largely separable from firmware ● Challenges – Finding resources – Dealing with package-private methods Copyright © 2012 CommonsWare, LLC
  • 14. Library/Backport Strategy ● Use as Temporary Scaffolding ● Know which API levels a given library or backport is addressing ● May vary by what you are using ● Remove In Time ● Once you no longer are supporting the older API levels, drop the scaffold and re-test Copyright © 2012 CommonsWare, LLC
  • 15. Java Version Guards ● Android 2.x+ ● Wrap any references to items from newer API level in a test of the API level if (Build.VERSION.SDK_INT>Build.VERSION_CODES.GINGERBREAD) { // do something } Copyright © 2012 CommonsWare, LLC
  • 16. Java Version Guards ● Android 2.x+ ● Wrap any references to items from newer API level in a test of the API level if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD) { @TargetApi(11) // do something needing API Level 11+ } Copyright © 2012 CommonsWare, LLC
  • 17. Java Version Guards ● Scenario: AsyncTask ● Serialized if targetSdkVersion >= 14 ● Use executeOnExecutor() to force to classic thread pool model ● Problem: executeOnExecutor() new to API Level 11 ● Solution: version guard block Copyright © 2012 CommonsWare, LLC
  • 18. Java Version Guards ● Android 1.x ● Problem: Dalvik complains if you load a class that refers to items from a newer API level ● Solution – Isolate newer-API items in a separate helper class – Only refer to that helper class within a version guard block Copyright © 2012 CommonsWare, LLC
  • 19. Java Version Guards ● The Old API-Centric Approach ● Use version guard to detect newer API level ● Use reflection to access items that only exist at that API level ● Benefit: can leave build target on older API level – Build target only affects what you directly reference Copyright © 2012 CommonsWare, LLC
  • 20. Versioned Resources ● Use -vNN Resource Set Suffix ● Apply to resources valid for API Level NN and higher ● Scenario – res/values/ = good for anything – res/values-v11/ = good for Honeycomb and newer ● Example – Style in -v11 that references Theme.Holo – Style with same name in default directory that does not reference Theme.Holo Copyright © 2012 CommonsWare, LLC
  • 21. Versioned Components ● Components Valid for Certain API Levels ● Activity only needed on Android 3.1+ ● Different app widgets for API Level 11, older devices ● Technique ● Boolean resource, contained in two resource sets – Default plus -vNN set ● Use boolean resource in android:enabled attribute in manifest – Disabled components do not show up anywhere Copyright © 2012 CommonsWare, LLC
  • 22. Scenario: Preferences ● Goals ● Want to use PreferenceFragments and headers on API Level 11 and higher ● Want preferences to still work on Android 2.x ● Option #1: Different PreferenceActivity Classes ● Use versioned components trick to dictate which one is available ● Use action, not component, in launching Intent ● Reuse preference XML as best you can Copyright © 2012 CommonsWare, LLC
  • 23. Scenario: Preferences ● Option #2: Version Guards ● One PreferenceActivity ● onBuildHeaders() only called on API Level 11+ ● Reuse preference XML as best you can ● Option #3: Backport ● Find current implementation (source, resources) ● Convert to use Android Support fragments ● Most difficulty, but consistent implementation, look-and-feel Copyright © 2012 CommonsWare, LLC
  • 24. QOTD “You’re a Web developer in Java” is the first thing I tell every person mentioning fragmentation. There are no tablets. There are no phones. There’s no Google TV. There is only an unlimited set of configurations of every conceivable feature. Write your application in a dynamic, progressively enhancing manner that follows well- documented patterns and you will be just fine. Jake Wharton Copyright © 2012 CommonsWare, LLC