39
Android Dev Tips Kanda Runapongsa Saikaew

Android dev tips

Embed Size (px)

DESCRIPTION

Android dev tips

Citation preview

Page 1: Android dev tips

Android Dev Tips Kanda Runapongsa Saikaew

Page 2: Android dev tips

Agenda 1.  Love Relative Layout 2.  Use Hierarchy Viewer 3.  Use Eclipse Effectively 4.  Use LogCat 5.  Publish Application

Page 3: Android dev tips

1. Love Relative Layout •  Most of the tutorials use LinearLayout, but you will find

that RelativeLayout is truly useful •  A common example is the abuse of LinearLayout, which

leads to a proliferation of views in the view hierarchy •  Every view, or worse every layout manager, you add to

your application comes at a cost: initialization, layout and drawing become slower

Page 4: Android dev tips

Use LinearLayout

Page 5: Android dev tips

Use RelativeLayout

Page 6: Android dev tips

2. Use Hierarchy Viewer •  The Android SDK tools include a tool called Hierarchy

Viewer that allows you to analyze your layout while your application is running

•  Hierarchy Viewer works by allowing you to select running processes on a connected device or emulator, then display the layout tree

•  The traffic lights on each block represent its Measure, Layout and Draw performance, helping you identify potential issues.

Page 7: Android dev tips

Using HierarchyViewer •  The hierarchyviewer tool is available in <sdk>/tools/ •  When opened, the Hierarchy Viewer shows a list of

available devices and its running components

Page 8: Android dev tips

Using Hierarchy Viewer •  Click Load View Hierarchy to view the

layout hierarchy of the selected component

Page 9: Android dev tips

Using Hierarchy Viewer -  A small bitmap image on the left -  Two stacked items of text on the right

Page 10: Android dev tips

Using LinearLayout

Page 11: Android dev tips

Using LinearLayout •  There is a 3-level hierarchy with some

problems laying out the text items •  The timings for rendering a complete list

item using this layout are •  Measure: 0.977ms •  Layout: 0.167ms •  Draw: 2.717ms

Page 12: Android dev tips

Using RelativeLayout

Page 13: Android dev tips

Using RelativeLayout •  Because the layout performance above slows down due to

a nested LinearLayout •  The performance might improve by flattening the layout—

make the layout shallow and wide, rather than narrow and deep

•  Now rendering a list item takes •  Measure: 0.598ms •  Layout: 0.110ms •  Draw: 2.146ms

Page 14: Android dev tips

3. Use Eclipse Effectively •  You should try to keep your hands on

keyboard •  The less you touch the mouse, the more

code you can write •  I am trying to keep the mouse laying still and

control the IDE completely using keyboard.

Page 15: Android dev tips

Eclipse Short Cut Keys •  Ctrl + D Delete row •  Ctrl + 1 Activates the quick fix •  Ctrl + Shift + O Organize imports •  Ctrl + Shift + F Format codes •  Ctrl + Shift + L Shows you a list of your

currently defined shortcut keys

Page 16: Android dev tips

4. Use LogCat •  It can be difficult in Android to figure out

“what went wrong”. •  LogCat will show cause of the problems.

o  Error message with red string. o  Problems that cause by …. (something) with line of

that code.

Page 17: Android dev tips

How to Use LogCat •  To use LogCat, first import android.util.Log

into your project •  Now you can call the static class Log from

your project to start logging •  Logcat has different levels of logging

Page 18: Android dev tips

Different Levels of Logging V — Verbose (lowest priority) D — Debug I — Info W — Warning E — Error F — Fatal S — Silent (highest priority, on which nothing is ever printed)

Page 19: Android dev tips

Setting Different Colors for Different Levels

•  Go to Preferences > LogCat > Colors

Page 20: Android dev tips

Example of Using Log Class

Page 21: Android dev tips

How to View LogCat •  Open LogCat view by clicking the LogCat icon at the

bottom right corner (1 in the figure) •  Filter LogCat level (2 in the figure) •  Search for some keyword (3 in the figure)

Page 22: Android dev tips

5. Publish Application •  Prepare the application for release •  Release the application to users

Page 23: Android dev tips

Configure Your Application for Release

•  Choose a good package •  The package name cannot start with com.example

•  Turn off logging and debugging •  Remove Log calls •  Remove android:debuggable attribute from your

manifest file •  Remove all Debug tracing calls such as

startMethodTracing()

Page 24: Android dev tips

Configure Your Application for Release

•  Clean up your directory •  Review the contents of your jni/, lib/, and src/ directories

•  The jni/ directory should contain only source files associated with the Android NDK, such as .c, .cpp, .h, and .mk files

•  The lib/ directory should contain only third-party library files or private library files, including prebuilt shared and static libraries

•  The src/ directory should not contain any .jar files.

Page 25: Android dev tips

Configure Your Application for Release

•  Review and update your manifest settings •  <uses-permission> element

•  You should specify only those permissions that are relevant and required for application

•  android:icon and android:label attributes •  You must specify values for these attributes, which are

located in the <application> element •  android:versionCode and android:versionName attributes.

• We recommend that you specify values for these attributes

Page 26: Android dev tips

Configure Your Application for Release

•  Address compatibility issues •  Add support for multiple screen configurations. •  Optimize your application for Android tablet devices.

•  If your application is designed for devices older than Android 3.0, make it compatible with Android 3.0 devices

•  Consider using the Support Library. •  If your application is designed for devices running Android 3.x,

make your application compatible with older versions of Android

Page 27: Android dev tips

Support Different Devices •  Support different languages •  Support different screens

•  Different layouts •  Different bitmaps •  Different text sizes

Page 28: Android dev tips

Support Different Languages •  Create the resource subdirectories and string resource

files •  Example MyProject/ res/ values/ strings.xml values-es/ strings.xml

Page 29: Android dev tips

Support Different Languages English (default locale), /values/strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">My Application</string> <string name="hello_world">Hello World!</string> </resources> Spanish, /values-es/strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string> </resources>

Page 30: Android dev tips

Support Different Screens •  Android categorizes device screens using two

general properties: size and density •  There are four generalized sizes: small,

normal, large, xlarge •  Four generalized densities: low (ldpi), medium

(mdpi), high (hdpi), extra high (xhdpi)

Page 31: Android dev tips

Support Different Layouts MyProject/ res/ layout/ # default (portrait) main.xml layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml

Page 32: Android dev tips

Support Different Bitmaps •  To generate these images, you should start with your raw resource

in vector format and generate the images for each density using the following size scale: •  xhdpi: 2.0 •  hdpi: 1.5 •  mdpi: 1.0 (baseline) •  ldpi: 0.75

•  This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.

Page 33: Android dev tips

Support Different Bitmaps Then, place the files in the appropriate drawable resource directory: MyProject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png Any time you reference @drawable/awesomeimage, the system selects the

appropriate bitmap based on the screen's density.

Page 34: Android dev tips

Support Different Text Sizes •  You should use the resource folders such as values-ldpi values-mdpi values-hdpi •  Write the text size in 'dimensions.xml' file for each range Sample dimensions.xml <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textsize">15sp</dimen> </resources> •  In Java code, textView.setTextSize(getResources().getDimension(R.dimen.textsize));

Page 35: Android dev tips

What to Test •  Change in orientation

•  Is the screen re-drawn correctly? •  Does the application maintain its state?

•  Change in configuration •  A situation that is more general than a change in

orientation is a change in the device's configuration, such as a change in the availability of a keyboard or a change in system language

Page 36: Android dev tips

What to Test •  Battery Life

•  You need to write your application to minimize battery usage, you need to test its battery performance, and you need to test the methods that manage battery usage.

•  Techniques for minimizing battery usage were presented at the 2010 Google I/O conference in the presentation Coding for Life -- Battery Life, That Is.

Page 37: Android dev tips

What to Test •  Dependence on external resources

•  If your application depends on network access, SMS, Bluetooth, or GPS, then you should test what happens when the resource or resources are not available

•  For example, if your application uses the network, it can notify the user if access is unavailable, or disable network-related features, or do both

Page 38: Android dev tips

References •  http://stackoverflow.com/questions/2961049/effective-android-programming-

techniques

•  http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/ •  http://www.curious-creature.org/2012/12/01/android-performance-case-study/ •  http://developer.android.com/training/improving-layouts/optimizing-layout.html •  http://eclipse.dzone.com/news/effective-eclipse-shortcut-key •  http://developer.android.com/tools/testing/what_to_test.html •  http://developer.android.com/tools/publishing/preparing.html •  http://stackoverflow.com/questions/9494037/how-to-set-text-size-of-textview-

dynamically-for-diffrent-screens •  http://developer.android.com/training/basics/supporting-devices/screens.html

Page 39: Android dev tips

Thank you Kanda Runapongsa Saikaew •  Khon Kaen University, Thailand

•  Assistant Professor of Department of Computer Engineering •  Associate Director for Administration of Computer Center

•  [email protected] •  Twitter: @krunapon •  G+: https://plus.google.com/u/0/118244887738724224199