roy clarkson & josh longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • roy...

73
Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development Practices Saturday, October 29, 11

Upload: others

Post on 20-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

• Roy Clarkson & Josh LongSpringSource, a division of VMware

1

Native Android Development Practices

Saturday, October 29, 11

Page 2: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

@royclarkson

2

About Roy Clarkson (Spring Android Lead)

Saturday, October 29, 11

Page 3: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

@royclarkson

2

About Roy Clarkson (Spring Android Lead)

Saturday, October 29, 11

Page 4: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

@royclarkson

2

About Roy Clarkson (Spring Android Lead)

Saturday, October 29, 11

Page 5: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

@starbuxman

3

About Josh Long (Spring Developer Advocate)

[email protected]

Saturday, October 29, 11

Page 6: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Mobile

• Provides support for developing mobile web applications– Builds on Spring MVC, focuses on server-side support– Compliments client-side mobile frameworks

• Key Features– Device Detection– Site Preference Management– Site Switcher

4

Saturday, October 29, 11

Page 7: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Device Detection

• Useful when requests by mobile devices need to be handled differently from requests made by desktop browsers

• Introspects HTTP requests to determine the device that originated the request.– Achieved by analyzing the User-Agent header and other

request headers– In contrast to “Feature Detection” where client detects

available features

• Spring Mobile provides a DeviceResolver abstraction and interceptor

5

Saturday, October 29, 11

Page 8: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Device Detection Demo

6

Saturday, October 29, 11

Page 9: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Site Preference Management

• Device detection is often used to determine which "site" will be served to the user– Mobile site vs. desktop site

• Spring Mobile also provides support for “site preference management”

• Allows the user to indicate whether he or she prefers the mobile site or the normal site

• Remembers the user’s preference for their session

7

Saturday, October 29, 11

Page 10: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Site Preference Demo

8

Saturday, October 29, 11

Page 11: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Site Switcher

• Some applications may wish to host their "mobile site" at a different domain from their "normal site"– For example, Google will switch you to m.google.com if you

access google.com from your mobile phone

• SiteSwitcherHandlerInterceptor can be used to redirect mobile users to a dedicated mobile site

• Supported SiteSwitchers– mDot - m.example.com– dotMobi - example.mobi

9

Saturday, October 29, 11

Page 12: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Site Switcher Demo

10

Saturday, October 29, 11

Page 13: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Limitations of Mobile web sites

• they can’t access the native capabilities of the phone• they require network access (no offline support)• formatting an application to look mobile is different than

actually being a mobile application

11

Saturday, October 29, 11

Page 14: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Agenda

12

Saturday, October 29, 11

Page 15: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Agenda

12

Saturday, October 29, 11

Page 16: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Agenda

12

Saturday, October 29, 11

Page 17: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

An Introduction to Android!

13

More than 500,000 activations every day

Saturday, October 29, 11

Page 18: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

An Introduction to Android!

• Huge and growing ecosystem ofapplications and a market to boot

14

8.1 billion app downloadsAndroid Market PlaceApple App Store 6 billion

* http://news.cnet.com/8301-1035_3-20103230-94/android-to-overtake-apple-in-app-downloads/

Expected downloads in 2011

Saturday, October 29, 11

Page 19: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Easy to get started

15

• Programs are written in Java ( )

Saturday, October 29, 11

Page 20: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Easy to get started

15

• Programs are written in Java ( )

Saturday, October 29, 11

Page 21: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Easy to get started

15

• Programs are written in Java ( )

Saturday, October 29, 11

Page 22: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Easy APIs and concepts

• no real “applications,” only loosely coupled components

16

Activities describes the unit of work for one screen

Services does background work like synchronization with a cloud service

Content Providers component that knows how to render and manipulate content of a certain type

Broadcast Receivers knows how to receive and respond to system-wide events like screen shutoff.

* http://developer.android.com/guide/topics/fundamentals.html

Saturday, October 29, 11

Page 23: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

A Simple Activity

17

package org.springframework.android.activities;

import android.app.Activity;import android.os.Bundle;

public class HelloAndroid extends Activity {     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }

}

Saturday, October 29, 11

Page 24: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

You *must* extend Android classes to build proper components

A Simple Activity

17

package org.springframework.android.activities;

import android.app.Activity;import android.os.Bundle;

public class HelloAndroid extends Activity {     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }

}

Saturday, October 29, 11

Page 25: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

You *must* extend Android classes to build proper components

R.* refers to constants that Android code generates for you that correspond to “resources”

A Simple Activity

17

package org.springframework.android.activities;

import android.app.Activity;import android.os.Bundle;

public class HelloAndroid extends Activity {     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }

}

Saturday, October 29, 11

Page 26: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Declaring the Simple Activity

18

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/textview"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:text="@string/hello"/>

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello, Android! I am a string resource!</string>    <string name="app_name">Hello, Android</string></resources>

/res/values/strings.xml

Saturday, October 29, 11

Page 27: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Lifecycle

• Android controls lifecycles of these components• Registered in manifest

19

<activity android:name=".activities.HelloAndroid          android:label="@string/app_name"           android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>

Saturday, October 29, 11

Page 28: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Class is set relative to root package specified in manifest

Lifecycle

• Android controls lifecycles of these components• Registered in manifest

19

<activity android:name=".activities.HelloAndroid          android:label="@string/app_name"           android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>

Saturday, October 29, 11

Page 29: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

you specify that an Activity is the primary one like this

Class is set relative to root package specified in manifest

Lifecycle

• Android controls lifecycles of these components• Registered in manifest

19

<activity android:name=".activities.HelloAndroid          android:label="@string/app_name"           android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>

Saturday, October 29, 11

Page 30: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Android Sample Demo

• How to use STS and the Android Eclipse plugin

20

Saturday, October 29, 11

Page 31: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

How can Maven help?

• Android4Maven– This project compiles android.jar from source and pulls out

source and resource files to replicate android.jar in the SDK– http://sourceforge.net/projects/android4maven/

• Maven Android SDK Deployer– If you need to use Google maps, then you have to go this

route– https://github.com/mosabua/maven-android-sdk-deployer

• Maven Android Plugin– Provides support for Maven dependency management within

Android projects– http://code.google.com/p/maven-android-plugin/

21

Saturday, October 29, 11

Page 32: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Maven Android Plugin Configuration

22

<plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <version>2.8.4</version> <configuration> <sdk> <platform>3</platform> </sdk> <emulator> <avd>3</avd> </emulator> <deleteConflictingFiles>true</deleteConflictingFiles> <undeployBeforeDeploy>true</undeployBeforeDeploy> </configuration> <extensions>true</extensions> </plugin>

Saturday, October 29, 11

Page 33: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

m2eclipse Support

• Maven Integration for Android Development Tools– An Eclipse plugin that adds support for integrating

m2eclipse, Android Developer Tools, and the Maven Android Plugin

– http://code.google.com/a/eclipselabs.org/p/m2eclipse-android-integration/

• Maven Android archetypes– This projects provides several Maven archetypes for

Android. These archetypes allow you to quickly bootstrap a Maven project to develop an android application.

– https://github.com/akquinet/android-archetypes

23

http://blog.springsource.com/2010/12/17/spring-android-and-maven-part-1/http://blog.springsource.com/2010/12/17/spring-android-and-maven-part-2/

Saturday, October 29, 11

Page 34: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Running the simple Activity

24

Saturday, October 29, 11

Page 35: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Running the simple Activity

24

Saturday, October 29, 11

Page 36: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

25

...what about something a bit more non-trivial?

Saturday, October 29, 11

Page 37: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

26

Spring’s aim:

bring simplicity to java development

modern web data access integration mobile social security

tc ServerTomcatJetty

lightweightCloudFoundry

Google App EngineAmazon Web Services

BeanStalkHeroku

the cloud: WebSphereJBoss ASWebLogic

(on legacy versions, too!)

traditional

The Spring framework

security

Enter Spring Android!

Saturday, October 29, 11

Page 38: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

What problem are we trying to solve?

• Concerns– REST has become a popular choice for architecting both

public and private web services– The Android runtime provides HTTP clients capable of

making HTTP connections and requests, but it does not have a fully featured REST client

• Spring Android Solution– The goal of Spring Android Rest Template is to provide an

easy to use, and functional REST client that supports marshaling objects from XML and JSON.

27

Saturday, October 29, 11

Page 39: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

REST

• Origin– The term Representational State Transfer was introduced and

defined in 2000 by Roy Fielding in his doctoral dissertation.• His paper suggests these four design principles:

– Use HTTP methods explicitly.• POST, GET, PUT, DELETE• CRUD operations can be mapped to these existing methods

– Be stateless.• State dependencies limit or restrict scalability

– Expose directory structure-like URIs.• URI’s should be easily understood

– Transfer XML, JavaScript Object Notation (JSON), or both.• Use XML or JSON to represent data objects or attributes

28

Saturday, October 29, 11

Page 40: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Basic Rest Template Example

29

RestTemplate restTemplate = new RestTemplate();String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}";String result = restTemplate.getForObject(url, String.class, "SpringSource");

RestTemplate restTemplate = new RestTemplate();String url = "http://example.com/hotels/{hotel}/bookings/{booking}";String result = restTemplate.getForObject(url, String.class, "42", “21”);

Google search example

Multiple parameters

Saturday, October 29, 11

Page 41: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Demo

• Using Spring Android to communicate with a RESTful web service (Google Search Demo)

30

Saturday, October 29, 11

Page 42: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Android Rest Template

• Based on SpringFramework– The majority of the supporting classes are pulled from

SpringFramework.– Modifications were made to support Android.

• RestTemplate class is the heart of the library– Entry points for the six main HTTP methods

• DELETE - delete(...)• GET - getForObject(...)• HEAD - headForHeaders(...)• OPTIONS - optionsForAllow(...)• POST - postForLocation(...)• PUT - put(...)• any HTTP operation - exchange(...) and execute(...)

31

Saturday, October 29, 11

Page 43: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Android Rest Template

• Http Client– The HttpComponents HttpClient is a native HTTP client

available on the Android platform.– HttpComponentsClientHttpRequestFactory

• Message Converters– MappingJacksonHttpMessageConverter - object to

JSON marshaling supported via the Jackson JSON Processor

– SimpleXmlHttpMessageConverter - object to XML marshaling supported via the Simple XML Serializer

– SyndFeedHttpMessageConverter - RSS and Atom feeds supported via the Android ROME Feed Reader

32

Saturday, October 29, 11

Page 44: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Android Showcase

• Examples– HTTP GET

• JSON• XML

– HTTP GET with Parameters• JSON• XML

– HTTP POST• String• JSON• XML• MultiValueMap

– HTTP and GZIP

33

Saturday, October 29, 11

Page 45: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Android Demos

• Spring Android Showcase Demo

34

Saturday, October 29, 11

Page 46: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Spring Social on Android

• Supports connecting to supported Spring Social services• uses same RESTful connectivity based on RestTemplate

35

Saturday, October 29, 11

Page 47: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Enter Spring Android!

36

Saturday, October 29, 11

Page 48: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

37

do NOT reinvent the Wheel!

Saturday, October 29, 11

Page 49: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

38

Dependency Injection on Android

• Problems with DI on Android– hard reliance on base classes– hard reliance on Android to manage the runtime lifecycle

• a POJO peer system would have been onerous

Saturday, October 29, 11

Page 50: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

39

Dependency Injection on Android

• Lots of options– RoboGuice– Android Annotations– the Android way

Saturday, October 29, 11

Page 51: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

40

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)– Pros:

• requires you to extend RoboApplication• You must configure your beans using the

AbstractAndroidModule• Each Activity must extend from RoboActivity

– Cons:• no AOP• not small, at all!

– (400kb to a mobile application may as well be 400MBs to your enterprise application!)

Saturday, October 29, 11

Page 52: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

public class MyActivity extends Activity {     private TextView label;     private Drawable image;     private SearchManager searchManager;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.myactivity);        this.label = (TextView) findViewById(R.id.mylabel);        this.image = getResources().getDrawable(R.drawable.myimage);        this.searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE);    }}

before RoboGuice

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 53: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 54: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

public class MyActivity extends RoboActivity {     @InjectView(R.id.mylabel)    TextView label;     @InjectResource(R.drawable.myimage)    Drawable image;     @Inject    SearchManager searchManager;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.myactivity);    }}

with RoboGuice

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 55: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

used to inject other widgets or “views”

public class MyActivity extends RoboActivity {     @InjectView(R.id.mylabel)    TextView label;     @InjectResource(R.drawable.myimage)    Drawable image;     @Inject    SearchManager searchManager;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.myactivity);    }}

with RoboGuice

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 56: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

used to inject Resources

used to inject other widgets or “views”

public class MyActivity extends RoboActivity {     @InjectView(R.id.mylabel)    TextView label;     @InjectResource(R.drawable.myimage)    Drawable image;     @Inject    SearchManager searchManager;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.myactivity);    }}

with RoboGuice

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 57: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

used to inject other objects

used to inject Resources

used to inject other widgets or “views”

public class MyActivity extends RoboActivity {     @InjectView(R.id.mylabel)    TextView label;     @InjectResource(R.drawable.myimage)    Drawable image;     @Inject    SearchManager searchManager;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.myactivity);    }}

with RoboGuice

41

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)

Saturday, October 29, 11

Page 58: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

42

Dependency Injection on Android

• RoboGuice (http://code.google.com/p/roboguice/)– Pros:

• requires you to extend RoboApplication• You must configure your beans using the

AbstractAndroidModule• Each Activity must extend from RoboActivity

– Cons:• no AOP• not small, at all!

– (400kb to a mobile application may as well be 400MBs to your enterprise application!)

• runtime inefficiency

Saturday, October 29, 11

Page 59: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

43

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)– Pros:

• compile-time code generation means no runtime cost• can be used side-by-side with RoboGuice

– Cons:• extra build step• some redundancy with RoboGuice

Saturday, October 29, 11

Page 60: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

44

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)

@EActivity(R.layout.myactivity) public class MyActivity extends Activity {     @InjectView     TextView mylabel;     @DrawableRes(R.drawable.myimage)    Drawable image;     @SystemService    SearchManager searchManager;}

Saturday, October 29, 11

Page 61: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

sets the layout

44

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)

@EActivity(R.layout.myactivity) public class MyActivity extends Activity {     @InjectView     TextView mylabel;     @DrawableRes(R.drawable.myimage)    Drawable image;     @SystemService    SearchManager searchManager;}

Saturday, October 29, 11

Page 62: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

sets the layout

Inject another widget or “view”

44

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)

@EActivity(R.layout.myactivity) public class MyActivity extends Activity {     @InjectView     TextView mylabel;     @DrawableRes(R.drawable.myimage)    Drawable image;     @SystemService    SearchManager searchManager;}

Saturday, October 29, 11

Page 63: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

sets the layout

Inject another widget or “view”

specify a resource id (it is optional)

44

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)

@EActivity(R.layout.myactivity) public class MyActivity extends Activity {     @InjectView     TextView mylabel;     @DrawableRes(R.drawable.myimage)    Drawable image;     @SystemService    SearchManager searchManager;}

Saturday, October 29, 11

Page 64: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

sets the layout

Inject another widget or “view”

specify a resource id (it is optional)

Inject objects configured manually

44

Beyond Dependency Injection

• Android Annotations(http://code.google.com/p/androidannotations/)

@EActivity(R.layout.myactivity) public class MyActivity extends Activity {     @InjectView     TextView mylabel;     @DrawableRes(R.drawable.myimage)    Drawable image;     @SystemService    SearchManager searchManager;}

Saturday, October 29, 11

Page 65: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

45

Dependency Injection on Android

• The Android way – android applications all have required access to a single

“Application” class– You can override the Application class– Thus, instant singleton!

Saturday, October 29, 11

Page 66: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

46

Dependency Injection on Android

• The Android way public class MainApplication extends Application  {     private MyService service;     @Override    public void onCreate() {        super.onCreate();        service = new MyServiceImpl();    }     public MyService getMyService() {        return this.service;    }}

Saturday, October 29, 11

Page 67: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

extend the Application

46

Dependency Injection on Android

• The Android way public class MainApplication extends Application  {     private MyService service;     @Override    public void onCreate() {        super.onCreate();        service = new MyServiceImpl();    }     public MyService getMyService() {        return this.service;    }}

Saturday, October 29, 11

Page 68: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

register your global singleton services

extend the Application

46

Dependency Injection on Android

• The Android way public class MainApplication extends Application  {     private MyService service;     @Override    public void onCreate() {        super.onCreate();        service = new MyServiceImpl();    }     public MyService getMyService() {        return this.service;    }}

Saturday, October 29, 11

Page 69: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

public class MainActivity extends Activity  {     private MyService service;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        MainApplication app = (MainApplication) getApplication();        service = app.getMyService();    }}

47

Dependency Injection on Android

• The Android way

Saturday, October 29, 11

Page 70: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

get a pointer to the Application

public class MainActivity extends Activity  {     private MyService service;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        MainApplication app = (MainApplication) getApplication();        service = app.getMyService();    }}

47

Dependency Injection on Android

• The Android way

Saturday, October 29, 11

Page 71: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

access your service

get a pointer to the Application

public class MainActivity extends Activity  {     private MyService service;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        MainApplication app = (MainApplication) getApplication();        service = app.getMyService();    }}

47

Dependency Injection on Android

• The Android way

Saturday, October 29, 11

Page 72: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

Additional Resources

• Project Home– http://www.springsource.org/spring-android– http://www.springsource.org/spring-mobile

• Sample Code– https://github.com/SpringSource/spring-android-samples.git– https://github.com/SpringSource/spring-mobile-samples.git

• Blog Posts– http://blog.springsource.org

48

Saturday, October 29, 11

Page 73: Roy Clarkson & Josh Longwhiteship.me/wp-content/uploads/2011/11/one_up_android.pdf · • Roy Clarkson & Josh Long SpringSource, a division of VMware 1 Native Android Development

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Q&A

49

Saturday, October 29, 11