31
Developing Fault-Tolerant, “German Engineered” Android Apps Andrew Levy [email protected] CEO, co-founder Crittercism

Crittercism Droidcon Berlin 2012

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Crittercism Droidcon Berlin 2012

Developing Fault-Tolerant, “German Engineered” Android Apps

Andrew [email protected], co-founder Crittercism

Page 2: Crittercism Droidcon Berlin 2012

[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

otherswww.crittercism.com - @crittercism

Page 3: Crittercism Droidcon Berlin 2012

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

Page 4: Crittercism Droidcon Berlin 2012

Android Device Diversity

Example app shortly after launch: has 30k DAU, 175k unique users, 50 different devices

www.crittercism.com - @crittercism

Page 5: Crittercism Droidcon Berlin 2012

iOS Fragmentation

This app has over 850,000 users, 26 diff app versions, 12 different devices

www.crittercism.com - @crittercism

Page 6: Crittercism Droidcon Berlin 2012

How Many iOS Versions Released?

www.crittercism.com - @crittercism

iOS 5.0.1 November 10 2011

iOS 5.1 March 7 2012

Page 7: Crittercism Droidcon Berlin 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

Page 8: Crittercism Droidcon Berlin 2012

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

Page 9: Crittercism Droidcon Berlin 2012

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

Page 10: Crittercism Droidcon Berlin 2012

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

Page 11: Crittercism Droidcon Berlin 2012

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

Page 12: Crittercism Droidcon Berlin 2012

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

Page 13: Crittercism Droidcon Berlin 2012

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

Page 14: Crittercism Droidcon Berlin 2012

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

Page 15: Crittercism Droidcon Berlin 2012

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

Page 16: Crittercism Droidcon Berlin 2012

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

Page 17: Crittercism Droidcon Berlin 2012

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

Page 18: Crittercism Droidcon Berlin 2012

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

Page 19: Crittercism Droidcon Berlin 2012

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

Page 20: Crittercism Droidcon Berlin 2012

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

Page 21: Crittercism Droidcon Berlin 2012

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

Page 22: Crittercism Droidcon Berlin 2012

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

Page 23: Crittercism Droidcon Berlin 2012

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

Page 24: Crittercism Droidcon Berlin 2012

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

Page 25: Crittercism Droidcon Berlin 2012

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

Page 26: Crittercism Droidcon Berlin 2012

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

Page 27: Crittercism Droidcon Berlin 2012

www.crittercism.com - @crittercism

Crashes on iOS & Android

Page 28: Crittercism Droidcon Berlin 2012

www.crittercism.com - @crittercism

Hard to read pie chart

Crashes on iOS & Android

Page 29: Crittercism Droidcon Berlin 2012

www.crittercism.com - @crittercism

Page 30: Crittercism Droidcon Berlin 2012

www.crittercism.com - @crittercism

Page 31: Crittercism Droidcon Berlin 2012

Andrew [email protected]@crittercism and @andrewmlevy

Questions?