How to deal with Fragmentation on Android

10,382 views

Published on

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

3 Comments
18 Likes
Statistics
Notes
No Downloads
Views
Total views
10,382
On SlideShare
0
From Embeds
0
Number of Embeds
244
Actions
Shares
0
Downloads
191
Comments
3
Likes
18
Embeds 0
No embeds

No notes for slide

How to deal with Fragmentation on Android

  1. How to deal with Fragmentation on Android Sittiphol Phanvilai Managing Director Hua Lampong Co.,Ltd.
  2. 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™
  3. MOLOME ™• Share our experience how we beat fragmentation on Android• Who should concern? – Designer – Developer
  4. Agenda• Understand the fragmentation on Android• How to beat it
  5. Android Freedom Pain for Diverse developerHappiness for User
  6. Android Fragmentation “ Android Fragmentation will bethe huge problem for developer ” Sittiphol Phanvilai, 2010
  7. “Problem comes with solution or … ”
  8. Understand the Fragmentation• Fragmentation – No standard – Too many standard• Fragmentation in Android – Software – Hardware
  9. Android Fragmentation• Software And each version of Android has more than 30 generations of ROM 4.1 Jelly Beans
  10. Software Fragmentation• Each Android OS version has 30+ version of ROMs• Some non-Android phone also has custom ROM
  11. API Level• API Level 1-16• Some “must-have” API is available only on high API Level
  12. Android Fragmentation• Hardware – Screen resolution – CPU – GPU – Camera – Sensors
  13. Screen FragmentationScreen Density Screen Size • ldpi • Small screen • mdpi • Normal screen • hdpi • Large screen • xhdpi • Extra Large screen • tvdpi !
  14. Screen Fragmentation … and many more …
  15. CPU Fragmentation• Android compatible CPU architectures – armeabi – armeabi-v7a – x86• This causes some application couldn’t run on some devices
  16. GPU Fragmentation• GPU used on Android – NVIDIA – PowerVR – Mali – Adreno – etc.• This causes some game couldn’t run properly in many devices
  17. Android Fragmentation Visualized http://www.techanalyzer.net/2012/08/22/why-android-fragmentation-never-really-mattered/
  18. Learning Curve• It is very easy to develop easy application
  19. Learning Curve• But it needs very high effort to make the good one
  20. Learning Curve
  21. Thats why …• That’s why – There are a lot of junk apps on Google Play – UI of Android Application is not so beautiful (in average)
  22. Deal with Software Fragmentation Build application based on Android 2.2 Froyo No need to support Android 2.1 Eclair
  23. API Level• FACT: You could compile with higher API level and run in lower OS version <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" />
  24. 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(); }
  25. Deal with Screen Fragmentation320x480 540x960 720x1280 600x1024 800x1280
  26. Deal with Screen Fragmentation Recommended lowest supported screen: normal-mdpi Care a bit for lower but no need Hard-hearted you need to be
  27. 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
  28. Designing Step• Always snap each component to screen or another component• Use – LinearLayout – RelativeLayout – FrameLayout• Don’t fix the position
  29. Snap Component to ScreenSnap to Top of Screen Snap to the rightSnap to the leftSnap to the leftSnap to Bottom of Screen
  30. Snap Component to Screen
  31. Snap Component RelativeLayout
  32. Snap Component ListView
  33. Snap Component LinearLayout - Horizontal - weight 1:60:1:60:1:60:1
  34. Snap ComponentFrameLayout
  35. Never trust in “Pixel”• Actual Pixel on Screen – px Don’t use it!• Physical Size of the Screen – dp, dip (Relative to 160dpi) Recommended for UI – sp Recommended for font – pt (1/72 inch) – in – mm
  36. Different dpi Screen Handling• Unhandled (px)• Handled (dp)
  37. Designing Step: Component SizeFixed dp Screen Width-Related px – Use for the “Exact Size” – To make the design fit component for example any screen button (since your Finger – To please designer is always in the same size) – This will make final product different from design – To please developer
  38. Example
  39. Fixed dp
  40. Fixed dp
  41. Whats actually is in px?
  42. Screen Width-Related px
  43. 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
  44. Screen Width-Related pxWindowManager 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;
  45. Screen Width-Related px
  46. Screen Width-Related px
  47. Screen Height-Related px• Do it for landscape application – Horizontal Scrolling
  48. Considering• How to consider which one to be used? – Purpose of that component – Beautifulness – Emotional ( )
  49. Example
  50. Example
  51. Common Problem with ImageView How to show this image fitting the screen in aspect ratio?
  52. 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" />
  53. Common Problem with ImageView // Inherits ImageView protected 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" />
  54. 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
  55. 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.mk APP_ABI := armeabi armeabi-v7a x86 APP_PLATFORM := android-9
  56. Fragmentation Beaten• Congratulations !
  57. RAM• Each phone has different amount of free RAM• Always use AdapterView. It will help you reduce memory consumption – ListView – Gallery – GridView
  58. 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.
  59. 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
  60. 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
  61. 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
  62. 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
  63. Service• If you need to do some background task, go for Service• Launch when needed only and terminate once the task has finished
  64. Multiple APK• Google Play Store provides feature to use multiple apk• Don’t use it. It will ruin your life in long run.
  65. 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
  66. Custom ROM• Don’t care a lot about phone with Custom ROM. There is always a problem.• Care only major Custom ROM.
  67. 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
  68. Message to Developer Good luck

×