how to deal with fragmentation on android

80
How to deal with Fragmentation on Android Sittiphol Phanvilai Managing Director Hua Lampong Co.,Ltd.

Upload: sittiphol-phanvilai

Post on 11-May-2015

5.420 views

Category:

Documents


3 download

DESCRIPTION

Slide on "How to deal with Fragmentation on Android" session in GDG DevFest Fri 26, 2012

TRANSCRIPT

Page 1: How to deal with Fragmentation on Android

How to deal with Fragmentationon Android

Sittiphol PhanvilaiManaging Director

Hua Lampong Co.,Ltd.

Page 2: How to deal with Fragmentation on Android

Who am I

• Sittiphol Phanvilai• nuuneoi• Cross Platform Mobile

Application Developer• Managing Director at

Hua Lampong Co.,Ltd.• Founder of DroidSans.com

the Android Community• Founder of MOLOME™

Page 3: How to deal with Fragmentation on Android

MOLOME ™

• Share our experience how we beat fragmentation on Android

• Who should concern?– Designer– Developer

Page 4: How to deal with Fragmentation on Android

Agenda

• Understand the fragmentation on Android

• How to beat it

Page 5: How to deal with Fragmentation on Android

Android

Diverse

Freedom

Pain for developer

Happiness for User

Page 6: How to deal with Fragmentation on Android

Android Fragmentation

“Android Fragmentation will be

the huge problem for developer”

Sittiphol Phanvilai, 2010

Page 7: How to deal with Fragmentation on Android

“Problem comes with solution

or …”

Page 8: How to deal with Fragmentation on Android

กาก

Page 9: How to deal with Fragmentation on Android

Understand the Fragmentation

Page 10: How to deal with Fragmentation on Android

Understand the Fragmentation

• Fragmentation–No standard– Too many standard

• Fragmentation in Android– Software– Hardware

Page 11: How to deal with Fragmentation on Android

Android Fragmentation

• Software

4.1

Jelly Beans

And each version of Androidhas more than 30 generationsof ROM

Page 12: How to deal with Fragmentation on Android

Software Fragmentation

• Each Android OS version has 30+ version of ROMs

• Some non-Android phone also has custom ROM

Page 13: How to deal with Fragmentation on Android

API Level

• API Level 1-16• Some “must-have”

API is available only on high API Level

Page 14: How to deal with Fragmentation on Android

Android Fragmentation

• Hardware– Screen resolution– CPU– GPU– Camera– Sensors

Page 15: How to deal with Fragmentation on Android

Screen Fragmentation

Screen Density• ldpi• mdpi• hdpi• xhdpi• tvdpi !

Screen Size• Small screen• Normal screen• Large screen• Extra Large screen

Page 16: How to deal with Fragmentation on Android

Screen Fragmentation

… and many more …

Page 17: How to deal with Fragmentation on Android

CPU Fragmentation

• Android compatible CPU architectures– armeabi– armeabi-v7a– x86

• This causes some application couldn’t run on some devices

Page 18: How to deal with Fragmentation on Android

GPU Fragmentation

• GPU used on Android– NVIDIA– PowerVR– Mali– Adreno– etc.

• This causes some game couldn’t run properly in many devices

Page 19: How to deal with Fragmentation on Android

Android Fragmentation Visualized

http://www.techanalyzer.net/2012/08/22/why-android-fragmentation-never-really-mattered/

Page 20: How to deal with Fragmentation on Android

(My) Definition ofAndroid Application

Development

Page 21: How to deal with Fragmentation on Android

Learning Curve

• It is very easy to develop easy application

Page 22: How to deal with Fragmentation on Android

Learning Curve

• But it needs very high effort to make the good one

Page 23: How to deal with Fragmentation on Android

Learning Curve

Page 24: How to deal with Fragmentation on Android

Why?

Page 25: How to deal with Fragmentation on Android

90% of development timegoes for UI implementation

Page 26: How to deal with Fragmentation on Android

That's why …

• That’s why– There are a lot of junk apps on Google Play– UI of Android Application is not so beautiful (in

average)

Page 27: How to deal with Fragmentation on Android

Feeling nervous?

Page 28: How to deal with Fragmentation on Android

Sorry, but you can’t chooseUser has already chosen

Page 29: How to deal with Fragmentation on Android

So, how to deal withFragmentation?

Page 30: How to deal with Fragmentation on Android

Deal with Software Fragmentation

Build application based on Android 2.2 Froyo

No need to support Android 2.1 Eclair

Page 31: How to deal with Fragmentation on Android

API Level

• FACT: You could compile with higher API level and run in lower OS version

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" />

Page 32: How to deal with Fragmentation on Android

How to run new API on old OS

• Sorry you couldn’t but you could do the trick

if (android.os.Build.VERSION.SDK_INT > 8){

camera = Camera.open(camera_id);}else{

camera = Camera.open();}

Page 33: How to deal with Fragmentation on Android

Deal with Screen Fragmentation

320x480 540x960 720x1280 600x1024 800x1280

Page 34: How to deal with Fragmentation on Android

Deal with Screen Fragmentation

Recommended lowest supported screen: normal-mdpiCare a bit for lower but no need

Hard-hearted you need to be

Page 35: How to deal with Fragmentation on Android

Multiple drawable

• Android provides mechanic to use different resource for different dpi / screen size

• I suggest you to go for drawable-hdpi only, others will be scaled automatically

• As same as layout• Unless you have to

Page 36: How to deal with Fragmentation on Android

Designing Step

• Always snap each component to screen or another component

• Use– LinearLayout– RelativeLayout– FrameLayout

• Don’t fix the position

Page 37: How to deal with Fragmentation on Android

Snap Component to ScreenSnap to Top of Screen

Snap to the left

Snap to the right

Snap to Bottom of Screen

Snap to the left

Page 38: How to deal with Fragmentation on Android

Snap Component to Screen

Page 39: How to deal with Fragmentation on Android

Snap Component

RelativeLayout

Page 40: How to deal with Fragmentation on Android

Snap Component

ListView

Page 41: How to deal with Fragmentation on Android

Snap Component

LinearLayout- Horizontal- weight 1:60:1:60:1:60:1

Page 42: How to deal with Fragmentation on Android

Snap Component

FrameLayout

Page 43: How to deal with Fragmentation on Android

Never trust in “Pixel”

• Actual Pixel on Screen– px

• Physical Size of the Screen– dp, dip (Relative to 160dpi)

– sp– pt (1/72 inch)

– in– mm

Don’t use it!

Recommended for font

Recommended for UI

Page 44: How to deal with Fragmentation on Android

Different dpi Screen Handling

• Unhandled (px)

• Handled (dp)

Page 45: How to deal with Fragmentation on Android

Designing Step: Component Size

Fixed dp– Use for the “Exact Size”

component for example button (since your Finger is always in the same size)

– This will make final product different from design

– To please developer

Screen Width-Related px– To make the design fit

any screen– To please designer

Page 46: How to deal with Fragmentation on Android

Example

Page 47: How to deal with Fragmentation on Android

Fixed dp

Page 48: How to deal with Fragmentation on Android

Fixed dp

Page 49: How to deal with Fragmentation on Android
Page 50: How to deal with Fragmentation on Android

What's actually is in px?

Page 51: How to deal with Fragmentation on Android

Screen Width-Related px

Page 52: How to deal with Fragmentation on Android

Screen Width-Related px

• Inherit View or Layout and set the size on initializing

• Override onLayout if you wish to manage the children’s position manually

• Don’t override onMeasure if not neccessary

Page 53: How to deal with Fragmentation on Android

Screen Width-Related px

WindowManager wm =(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

Display display = wm.getDefaultDisplay();int width = display.getWidth();

View v = (View)findViewById(R.id.view);v.getLayoutParams().width = screenWidth / 3;

Page 54: How to deal with Fragmentation on Android

Screen Width-Related px

Page 55: How to deal with Fragmentation on Android

Screen Width-Related px

Page 56: How to deal with Fragmentation on Android

Screen Height-Related px

• Do it for landscape application– Horizontal Scrolling

Page 57: How to deal with Fragmentation on Android

Considering

• How to consider which one to be used?– Purpose of that component– Beautifulness– Emotional (อารมณ์�ล้วนๆ)

Page 58: How to deal with Fragmentation on Android

Example

Page 59: How to deal with Fragmentation on Android

Example

Page 60: How to deal with Fragmentation on Android

Common Problem with ImageView

How to show this image fitting the screen in aspect ratio?

Page 61: How to deal with Fragmentation on Android

Common Problem with ImageView<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/img" />

Page 62: How to deal with Fragmentation on Android

Common Problem with ImageView// Inherits ImageViewprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {if (getDrawable() == null){

setMeasuredDimension(0, 0);return;

}int width = MeasureSpec.getSize(widthMeasureSpec);setMeasuredDimension(width, width *

getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth());

}

// xml<com.hlpth.fragmentation.AspectRatioImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/img" />

Page 63: How to deal with Fragmentation on Android

More Common Problem

• Please note that you will see this kind of problem a lot on Android Application Development

• You need to check one by one unavoidably

Page 64: How to deal with Fragmentation on Android

Deal with CPU Fragmentation

• Directly effect Native Code (NDK)• Don’t use any platform-specific code or

assembly code• Compile for every single CPU architecture

// Application.mkAPP_ABI := armeabi armeabi-v7a x86APP_PLATFORM := android-9

Page 65: How to deal with Fragmentation on Android

Fragmentation Beaten

• Congratulations !

Page 66: How to deal with Fragmentation on Android

Other concerns

Page 67: How to deal with Fragmentation on Android

RAM

• Each phone has different amount of free RAM• Always use AdapterView. It will help you

reduce memory consumption– ListView– Gallery– GridView

Page 68: How to deal with Fragmentation on Android

Memory Leak

• First reason of app crashing in large-scale application

• Don’t 100% trust in Garbage Collector• Use Context wisely, it holds all of UI component

inside. If there is still some variable point to context, say hello to memory leak

• Use Heap monitoring tools comes with ADT plugin. Your life will be easier.

Page 69: How to deal with Fragmentation on Android

Bitmap.recycle()

• If you fully take control the Bitmap object, don’t forget to recycle() it when you don’t need it anymore.

• Not necessary, just recommended

Page 70: How to deal with Fragmentation on Android

Nested Layout

• Don’t implement more than 6 levels of nested Layout

• There is always the way to reduce level of nested Layout. Just need to put more effort.

• LayoutInflater and <merge> is one of the key to implement complicated UI with high performance

Page 71: How to deal with Fragmentation on Android

Caching

• Normally there is so limited amount of Storage

• But some has so many• If you wish to cache anything, limit cache size

to 2-4 MB

Page 72: How to deal with Fragmentation on Android

OpenGL

• OpenGL could limit number of target devices, don’t use it unless you have to.

• Better use OpenGL 1.1 over OpenGL 2.0

Page 73: How to deal with Fragmentation on Android

Service

• If you need to do some background task, go for Service

• Launch when needed only and terminate once the task has finished

Page 74: How to deal with Fragmentation on Android

Multiple APK

• Google Play Store provides feature to use multiple apk

• Don’t use it. It will ruin your life in long run.

Page 75: How to deal with Fragmentation on Android

Target Devices

• You can’t please everybody. If you have to scope down the number of target devices, do it with hard-hearted.

• To make your application fully compatible with 3000 THB phone might costs your time double

Page 76: How to deal with Fragmentation on Android

Custom ROM

• Don’t care a lot about phone with Custom ROM. There is always a problem.

• Care only major Custom ROM.

Page 77: How to deal with Fragmentation on Android

Message to Designer

• Don’t expect what you design and the final product will be 100% exactly

• You could expect that on iOS but not on Android

• Designer DO need to know Android’s UI guideline before design or it might not be able to implement

Page 78: How to deal with Fragmentation on Android

Message to Developer

Good luck

Page 79: How to deal with Fragmentation on Android

Good luckand

be free from Fragmentation!

Page 80: How to deal with Fragmentation on Android

The End