47
Mobile2Days The Good, The Bad, and The Ugly… Things to do with Android Stanojko Markovik 03.11.2011@Java2Days

The Good, the Bad and the Ugly things to do with android

Embed Size (px)

DESCRIPTION

The Java2Days 2011 conference Android presentation. The Good, the Bad and the Ugly things to do with android

Citation preview

Page 1: The Good, the Bad and the Ugly things to do with android

Mobile2Days

The Good, The Bad, and The Ugly… Things to do with Android

Stanojko Markovik

03.11.2011@Java2Days

Page 2: The Good, the Bad and the Ugly things to do with android

Your presenter…

• Speaks a few languages (Java, C++, Python…)

• Currently works at the ARTEMIS R&D department at SudParis Telecom Institute

• Worked with android from v1.1

both for app development and custom ROMs

• Linux geek

• JUGMK member

Page 3: The Good, the Bad and the Ugly things to do with android

While developing for Android you can do…

Page 4: The Good, the Bad and the Ugly things to do with android

Good stuff

Page 5: The Good, the Bad and the Ugly things to do with android

Bad Stuff

Page 6: The Good, the Bad and the Ugly things to do with android

UGLY stuff…

Page 7: The Good, the Bad and the Ugly things to do with android

• Good practices and the proper way to do things 1

• Bad things people tend to do and how to evade… 2

• The ugly things no one wants to talk about. 3

What will we talk about??

Page 8: The Good, the Bad and the Ugly things to do with android

THE GOOD

Page 9: The Good, the Bad and the Ugly things to do with android

The standard stuff first

• Always keep Clean and concise code

• DRY KISS - Always

• Be weary of memory usage

• Try to use libraries as external JAR files. - modularity - easier development sloop

• If you comment something out.. Erase it.. You will probably never use it again

Page 10: The Good, the Bad and the Ugly things to do with android

Android Specific stuff - UI

• Android uses a modified version of the MVC patter, Views are made in XML. Populated by code

• Android is aware of the display size. So use this, and have different graphics. For each size of the screen

Page 11: The Good, the Bad and the Ugly things to do with android

Android Specific stuff - UI

• Try to utilize the 9patch drawables as much as possible

Example 1 Example 2

Page 12: The Good, the Bad and the Ugly things to do with android

Android Specific stuff - UI

• If the shapes are simple.. Write them in XML

Sample Round shape definition in XML

Page 13: The Good, the Bad and the Ugly things to do with android

Android Specific stuff - Code

• Differentiate the LinkedList from the ArrayList in the mobile world, it matters…

• Android uses a Google re-written Java, so most of the standard java classes you use are “optimized”, optimizations have bugs .

• Cleanup code before using it.. The Android compiler does not exclude unused classes. This will greatly reduce your application size

Page 14: The Good, the Bad and the Ugly things to do with android

Android Specific stuff - SDK

• Always have a target SDK, and a target device. Maybe the code can be uniform and meant for all device, but the UI cannot.

• Forget String-s use StringBuilders

• Use a Debug condition for your Log statements. Remember that : logs concat strings and are NOISY!

• Android has a GC, but System.gc() doesn’t work! Have it in mind…

Page 15: The Good, the Bad and the Ugly things to do with android

Android Specific stuff Visual Scaling

• Try to get used to thinking in DIPs and SP.

• A good developer always wraps his content!

• Android has a GC, but System.gc() doesn’t work! Have it in mind…

Page 16: The Good, the Bad and the Ugly things to do with android

View.getTag() method

This is the “hidden” pocket meant for the object to which the view is refering

• Use it on buttons to hold a state, or reference

• Use it with cursor adapters to hold an object or database ID

• Use it with custom views to hold misc data for the extra data you need.

Page 17: The Good, the Bad and the Ugly things to do with android

notifyChange() method

Very often you have to write applications that use adapters from databases or webservices.

The notifyChange() method is very important for automatic update and/or content change.

Whenever you insert/update any data in the source it is important to call this method to tell all the users to refresh or re-query their data.

Page 18: The Good, the Bad and the Ugly things to do with android

THE BAD…

Page 19: The Good, the Bad and the Ugly things to do with android

Developers tend to be lazy… • Respect The Activity Lifecycle

Page 20: The Good, the Bad and the Ugly things to do with android

Database Cursors

• Database connections are often left open or half open

- This wreaks havoc in the android heap and memory management, Never forget to cursor.close()

- If this is hard, then either call the manageCursor to link the cursor to the activity lifecycle or use cursor helper.

Page 21: The Good, the Bad and the Ugly things to do with android

Use XML resources with care

XML layouts are a very handy, but developers over or under use them very often.

- Handle the system services (layoutInflater)

- Inflate to combine layouts, much better and easier than writing it in code.

- Don’t forget to recycle the Inflated layouts !

- Externalize ALL Strings to strings.xml

- Use values for system configurations

Page 22: The Good, the Bad and the Ugly things to do with android

Don’t over nest

“Lazy” developers tend to overuse the LinearLayout

Instead of using RelativeLayout

Eclipse Demo here…

Page 23: The Good, the Bad and the Ugly things to do with android

Cursor adapters and lists

• Use relative layout for the list items

• Use recyclable views and adapter

• Load images in the backgroud

Eclipse Demo here…

Page 24: The Good, the Bad and the Ugly things to do with android

Background and Services

• Differentiate when to use binded and unbinded services.

• Understand the onStartCommand() method and it’s return statement.

• Differentiate threads and asynctasks

• Never use Toast or Dialog from a service

Page 25: The Good, the Bad and the Ugly things to do with android

View Hierarchy

Developers tend to create complex view hierarchies which are difficult for the GPU to render and refresh.

Views should be simple and a better resolution is for the view hierarchy to be wide than tall.

Use the HierarchyViewer tool to find the skeleton of your activity.

Page 26: The Good, the Bad and the Ugly things to do with android

View Hierarchy GOOD example

Page 27: The Good, the Bad and the Ugly things to do with android

View Hierarchy BAD example

Page 28: The Good, the Bad and the Ugly things to do with android

THE UGLY…

Page 29: The Good, the Bad and the Ugly things to do with android

ANR

• Affected : Activity, Service, BroadcastReceiver

• Reason : Lifecycle rules broken

• Fix Run heavy processing outside of the main thread, use asynctask or another thread.

Page 30: The Good, the Bad and the Ugly things to do with android

Fantom Apps The error

Sample LogCat output.

Page 31: The Good, the Bad and the Ugly things to do with android

Fantom Apps - The Cause

In general this is a very difficult error to trace

- No Java StackTrace - No Error screen However if you look at the debug dump, you can see That the heap is being overflowed. To check Runtime.getRuntime().maxMemory() – heap size

Page 32: The Good, the Bad and the Ugly things to do with android

Fantom Apps - The Solution

VMRuntime .getRuntime().setMinimumHeapSize(BIGGER_SIZE) (how googers help them self with memory) Or: - Avoid operations that require threadpools.

Control your background processes. - Try to find out if you overuse strings since + is a

very heavy duty operation! - Use MemoryAnalyzer to find the “memory hoag”

Page 33: The Good, the Bad and the Ugly things to do with android

Bitmaps and Graphics

Android 2.x does not work with bitmaps well

• VM does not load Bitmaps in heap,

• Bitmaps are immutable

• Matrix transformations are EXTREMELY risky

Page 34: The Good, the Bad and the Ugly things to do with android

Bitmaps and Graphics

Repetitive problems

ERROR/AndroidRuntime(7906): java.lang.OutOfMemoryError: bitmap size exceeds VM budget ERROR/AndroidRuntime(7906): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:295)

Page 35: The Good, the Bad and the Ugly things to do with android

Other Memory issues

This is actually caused by unclosed cursors and huge bitmaps

java.lang.RuntimeException: No memory in memObj at android.database.CursorWindow.native_init at android.database.CursorWindow.<init> at android.database.CursorWindow.<init> at android.database.CursorWindow$1.createFromParcel at android.database.CursorWindow$1.createFromParcel at android.content.ContentProviderNative.onTransact at android.os.Binder.execTransact at dalvik.system.NativeStart.run

Page 36: The Good, the Bad and the Ugly things to do with android

The problem

You are stranded with a slow application.

• Memory is failing

• Application is Slow

• Sometimes it fails

• Sometimes it just “dissapears”

Page 37: The Good, the Bad and the Ugly things to do with android

The tools of trade!

• TraceView

• Eclipse MAT

• DDMS system

Page 38: The Good, the Bad and the Ugly things to do with android

TraceView

Page 39: The Good, the Bad and the Ugly things to do with android

Eclipse MAT

Page 40: The Good, the Bad and the Ugly things to do with android

DDMS console

Page 41: The Good, the Bad and the Ugly things to do with android

Threads vs. Async Tasks

• Async Task Queue Limit

• Thread overrun

• AsyncTaskEx

• NeverEnding cursors

Page 42: The Good, the Bad and the Ugly things to do with android

Again the notifyChange() method

This can sometimes wreak havoc.

An example (that happened to me):

I download news from a website, upon download I have a list of 50 items. NotifyChange() is called 50 times!

Definition of an UGLY SCENARIO

Page 43: The Good, the Bad and the Ugly things to do with android

The logging facility

Android Log is great, but People tend to

1. Overlog

2. Forget the logs

3. Concatenate a huge amount of strings

4. Leave logs in loops (for, while, do-while)

5. Add stupid tags

6. Offer the consumer private information

Page 44: The Good, the Bad and the Ugly things to do with android

CONCLUSION…

Page 45: The Good, the Bad and the Ugly things to do with android

It’s Hard

No, It’s not!

It’s fun!

It’s easy!

It’s lucrative

It’s cool!

New Employee

1 yr 2 yr 3 yr

Page 46: The Good, the Bad and the Ugly things to do with android

You only have to : 1. Be aware of the rules of engagement!

2. Know your target

3. Be weary of memory!

4. Remember that it’s a VM

5. Have Respect for the other platforms, and you just might learn something new…

Page 47: The Good, the Bad and the Ugly things to do with android

Q&A

CONTACT ME AT : [email protected] HTTP://TWITTER.COM/SMARKOVIK