Post on 27-Jan-2015
description
Developing Fault-Tolerant, !“German Engineered” Android Apps
Andrew Levy andrew@cri/ercism.com CEO, co-‐founder Cri/ercism
[droidcon ~]$ whoami
• Andrew Levy – Co-‐founder & CEO @Cri/ercism
• Cri/ercism – Real-‐Cme crash reports and error analysis for mobile developers
– Analyzed well over a billion applicaCon loads in the past few months
– Backed by Google Ventures, Kleiner-‐Perkins, and others
www.cri&ercism.com -‐ @cri&ercism
What’s Ahead
1. TesCng DifficulCes 2. Top 5 Android Crashes 3. iOS vs Android, which crashes less??? 4. Quick peak at Cri/ercism’s Error Analysis 5. QuesCons 6. Free SCckers
www.cri&ercism.com -‐ @cri&ercism
Android Device Diversity
Example app shortly a_er launch: has 30k DAU, 175k unique users, 50 different devices
www.cri&ercism.com -‐ @cri&ercism
iOS Fragmentation
This app has over 850,000 users, 26 diff app versions, 12 different devices
www.cri&ercism.com -‐ @cri&ercism
How Many iOS Versions Released?
iOS VersionsiOS Versions
2008 2009 2010 2011 2012
4.3.2April 14, 2011
1.0.1July 31, 2007
4.2.8May 4, 2011
4.3.1March 25, 2011
3.0.1July 31, 2009
3.2.2August 11, 2010
1.0June 29, 2007
4.3March 9, 2011
3.2.1July 15, 2010
4.2.10July 25, 2011
1.0.2August 21, 2007
4.1September 8, 2010
2.0July 11, 2008
4.0.2August 11, 2010
1.1September 14, 2007
4.2.1November 22, 2010
1.1.3January 15, 2008
3.2April 3, 2010
4.2.6February 10, 2011
3.1.3February 2, 2010
4.3.3May 4, 2011
4.3.5July 25, 2011
5.0October 12, 2011
4.2.7April 14, 2011
4.2.9July 15, 2011
3.1September 9, 2009
1.1.5July 15, 2008
2.2November 21, 2008
4.2.5February 7, 2011
3.0June 17, 2009
4.3.4July 15, 2011
2.0.2August 18, 2008
4.0June 21, 2010
1.1.4February 26, 2008
3.1.2October 8, 2009
1.1.1September 27, 2007
2.1September 9, 2008
4.0.1July 15, 2010
2.2.1January 27, 2009
2.0.1August 4, 2008
1.1.2November 12, 2007
www.cri&ercism.com -‐ @cri&ercism
iOS 5.0.1 November 10 2011
iOS 5.1 March 7 2012
www.cri&ercism.com -‐ @cri&ercism
Android Testing!Build & Test • Integrated TesCng Tools in your IDE
• OEMs (developer.samsung.com, developer.motorola.com, etc) • 3rd Party SoluCons
• RoboCum, MonkeyRunner, Robolectric, etc
• Remote Device TesCng • Also offered by OEMs • Device Anywhere, uTest, Mob4Hire, Perfecto Mobile, etc.
• Crashes sCll make it out into the wild • QA Matrix/PermutaCons
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
java.lang.NullPointerException
• “works for me” is not a good test, there are a thousand Android devices – Assume the worst, common places of NPE:
• #1 locaCon in code for NPEs seems to be onResume() – Probably due to devs assuming class members accessible a_er onPause()
• Data from intents • Peripheral/Sensor data (Camera, GPS, etc)
– Some of these errors can be device/manufacturer specific
• Broadcast receivers
www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
java.lang.OutOfMemoryError
try {
// allocate lots of objects } catch(OutOfMemoryError oom) {
// recycle some objects }
Not the best idea:
Memory Leak in Dalvik: Keeping a reference to an object longer than needed
www.cri&ercism.com -‐ @cri&ercism
java.lang.OutOfMemoryError
Biggest Culprit: Bitmaps – memory usage
• width px * height px * 4 bytes (for ARGB images) – So_References, recycle – Pre Honeycomb (<3.0) naCve heap, now in the normal heap • Use hprof -‐-‐ if using Eclipse, Memory Analyzer (MAT)
ListViews -‐ Use convertView & viewHolder Pa/ern
Memory Leak in Dalvik: Keeping a reference to an object longer than needed
www.cri&ercism.com -‐ @cri&ercism
java.lang.OutOfMemoryError
• What about the NDK? – In the past some devs built their C++ code on other plaxorms, then used tools on that plaxorm to analyze
– New as of November 5, 2011: • Valgrind 3.7.0 adds preliminary ARM Android support (2.3.x) – May need rooted AOSP build, need libc with debug symbols
www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
android.view.WindowManager$BadTokenException
Biggest Culprit: Dialogs • Common error for beginners, creaCng a dialog via
getApplicaConContext() – Android docs are wrong, need to create dialog in context of an AcCvity
www.cri&ercism.com -‐ @cri&ercism
android.view.WindowManager$BadTokenException
Biggest Culprit: Dialogs • Anything that changes the context of the acCvity will cause issues
– Showing a dialog a_er acCvity’s onPause is called (phone call, Back, Home, etc)
– Examples • Screen RotaCon during a ProgressBar
– The most common (and not recommended) fix: » Add android:configChanges="orientaCon” to manifest, just
onConfiguraConChanged is called – Recommended:
» save your instance state, re-‐set any acCvity context references to the new one
• Launch dialog with an old/null context
– Be sure to use DialogFragment, old dialogs are deprecated
www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
java.lang.IllegalArgumentException !(extends java.lang.RuntimeException)
• Common Examples:
– ContentProvider does not exist • i.e. try the Network LocaCon Provider in the emulator • Don’t use undocumented Content Providers
– Using unknown or invalid View IDs • Confusing a string idenCfier and integer value wrt setValue()
– AcCvity is finished already, but you call dismiss on a dialog
www.cri&ercism.com -‐ @cri&ercism
java.lang.RuntimeException
• Common Pixalls
– Forge{ng to add an AcCvity to the Manifest
– Failing to close a camera service before using it again • Some phones have a race condiCon with releasing the camera, be sure to make these calls in surfaceDestroyed
1. cameraObject.setPreviewCallback(null); // prevent race condiCon 2. cameraObject.stopPreview(); 3. cameraObject.release();
– Loading UI elements directly from a background thread
– Trying to use a recycled bitmap
www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
Top 5 Android Crashes!+85% of crashes !
1. java.lang.NullPointerException !!2. java.lang.OutOfMemoryError"!
3. android.view.WindowManager$BadTokenException"!
4. java.lang.IllegalArgumentException & java.lang.RuntimeException!
5. android.database.sqlite.SQLiteException!www.cri&ercism.com -‐ @cri&ercism
android.database.sqlite.SQLiteException
• Use the database helper, accessing DB file directly prone to errors – Read/write permissions, SD card issues, etc.
• Use only one SQLite connecCon – Even if you use threads, share one connecCon
• Prepared Queries with unknown columns is a common mistake
• SQL Syntax Errors
www.cri&ercism.com -‐ @cri&ercism
Crashes on iOS & Android
www.cri&ercism.com -‐ @cri&ercism
www.cri&ercism.com -‐ @cri&ercism
Hard to read pie chart
Crashes on iOS & Android
www.cri&ercism.com -‐ @cri&ercism
www.cri&ercism.com -‐ @cri&ercism
Come find us at"
Andrew Levy andrew@cri/ercism.com @cri/ercism and @andrewmlevy
Questions?!