how to deal with fragmentation on android
DESCRIPTION
Slide on "How to deal with Fragmentation on Android" session in GDG DevFest Fri 26, 2012TRANSCRIPT
How to deal with Fragmentationon Android
Sittiphol PhanvilaiManaging Director
Hua Lampong Co.,Ltd.
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™
MOLOME ™
• Share our experience how we beat fragmentation on Android
• Who should concern?– Designer– Developer
Agenda
• Understand the fragmentation on Android
• How to beat it
Android
Diverse
Freedom
Pain for developer
Happiness for User
Android Fragmentation
“Android Fragmentation will be
the huge problem for developer”
Sittiphol Phanvilai, 2010
“Problem comes with solution
or …”
กาก
Understand the Fragmentation
Understand the Fragmentation
• Fragmentation–No standard– Too many standard
• Fragmentation in Android– Software– Hardware
Android Fragmentation
• Software
4.1
Jelly Beans
And each version of Androidhas more than 30 generationsof ROM
Software Fragmentation
• Each Android OS version has 30+ version of ROMs
• Some non-Android phone also has custom ROM
API Level
• API Level 1-16• Some “must-have”
API is available only on high API Level
Android Fragmentation
• Hardware– Screen resolution– CPU– GPU– Camera– Sensors
Screen Fragmentation
Screen Density• ldpi• mdpi• hdpi• xhdpi• tvdpi !
Screen Size• Small screen• Normal screen• Large screen• Extra Large screen
Screen Fragmentation
… and many more …
CPU Fragmentation
• Android compatible CPU architectures– armeabi– armeabi-v7a– x86
• This causes some application couldn’t run on some devices
GPU Fragmentation
• GPU used on Android– NVIDIA– PowerVR– Mali– Adreno– etc.
• This causes some game couldn’t run properly in many devices
Android Fragmentation Visualized
http://www.techanalyzer.net/2012/08/22/why-android-fragmentation-never-really-mattered/
(My) Definition ofAndroid Application
Development
Learning Curve
• It is very easy to develop easy application
Learning Curve
• But it needs very high effort to make the good one
Learning Curve
Why?
90% of development timegoes for UI implementation
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)
Feeling nervous?
Sorry, but you can’t chooseUser has already chosen
So, how to deal withFragmentation?
Deal with Software Fragmentation
Build application based on Android 2.2 Froyo
No need to support Android 2.1 Eclair
API Level
• FACT: You could compile with higher API level and run in lower OS version
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" />
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();}
Deal with Screen Fragmentation
320x480 540x960 720x1280 600x1024 800x1280
Deal with Screen Fragmentation
Recommended lowest supported screen: normal-mdpiCare a bit for lower but no need
Hard-hearted you need to be
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
Designing Step
• Always snap each component to screen or another component
• Use– LinearLayout– RelativeLayout– FrameLayout
• Don’t fix the position
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
Snap Component to Screen
Snap Component
RelativeLayout
Snap Component
ListView
Snap Component
LinearLayout- Horizontal- weight 1:60:1:60:1:60:1
Snap Component
FrameLayout
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
Different dpi Screen Handling
• Unhandled (px)
• Handled (dp)
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
Example
Fixed dp
Fixed dp
What's actually is in px?
Screen Width-Related px
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
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;
Screen Width-Related px
Screen Width-Related px
Screen Height-Related px
• Do it for landscape application– Horizontal Scrolling
Considering
• How to consider which one to be used?– Purpose of that component– Beautifulness– Emotional (อารมณ์�ล้วนๆ)
Example
Example
Common Problem with ImageView
How to show this image fitting the screen in aspect ratio?
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" />
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" />
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
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
Fragmentation Beaten
• Congratulations !
Other concerns
RAM
• Each phone has different amount of free RAM• Always use AdapterView. It will help you
reduce memory consumption– ListView– Gallery– GridView
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.
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
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
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
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
Service
• If you need to do some background task, go for Service
• Launch when needed only and terminate once the task has finished
Multiple APK
• Google Play Store provides feature to use multiple apk
• Don’t use it. It will ruin your life in long run.
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
Custom ROM
• Don’t care a lot about phone with Custom ROM. There is always a problem.
• Care only major Custom ROM.
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
Message to Developer
Good luck
Good luckand
be free from Fragmentation!
The End