Android Application Development DevFest event 2012 @ Pear Continental, Karachi

68
Android Application Development DevFest event 2012 @Pear Continental, Karachi Presenter: Imam Raza

description

Android Application Development DevFest event 2012 @ Pear Continental, Karachi Presenter: Imam Raza. Speaker.bio.toString (). Senior Software Architect @ Folio3. Specialties: Enterprise Software Architecture, Mobile Software Architecture, Software Best Practices(TDD,CI ,AOP, IOC). - PowerPoint PPT Presentation

Transcript of Android Application Development DevFest event 2012 @ Pear Continental, Karachi

Page 1: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Android Application DevelopmentDevFest event 2012

@Pear Continental, Karachi

Presenter: Imam Raza

Page 2: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Speaker.bio.toString()

Monday, September 19, 2011

• Senior Software Architect @ Folio3.• Specialties: Enterprise Software

Architecture, Mobile Software Architecture, Software Best Practices(TDD,CI ,AOP, IOC).

• Master in computer science from KU• B.E (Mechanical) from NED University

Page 3: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Me.loveQuestions==true

Monday, September 19, 2011

Page 4: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Monday, September 19, 2011

رفعنالک ذکرکو

Page 5: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Agenda• Market Statistics• The Android Work in Pakistan• Android Basics• Hello World• Main Building Blocks• Android Best Practices

Page 6: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Agenda• Market Statistics• The Android Work in Pakistan• Android Basics• Hello World• Main Building Blocks• Android Best Practices

Page 7: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

SmartPhone Vs PC sales 2011Category Q4 2011

shipments(millions)

Growth Q4’11/Q4’10

Full year 2011 shipments(millions)

Growth2011/2010

Smart Phones

158.5 56.6% 487.7 62.7%

Total Client Pcs

120.2 16.3% 414.6 14.8%

-pads 26.5 186.2% 63.2 274.2%

-netbooks 6.7 -32.4% 29.4 -25.3%

-notebooks 57.9 7.3% 209.6 7.5%

-Desktops 29.1 -3.6% 112.4 2.3%

Page 8: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Market Share Q2 2012Smart Phone %percentage Unit sold (millions)

Android 68.1% 104.8

iOS 16.9% 26

Black Berry 4.8% 7.4

Symbian 4.4% 6.8

Windows 3.5% 5.4

Page 9: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

AgendaMarket StatisticsThe Android Work in PakistanAndroid BasicsHello WorldMain Building BlocksAndroid Best Practices

Page 10: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Android Apps• Myomo MyProgress • Sony Socom Android App• Bitzer• NSDroid (NetSuite CRM)• Hiplink

Page 11: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Myomo

Page 12: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi
Page 13: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Sony Socom App

Page 14: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

BEAM(Bitzer Enterprise)

Page 15: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

NSDroid(NetSuite CRM)

Page 16: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Hiplink

Page 17: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

AgendaMarket StatisticsThe Android Work in PakistanAndroid BasicsHello WorldMain Building BlocksAndroid Best Practices

Page 18: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Android Stack

Page 19: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

The Stack

Page 20: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Linux Kernel• Android runs on Linux.

Linux provides: – Hardware

abstraction layer – Memory

management – Process

management – Networking

• Users never see Linux sub system

• The adb shell command opens Linux shell

Page 21: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Native Libraries• Pieces borrowed from

other open source projects:

– Bionic, a super fast and small license-friendly libc library optimized for Android

• WebKit library for fast HTML rendering

• OpenGL for graphics • Media codecs offer

support for major audio/video codecs

• SQLite database ..Much more…

Page 22: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Question: Difference between Java VM and Dalvik VM?

Page 23: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Dalvik VM• Dalvik VM is Android implementation of

Java VM • Dalvik is optimized for mobile devices:

–   Battery consumption –   CPU capabilities

• Key Dalvik differences: – Register-based versus stack-based VM – Dalvik runs .dex files – More efficient and compact

implementation – Different set of Java libraries than JDK

Page 24: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Application Framework• The rich set of system services wrapped

in an intuitive Java API. • This ecosystem that developers can

easily tap into is what makes writing apps for Android easy.

• Location, web, telephony, WiFi, Bluetooth, notifications, media, camera, just to name a few.

Page 25: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Applications

Page 26: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Applications• Dalvik Executable

+ Resources = APK

• Must be signed (but debug key is okay

for development) • Many markets

with different policies

Page 27: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Question: What is the difference between Android and Java?

Page 28: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Android and JavaAndroid Java = Java SE – AWT/Swing + Android API

Page 29: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Android SDK- what is in box?

SDK Tools Docs Platforms

– Data – Skins – Images – Sample

sAdd-ons

– Google

Page 30: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

AgendaMarket StatisticsThe Android Work in PakistanAndroid BasicsHello WorldMain Building BlocksAndroid Best Practices

Page 31: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Hello World-Create New ProjectUse the Eclipse tool to create a new

Android project. Here are some key constructs:

Page 32: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Step-1

Page 33: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Step-2

Page 34: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Step-3

Page 35: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Step-4

Page 36: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Hello World-Anatomy of App

Java Code + XML / OtherResources + Manifest File = Android App

Page 37: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

HelloWorld-Manifest File

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.folio3" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloworldActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

Page 38: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

HelloWorld- Layout Resource File<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>

Page 39: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

HelloWorld-JAVA Filepackage com.folio3; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }

Page 40: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

AgendaMarket StatisticsThe Android Work in PakistanAndroid BasicsHello WorldMain Building BlocksAndroid Best Practices

Page 41: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Main Building Blocks• Activities• Intents• Services• Content Providers• Broadcast Receivers• Fragments

Page 42: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

ActivitiesAn activity represents a screen or windows

Page 43: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Activity LifeCycleActivity have well-defined lifecycle. The android OS manages your activity by changing its state.You fill in the blanks

Page 44: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Intents• Intents represent

events or actions. • They are to

Android apps what hyperlinks are to websites. Sort of.

• Intents can be implicit or explicit.

Page 45: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

ServicesServices are code that runs in the

background. They can be started and stopped. Services doesn’t have UI.

Page 46: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Service LifeCycle• Service also has a

lifecycle, but it’s much simpler than activity’s.

• An activity typically starts and stops a service to do some work for it in the background, such as play music, check for new tweets, etc.

Page 47: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Content Provider• Content Providers

share content with

applications across application boundaries. • Examples of built-in Content Providers are:

– Contacts, – MediaStore, – Settings and

more.

Page 48: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Broadcast ReceiversAn Intent-based publish-subscribe

mechanism. Great for listening system events such as SMS messages.

Page 49: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Fragments A Fragment represents a behavior or a

portion of user interface in an Activity.

Page 50: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Fragments• Fragments were introduced in Android

3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets.

• Fragments are lot like an activity but it must exists within the activity.

• DialogFragment makes it easy to show a Dialog that is managed as part of the Activity lifecycle.

• ListFragment makes it easy to show a list of data.

Monday, September 19, 2011

Page 51: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Monday, September 19, 2011

Page 52: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

AgendaMarket StatisticsThe Android Work in PakistanAndroid BasicsHello WorldMain Building BlocksAndroid Best Practices

Page 53: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Best Practices• Use RoboGuice(DI based framework)• Learn Activity Life Cycle• Avoid getting activities thick• Design views for multiple

size/orientation.• Use Fragments to better manage sub-

portion of Activity.• Practice Good MVC. • Use Source Code Analyzer Tools

(findbugs, checkstyle,PMD and CPD). Integrate these tools with CI Tools like Teamcity.

Monday, September 19, 2011

Page 54: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

RoboGuice• It’s based on dependency injection

pattern just like Spring Framework in enterprise apps

• It takes the guesswork out of development. e.g checking null for getIntent().getExtras(). Casting findViewById().

• Make your writing unit test case easy• It reduces your lines of code and hence

the number of bugs see next slide for code.

Monday, September 19, 2011

Page 55: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

class AndroidWay extends Activity {     TextView name;     ImageView thumbnail;     LocationManager loc;     Drawable icon;     String myName;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name      = (TextView) findViewById(R.id.name);         thumbnail = (ImageView) findViewById(R.id.thumbnail);         loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);         icon      = getResources().getDrawable(R.drawable.icon);         myName    = getString(R.string.app_name);         name.setText( "Hello, " + myName );     } }

Monday, September 19, 2011

Page 56: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

class RoboWay extends RoboActivity {     @InjectView(R.id.name)             TextView name;     @InjectView(R.id.thumbnail)        ImageView thumbnail;     @InjectResource(R.drawable.icon)   Drawable icon;     @InjectResource(R.string.app_name) String myName;     @Inject                            LocationManager loc;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name.setText( "Hello, " + myName );     } }

Monday, September 19, 2011

Page 57: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Compatibility• Ability to install and run app on device.• Huge variety of devices so developer

need to make sure about the Hardware/Software feature his application needed to run.

Monday, September 19, 2011

Page 58: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Compatibility• Specify uses-feature node for every

API you use• Mark essential features as required.• Mark optional features as not required.<uses-featureAndroid:name=“android.hardware.g

ps”Android:required=“true”/>Monday, September 19,

2011

Page 59: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Compatibility• Check for API existence in code.PackageManager pm = getPackageManager();Boolean hasCompass=

pm.hasSystemFeature( PackageManager.FEATURE_SENSOR_COMPASS)

;If(hasCompass){//enable things are needed}Monday, September 19,

2011

Page 60: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

CompatibilityUse dp and sp instead of px:<Button

android:layout_width=“wrap_content”

Android:layout_height=“wrap_content”

Android:layout_marginTop=“20dp”/><TextView

android:layout_width=“match_parent”

Android:layout_height=“wrap_content”Android:textsize=“20sp”/>

Monday, September 19, 2011

Page 61: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Compatibility

Monday, September 19, 2011

Page 62: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Advance Task Killer App is among 50

millions or more install apps

Why?

Monday, September 19, 2011

Page 63: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Performance • Avoid creating objects. (e.g use

StringBuffer) .• Prefer static over virtual.• Use static final for constants.• Avoid internal getter/Setter (with

Proguard you don’t need it.)• Use Enhanced For Loop Syntax e.g:

for (Foo a : mArray) { sum += a.mSplat; }• Use native methods. • Avoid using Float and enums

Page 64: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Responsiveness

Monday, September 19, 2011

Page 65: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Responsiveness• “Application Not Responding”

• Respond to user within 5 seconds• Broadcast Receiver must complete within

10 seconds• Use Threads and AsyncTasks within

Services

Monday, September 19, 2011

Page 66: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Source Code Analyzer Tools• FindBugs• PMD• CheckStyle • CPD : Shows code duplication• You can integrate them with CI Server

like Teamcity to get consolidated reports of code quality of your team. We have been using it on our company and its really helped us in monitoring quality of code.

Monday, September 19, 2011

Page 67: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Monday, September 19, 2011

Page 68: Android Application Development DevFest  event 2012 @ Pear Continental, Karachi

Questions