Post on 09-Feb-2017
transcript
Developing Fault-Tolerant, “German Engineered” Android Apps
Andrew Levyandrew@crittercism.comCEO, co-founder Crittercism
[droidcon ~]$ whoami
• Andrew Levy– Co-founder & CEO @Crittercism
• Crittercism– Real-time crash reports and error analysis for mobile
developers– Analyzed well over a billion application loads in the
past few months– Backed by Google Ventures, Kleiner-Perkins, and others
www.crittercism.com - @crittercism
www.crittercism.com - @crittercism
What’s Ahead
1. Testing Difficulties2. Top 5 Android Crashes3. iOS vs Android, which crashes less???4. Quick peak at Crittercism’s Error Analysis5. Questions6. Free Stickers
Android Device DiversityExample app shortly after launch: has 30k DAU, 175k unique users, 50 different devices
www.crittercism.com - @crittercism
iOS Fragmentation
This app has over 850,000 users, 26 diff app versions, 12 different devices
www.crittercism.com - @crittercism
How Many iOS Versions Released?
www.crittercism.com - @crittercism
iOS 5.0.1 November 10 2011
iOS 5.1 March 7 2012
www.crittercism.com - @crittercism
Android TestingBuild & Test
• Integrated Testing Tools in your IDE• OEMs (developer.samsung.com, developer.motorola.com, etc)• 3rd Party Solutions
• Robotium, MonkeyRunner, Robolectric, etc
• Remote Device Testing• Also offered by OEMs• Device Anywhere, uTest, Mob4Hire, Perfecto Mobile, etc.
• Crashes still make it out into the wild• QA Matrix/Permutations
www.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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 location in code for NPEs seems to be onResume()
– Probably due to devs assuming class members accessible after onPause()
• Data from intents• Peripheral/Sensor data (Camera, GPS, etc)
– Some of these errors can be device/manufacturer specific• Broadcast receivers
www.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
java.lang.OutOfMemoryError
Biggest Culprit: Bitmaps– memory usage
• width px * height px * 4 bytes (for ARGB images) – SoftReferences, recycle– Pre Honeycomb (<3.0) native heap, now in the normal heap
• Use hprof -- if using Eclipse, Memory Analyzer (MAT)
ListViews- Use convertView & viewHolder Pattern
Memory Leak in Dalvik: Keeping a reference to an object longer than needed
www.crittercism.com - @crittercism
java.lang.OutOfMemoryError
• What about the NDK?– In the past some devs built their C++ code on
other platforms, then used tools on that platform 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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
android.view.WindowManager$BadTokenException
Biggest Culprit: Dialogs
• Common error for beginners, creating a dialog via getApplicationContext()– Android docs are wrong, need to create dialog in context of an Activity
www.crittercism.com - @crittercism
android.view.WindowManager$BadTokenException
Biggest Culprit: Dialogs
• Anything that changes the context of the activity will cause issues
– Showing a dialog after activity’s onPause is called (phone call, Back, Home, etc)
– Examples• Screen Rotation during a ProgressBar
– The most common (and not recommended) fix: » Add android:configChanges="orientation” to manifest, just
onConfigurationChanged is called– Recommended:
» save your instance state, re-set any activity context references to the new one• Launch dialog with an old/null context
– Be sure to use DialogFragment, old dialogs are deprecated
www.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
java.lang.IllegalArgumentException(extends java.lang.RuntimeException)
• Common Examples:
– ContentProvider does not exist• i.e. try the Network Location Provider in the emulator• Don’t use undocumented Content Providers
– Using unknown or invalid View IDs• Confusing a string identifier and integer value wrt setValue()
– Activity is finished already, but you call dismiss on a dialog
www.crittercism.com - @crittercism
java.lang.RuntimeException • Common Pitfalls
– Forgetting to add an Activity to the Manifest
– Failing to close a camera service before using it again• Some phones have a race condition with releasing the camera, be sure to make these
calls in surfaceDestroyed1. cameraObject.setPreviewCallback(null); // prevent race condition2. cameraObject.stopPreview();3. cameraObject.release();
– Loading UI elements directly from a background thread
– Trying to use a recycled bitmap
www.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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.crittercism.com - @crittercism
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 connection– Even if you use threads, share one connection
• Prepared Queries with unknown columns is a common mistake
• SQL Syntax Errors
www.crittercism.com - @crittercism
Crashes on iOS & Android
www.crittercism.com - @crittercism
Hard to read pie chart
Crashes on iOS & Android
www.crittercism.com - @crittercism
www.crittercism.com - @crittercism
Andrew Levyandrew@crittercism.com@crittercism and @andrewmlevy
Questions?