29
How to Make an A: ANDROID EDITION

How to Make an App: Android Edition

  • Upload
    kinvey

  • View
    1.465

  • Download
    5

Embed Size (px)

DESCRIPTION

Kinvey’s eBook titled, “How to Make an App: Android Edition” is a step by step guide written by developers, for developers.

Citation preview

Page 1: How to Make an App: Android Edition

How to Make an A�: ANDROID EDITION

Page 2: How to Make an App: Android Edition

Table of ContentsHow to Build an App: Android Edition

What’s All the Excitement About?

Step 1: Decide What New Thing You Want to Do

Step 2: Define Your MVP

Step 3: Design Your App

Step 4: Set up Your Android Development Environment

Step 5: Developing Your App

1

3

5

8

13

16

i

Page 3: How to Make an App: Android Edition

What’s All the Excitement About?

It’s never been easier or quicker to build

and distribute an application to millions

of devices and get paid, especially when

you tap into Android’s market leading

features.

“We are living in the Post-PC era.” That

sentence — in quotes — comes up over

14,000 times in a Google search. So what

are so many people writing about? They

are writing about a time when many

computing tasks — such as staying in

touch with friends, doing online

research, shopping, and playing games

— no longer require a laptop or desktop

computer. Not only can these activities

now be done “on the go” with a smart-

phone or tablet, but they can also be

done in new ways that enrich the

experience.

Android devices are a great example.

Many are location-aware; many have

accelerometers that know when the

phone is moving, in which direction and

how fast; and many include NFC (Near

Field Communications), a technology

that enables easy communications (such

as by tapping) between two devices or

between a device and a passive tag.

1

HOW TO MAKE AN APP: ANDROID EDITION

NFC is useful for actions like automated

store checkout, inventory counting,

contact information exchange, and

offering special deals to customers in a

specific store aisle.

Android also offers features not found on

other mobile platforms like iOS or

Windows Phone that make development

easier. Two good examples are the

AccountManager system and the

AccountAuthenticatorActivity base class,

both of which streamline the process of

setting up custom accounts — such as to

register and authenticate user IDs and

passwords. Android also offers

market-leading capabilities for letting

applications on the same device invoke

each other’s services. That way a

developer is spared the work of imple-

menting and replicating an already-exist-

ing service.

Besides its technical advantages, Android

also offers developers major business

opportunities.

Page 4: How to Make an App: Android Edition

2

HOW TO MAKE AN APP: ANDROID EDITION

According to Google, there were over

700,000 apps available on Google Play in

October 2012 and there were over 25

billion downloads in September 2012.

According to IDC, as of Q3 2012 Android

accounted for 75% of all smartphone

shipments — in a market that did not

exist until 2008.

Two recent developments in particular

have sparked this growth: the Android

ecosystem and the Cloud. The ecosys-

tem consists of resources like Android

Development Tools (to be discussed

later) and distribution platforms (like

Google Play) that accelerate app

development and marketing. These

platforms remove the “friction” involved

in distributing and selling traditional PC

applications. There’s no physical

packaging, no inventory stocking, and

much less waiting for developers to get

paid. You simply register online, pay a

nominal fee, upload the app and you’re

in business.

“The Cloud” is a general term meaning a

service that users and applications share

via the Internet. A cloud may be “private”

— i.e., the services belong to an

organization for the benefit of its

employees and business partners. Or a

cloud may be “public” — i.e., a company

(such as Amazon or Kinvey) owns the

services, which it offers commercially to

the public, including app developers. In

the Android space, Cloud services

typically function as an app’s “backend.”

They may do computational tasks

considered too “heavy” for a phone.

They might also store, secure and share

data among multiple users on different

phones. A good example is a game app

where users need to access the current

state of play.

If you are a developer and want to build

your own backend, you certainly can do

that. Whether you want to or not

probably depends on whether your

backend will set your app apart in the

eyes of your potential customer. If it’s a

service that’s fairly typical across lots of

apps — like user authentication or a

shopping cart — then the answer is

probably “no.” You may be better off

hooking into a commercial backend

provider via an API.

Two recent developments in particular have sparked

(Android) growth: the An-droid ecosystem and the cloud

Page 5: How to Make an App: Android Edition

3

DECIDE WHAT NEW THING YOU WANT TO DO

Step 1

Page 6: How to Make an App: Android Edition

4

HOW TO MAKE AN APP: ANDROID EDITION

Then you can focus on what counts most

— a unique user experience and

application-specific functionality. That

brings us to the first step in making an

app.

Why do people buy apps? Why do they

make them? The answer to both

questions is that a successful app

improves users’ lives in new ways —

typically by taking advantage of features

like location awareness and accelerome-

ters that are present in phones. A great

example app is Google Now, which

Google describes as follows:

Google Now gets you just the right

information at just the right time.

It tells you today’s weather before you

start your day, how much traffic to

expect before you leave for work, when

the next train will arrive as you’re

standing on the platform, or your

favorite team's score while they’re

playing. And the best part? All of this

happens automatically. Cards appear

throughout the day at the moment you

need them.

The Google Maps API enables location

awareness so apps can tailor the user

experience based on location — such as

by recommending stores or restaurants

in that area. As previously noted, NFC is

a feature that lets users easily exchange

data, such as photos or contact

information, with another NFC phone

just by tapping the two phones together.

Other app opportunities involve

information sharing. A prime example of

that is Google Docs that lets different

users on different devices work on the

same document (in the Cloud) coopera-

tively. A similar opportunity is context

sharing such as when the user starts a

movie on a smartphone, stops the

movie, and later picks up the action at

the very same point on a TV, PC, tablet,

or another smartphone.

The key, again, is identifying a function

that improves people’s lives. That

requires, first, identifying the value to be

provided, second, conceptualizing a core

function that provides that value and,

third, researching the market to make

sure there is nothing else out there that

already does what your app will do. The

research part is easy since you can

probably find out if a similar app already

exists just by spending a few minutes on

Google Play.

Does your app meet all three of these

tests? Good, then let’s proceed to the

next step.

Page 7: How to Make an App: Android Edition

5

DEFINE YOUR MVPStep 2

Page 8: How to Make an App: Android Edition

6

HOW TO MAKE AN APP: ANDROID EDITION

Most unsuccessful apps fail for any of

three reasons:

1. the app was a solution in

search of a problem (i.e., no real need

existed)

2. the implementation was poor

3. the app tried to do too much

Keeping the app as simple as possible

(especially in version 1) is the best way to

avoid all three pitfalls. Most successful

apps (indeed, most successful consumer

tech products) are based on what is

called an MVP, or minimum viable

product. It’s the product with the

smallest feature set that can deliver the

core function without any distracting

“bells and whistles.” You know you have

an MVP if the product would no longer

function if you removed any single

feature.

The best example is the original Google

Search. Whereas today’s Google Search

offers more than 20 special features

beyond the original word search

capability (e.g., synonyms, weather

forecasts, stock quotes, maps, transla-

tion) the original product was simply text

search — where the user types into a

search box at the top of an otherwise

all-but-empty web page and gets back a

page full of text search results.

It was enough to prove the convenience

and accuracy of using Google.

The same MVP approach drives the app

market today. With each app focused on

meeting a specific need, everyone’s

needs are as different as the collection

of apps on their various devices.

Focusing on a specific function also

makes for a less cluttered app — one

that is easier for the customer to

understand and for the developer to

build. As a result, an MVP will probably

perform better technically and its value

will be easier for both the developer and

buyer to discern.

So, in drawing up your MVP feature list,

ask yourself what features are key to the

experience. Possible must-haves include:

• Offline capability (i.e., ability to work

when not connected to the Internet)

Page 9: How to Make an App: Android Edition

7

HOW TO MAKE AN APP: ANDROID EDITION

• Location aware (i.e., will the app use

GPS?)

• NFC (i.e., can devices share data by

touching?)

• Push notification (i.e., sending

updated information to devices as

soon as it is available)

Draw up your feature lists knowing

which features are necessary and which

are there primarily to enhance the user

experience. Push notifications, for

example, are not key to Instagram but

the app has them anyway to encourage

user activity. Features that are there

solely to enhance the user experience

may have to wait for version 2. (The first

iPod only came in only one color and

could just hold 1000 songs.)

Once you have your feature list, the next

step is design — to decide where and

how to implement the features on the

device, in the cloud, or in some combina-

tion. Most app design decisions fall into

two broad categories: user experience

and shared information. Which takes us

to the next step.

MVP Takeaways• MVP = Minimal Viable

Product

• It’s the product with thesmallest feature set that can deliver the core func-tion

• You have an MVP if theproduct would no longer function if you removed any single feature

• An MVP will probablyperform better technically and its value will be easier for both the developer and buyer to discern

Page 10: How to Make an App: Android Edition

8

DESIGN YOUR APPStep 3

Page 11: How to Make an App: Android Edition

9

HOW TO MAKE AN APP: ANDROID EDITION

User experience (UX) decisions

determine the app’s look and feel. They

answer questions such as: What does

the app look like? What screens does it

have? In the app world, onscreen objects

like buttons, sliders, and fill-in boxes are

called widgets. So you need to decide

which types of widgets will reside on

which screens. What actions will occur as

a result of the user interacting with

those widgets?

Keep in mind that users will expect to

interact differently with a mobile device

than they do with a PC because the

screens on mobile devices are much

smaller. They’ll expect to use their

fingers instead of a mouse or a trackpad.

Ideally, your app can even be used with

one hand holding the device while using

just a thumb for scrolling and working

the app’s other controls — the Path app

is a good example, and increasingly so is

Facebook.

You will also need to decide which parts

(if any) of your application to write in

HTML5 and which to write in Java, the

primary programming language for

Android devices. For reasons of speed

and programming efficiency, many apps

(like Facebook) are designed a little like

1960s-era TV sets where a small window

of frequently updated content

(the HTML5 part) sits within an applica-

tion wrapper (the Java part) that

implements less dynamic content such

as the app’s widgets. Which part of the

app is HTML5 and which is Java is not

obvious to the untrained eye (there may

be no browser address bar, for example)

— but implementing the app this way

enables much faster content refresh (via

the web) and more response widgets

(via Java). HTML5’s “write once, deploy

anywhere” model is also another

advantage. Parts of the app written in

HTML5 can be deployed across iPhones,

iPads and Android devices without

rewrite.

In addition to deciding what happens on

the frontend, you also have to decide

what happens on the backend —

specifically, what data will users share?

For example, will users want to “broad-

cast” their GPS locations to other users

in real time (such as to enhance a

gaming experience)? Will the app share

or store movie or restaurant preferences

or purchase histories with backend

recommendation engines? If so, these

functions will most likely “call” the APIs

of backend service providers — you

won’t actually have to write those

functions yourself.

Page 12: How to Make an App: Android Edition

10

HOW TO MAKE AN APP: ANDROID EDITION

For the time being, then, set those

backend functions aside and focus on

the front end. Just like you want to build

a product with the minimal viable

number of features, you may also wish

to build your first prototype using

“dummy” data that’s static rather than

shared. It’s much easier to fine-tune the

frontend if you don’t have to simultane-

ously modify your backend too. Once

you get the app’s look-and-feel right,

then make those backend connections.

Why Android?

Besides the features and design of your

first app, there’s one other decision you

need to make before you start actual

development. That’s whether to deploy

on Apple’s iOS platform or on Google’s

Android platform. Some of the reasons

to develop for Android include:

1. Cross-Compatibility

There are well over 100 million Android

devices in use today, representing

hundreds of different models from

dozens of different manufacturers.

Companies as varied as Samsung,

Motorola, LG, HTC, etc. make devices

that will run Android apps — and these

devices come in many different screen

sizes and resolutions. Android-capable

devices also come in different configura-

tions of hardware features, such as

camera, accelerometer, GPS, and

Bluetooth — yet they all run Android —

and all are available to you as potential

targets for your Android app.

WHY ANDROID?

Cross-Compatibility

OpenPlatform

MarketGrowth

FreeTools

InternationalGrowth

Page 13: How to Make an App: Android Edition

11

HOW TO MAKE AN APP: ANDROID EDITION

WHY Not ANDROID?

Customers lesswilling to pay

High diversity of devices

Not in the Apple ecosystem

2. Open Platform

Android’s cross-compatibility stems from

the fact that it is an open platform,

meaning that any company can

download the free source code and

build products based on that code. You

can view and modify the code as you

wish to create new features, or to

handle an existing feature in a new way.

3. Market Growth

As noted earlier, the Android market is

huge and growing fast — thanks to its

openness and compatibility across so

many devices and device makers.

Android’s openness and market growth

also make for a highly innovative

environment where “the next big thing”

is likely to happen. That’s a very

attractive proposition for developers.

4. International Growth

In particular, Android overwhelmingly

dominates markets outside the U.S. For

example, it accounts for over 90%

market share in

China (Q3, 2012) according to

Beijing-based Analysis International as

reported by Tech In Asia.

5. Free Tools

All the tools you need to develop rich

Android apps are free of charge. Step 4

is about installing those tools and setting

up your Android development environ-

ment.

Why Not Android?

Although Android offers many compel-

ling advantages as a platform for your

mobile app, it also has some disadvan-

tages. Here are three:

Page 14: How to Make an App: Android Edition

12

HOW TO MAKE AN APP: ANDROID EDITION

1. Customers less willing to pay

Perhaps it’s because of Android’s open

source heritage, but whatever the

reason, Android apps generate less than

10% of the total dollars spent on mobile

apps, despite having 75% of the

smartphone market itself — and despite

the fact that the average selling price of

an app in the Android Market is $3.79

versus Apple App Store’s $2.01. In other

words, Android users are reluctant to

spend money on apps.

2. High diversity of devices

Building an app that runs successfully on

so many different screen sizes and

resolutions continues to be a challenge

despite the fact that Android has

provided innovations such as fragments

and GridLayout. Android introduced

fragments in Android 3.0 (API level 11),

primarily to support more dynamic and

flexible user interface designs on large

screens, such as tablets. A fragment is a

Java class that enables tablets to display

side-by-side on one screen content that

on a phone could not be displayed all at

once on the same screen — and so

would be displayed on multiple screens.

GridLayout is a class that places its

children in a rectangular grid composed

of a set of infinitely thin lines that

separate the viewing area into cells.

3. Not in the Apple Ecosystem

Some developers and users prefer

Apple’s closed and more tightly

controlled environment to Android.

Apple’s ecosystem, consisting of iCloud,

iTunes, the App Store, the iPad, the

iPhone, Mac computers, etc., offers a

very holistic user experience with the

ability to share data seamlessly across

devices plus the assurance of having

applications pre-approved for quality

and content prior to going on sale.

Page 15: How to Make an App: Android Edition

13

SET UP YOUR ANDROID DEVELOPMENT ENVIRONMENT

Step 4

Android ADT

Java Dev Kit

Eclipse IDE

Android SDK

Page 16: How to Make an App: Android Edition

14

HOW TO MAKE AN APP: ANDROID EDITION

Android apps are typically written as Java

classes. That source code is then

compiled into bytecode that the Dalvik

engine (a subset of a Java Virtual

Machine) runs under the Android

operating system. The following steps

describe how to acquire and configure

the tools you need to write your app’s

Java classes and then test and package

the app for deployment. NOTE: By the

time you read this, these instructions

may have changed. Please refer to the

Android Developers Website for current

updates.

For Android app development you’ll

need four basic toolsets:

1. The Java Development Kit (JDK)

This is the Software Development Kit

(SDK) for Java and provides the founda-

tion for the Android SDK.

2. Android SDK

This consists of all the tools you need to

develop and test your app:

• Eclipse + ADT plugin

• Android SDK Tools

• Android Platform-tools

• The latest Android platform

• The latest Android system image for

the emulator

3. Eclipse IDE (integrated develop-

ment environment)

Included in the Android SDK download,

the Eclipse IDE provides the “hands-on”

controls you need for writing your app

using Java, the Android SDK and the

Android ADT.

4. Android ADT (Android Development

Tools) This Eclipse plug-in performs

much of the housekeeping involved in

creating an Android app such as creating

the needed files and overall structure.

Installing the JDK

To install the official Oracle Java SE SDK

(JDK) for Windows, OS X, and Linux go to

the Oracle Java website and follow the

instructions.

Installing the Android SDK

At this point you still need to acquire the

Java files needed to compile an Android

app as well as some additional build

tools and the files required to run an

Android emulator. To access these

add-ons, look inside the tools/ directory

of the Android SDK you just downloaded

and run the Android batch file or shell

script. That opens the Android SDK

Manager. Make sure the following items

are checked and then click Install.

Page 17: How to Make an App: Android Edition

15

HOW TO MAKE AN APP: ANDROID EDITION

• “SDK Platform” for all Android SDK

releases you want to test against

• “ARM EABI v7a System Image”

• “Documentation for Android SDK”

• “Samples for SDK”

• “Google APIs by Google Inc.” for the

Android SDK release you’re download-

ing

• Android SDK Tools and

Platform-tools

• Android Support package (in the

Extras group at the bottom of the

tree)

One shortcut that many developers like

to use is to install everything with a

single command. Just type the following

command at the command line:

android update sdk --no-u

This will perform a full install of all

Android versions and samples along

with all system images. Obviously, this

approach takes up the most amount of

space on the system.

Once all the selected items are installed

(it may take several hours if you have a

slow Internet connection), you are ready

to begin making your Android app!

Development Takeaways• Android apps are written as

Java classes

• Source code is compiled

into bytecode that the Dalvik

engine runs under the

Android operating system

Four Basic Toolsets

1. Java Development Kit

2. Android SDK

3. Eclipse IDE

4. Android ADT

Page 18: How to Make an App: Android Edition

16

DEVELOPING YOUR APPStep 5

Page 19: How to Make an App: Android Edition

17

HOW TO MAKE AN APP: ANDROID EDITION

With the development environment

installed, the next step is to launch

Eclipse and begin making your app. For

this step, we will walk through two

examples: “My First App” and “Test

Drive.” The first app displays a button on

the phone — push the button to display

the text, “Hello World.” The second app

uses the Kinvey backend service. In that

app there are two buttons — push one

button and it saves static data to the

backend and the app displays text that

the save was successful. Push the other

button and the app loads the data to the

phone from the backend and displays

text saying the load was a success.

To get started on My First App, Launch

Eclipse and select File > New > Project to

bring up the “New Project” wizard.

Choose “Android Application Project”and

click “Next.”

In the “New Android Application” panel

enter the following information:

• For “Application Name” and “Project

Name” enter My First App

• For “Package Name” enter com.ex-

ample.myfirstapp

• For “Build SDK” select “Android 4.2”

• For “Minimum Required SDK,”

choose “API 9: Android 2.3 (Ginger-

bread)”

• Uncheck “Create custom launcher

icon”

• Click “Next”

This brings up the “Create Activity” page.

• Click “BlankActivity” and then “Next”

This brings up the “New Blank Activity”

page.

• For “Activity Name” enter

MainActivity

Page 20: How to Make an App: Android Edition

18

HOW TO MAKE AN APP: ANDROID EDITION

• For “Layout Name” enter activity_-

main

• For “Title” enter MainActivity

• Keep the remaining defaults as is

• Click “Finish” The Eclipse Package

Explorer now appears. It shows the

various folders containing the files that

Eclipse will use to build your Android

app. When you “make an app” you do so

by working on the files within this

hierarchy. Eclipse is context sensitive so

clicking on a file brings up an appropri-

ate wizard, graphic tool or text editor for

working on that file.

Eclipse just created one of these files for

you.

AndroidManifest.xml

This XML file is called the manifest. It is a

configuration file that tells Android what

your app contains. The manifest for My

First App looks like the image below.

The two key parts of the manifest are its

root, the <manifest> element, and that

element’s primary child, the <applica-

tion> element. Among other things, the

<manifest> element supplies the name

of your app’s package, uniquely

identifying your app to the device

running it and to the Google Play Store.

The <application> element supplies the

name for the class implementing activity

(MainActivity) and an <intent-filter>

describing under what conditions

Android displays the activity.

Page 21: How to Make an App: Android Edition

19

HOW TO MAKE AN APP: ANDROID EDITION

The default intent filter, as in this case, is

for the activity to appear in the launcher

so users can choose to run it. Note that

an app can have any number of activities

— an activity is the app’s behavior

associated with a particular screen.

You can easily add or change various

parts of the manifest, such to enable

various permissions or screen sizes, by

using the Eclipse manifest structured

editor. To show it, just double click

AndroidManifest.xml in the Package

Explorer.

Besides the manifest, other elements in

the application package to note are:

res/

Modifying this item is where much of

your actual “app making” work is done. It

holds resources such as “drawable” icons

and GUI layouts that are packaged with

the compiled Java in the application. For

example, the layout folder contains XML

files that determine the positioning and

size of text boxes, buttons and other

layout elements for a particular activity

— and also attach “onClick” methods to

them. Double clicking on a layout (in our

case, that’s activity_main.xml) brings up

the Eclipse graphical layout editor. As

you drag and drop items into place the

editor generates the appropriate XML.

The “drawable” files, meanwhile, contain

static images sized appropriately for

particular ranges of screen sizes, such as

drawable-mdi, which contains images

sized for medium size screens.

libs/

This folder contains third-party Java JARs,

such as those required to communicate

with the Kinvey backend. You can use

the Eclipse “Navigate” and “Search”

functions to find and import these JARs

to your libs/ directory.

scr/

Holds the application’s Java source code

(which you can edit directly).

gen/

Where Eclipse build tools place the Java

source code (in R.java) they generate (for

example, after you import a JAR or

modify a layout, close and then reopen

the project).

bin/

Holds the compiled application.

Page 22: How to Make an App: Android Edition

20

HOW TO MAKE AN APP: ANDROID EDITION

To complete My First App, we need to

add the button to the layout and also the

text to be displayed when the user taps

the button. To do that, click on activity_-

main.xml within layout in the Package

Explorer. Eclipse opens the layout editor,

which has both a graphical mode and an

XML text-editing mode. In the graphical

mode, follow these steps:

• Draw a button and a text box in the

layout.

• On the button, type the word

“Button.”

• Select the button you’ve just drawn

and click the “On Click” item in the

“Properties” palette.

• In the blank space just to the right of

“On Click” in the Properties Pallet type

the name of a method you wish to

attach to the button (an “on click

listener” that responds when the

button is clicked). Let’s type the

method name: showHelloWorld.

• On the text box, type the string:

@string/hello_world. This references a

constant (Hello World!) in strings.xml

in the values folder.

Using a relative reference rather than a

constant means if we ever wish to

change the displayed text, we need only

make the change in one place rather

than everywhere that string might be

used.

Switching to text-editing shows us the

resulting XML (except for the missing

text, in red):

--

Page 23: How to Make an App: Android Edition

21

HOW TO MAKE AN APP: ANDROID EDITION

Type in the missing code manually. Now

the “Hello World!” text will be hidden

until the button is pressed.

Finally, we will supply the logic for the

onClick method we have just named. To

do that open the src item in the Package

Explorer and click the MainActivity.java

file. Then type in the method’s Java code,

here shown in red:

Note that the method’s signature must

be public and must include only one

parameter, which is the view parameter.

To run the app, click “Run” in the Eclipse

toolbar. The app will run in the built-in

Android emulator, as shown in the

figure. If you plug your phone into your

computer via a USB cable, you can also

run the app on your own phone.

Eclipse automatically detects the phone

and asks whether you wish to run the

app on the phone or in the emulator.

Congratulations! Your app is done. Now

might be a good time to toast your

accomplishment.

Making a Toast

One of the convenient features that

Android offers developers is the ability

to build alerts, or “toast” messages, into

applications without writing much code.

Typically these alerts are displayed in a

reserved area at the top or bottom of

the phone’s screen whenever a predeter-

mined event occurs.

Page 24: How to Make an App: Android Edition

22

HOW TO MAKE AN APP: ANDROID EDITION

For example, suppose we wanted the

app we just built, My First App, to display

the toast “Button onClick Triggered!”

when the button is clicked. To do that we

simply add the following code, shown

here in red, to our Java source:

The makeText method invokes the string

referenced by TOAST_TEXT, which the

show method displays.

Connecting To a Backend as a Service

One factor mentioned earlier that’s

fueling apps’ explosive growth is the

availability of Cloud-based backend

services. These are useful when your

app needs more data than can be

reasonably stored on a mobile device

and/or the data must be accessible by

multiple devices. Connecting your app to

a commercial backend means you don’t

have to provide that service yourself —

effectively leveling the playing field for

anyone who can’t afford to build their

own app backend versus larger organiza-

tions that can.

Backend as a Service (BaaS) providers, as

they’re called, compete on a variety of

features and benefits, one of which is

how easy it is to connect to your app. So,

chances are, it will be a pretty easy

process including 8 steps.

1. Download the provider’s SDK to

your computer; this contains the

software that lets your app talk to the

backend

2. Create an account on the provider’s

website, usually with a credit card

3. Follow the provider’s instructions to

install the downloaded files into your

application package (e.g., place the

JARs in the libs folder)

4. Add your app on the backend and

type in your app’s name where

requested

5. Configure your backend data model

(i.e., a collection of objects)

6. Locate your app’s backend service

credentials used to authenticate the

app with the backend — copy them

into your app’s source code

7. Create a static app handle used to

identify the app on backend calls

8. Map your app’s data to your

backend model

Here’s how this would work with Kinvey

and our second example app, Test Drive.

Page 25: How to Make an App: Android Edition

23

HOW TO MAKE AN APP: ANDROID EDITION

The app, as previously noted, displays

“Save” and “Load” buttons. Tapping the

“Save” button saves some static dummy

data to the backend and displays a “Save

worked!” message (or “Save failed! if the

save did not work). Tapping the “Load”

button loads the saved data from the

backend to the device and displays a

“Load worked!” message (or “Load failed!

if the load did not work).

Figure X shows the “My Apps” page on

Kinvey with Test Drive already added

and its application credentials displayed

(by clicking the “Credentials” link). Figure

Y shows the backend collection (named

TestObjects) for the Test Drive app. Note

the “_id” and “name” columns with the

labels “12345” and “My first data!”

respectively. (When configuring a

collection, use the “+” and “-“ buttons to

add and delete columns.)

Figure X

Figure Y

These labels tag the dummy TestObject

data the app passes to TestObjects on

the backend when the user taps the

“Save” button. Similarly, when the user

taps the “Load” button the app retrieves

a TestObject from the backend TestOb-

jects.

For those operations to happen the

Kinvey SDK must have been downloaded

(see Kinvey’s “Getting Started” page) and

these SDK contents installed into the

project:

• kinvey-1.4.1.jar and kinvey-1.4.1.-

jar.properties to the libs folder

• urbanairship-lib-1.1.5.jar from the

ua-android-lib-1.1.5 directory to the

libs folder

• kinvey-1.4.1-javadoc folder to a docs

folder in the base project directory

Page 26: How to Make an App: Android Edition

24

HOW TO MAKE AN APP: ANDROID EDITION

The complete Test Drive project can be downloaded from here. To illustrate

steps 6 and 8 listed above, however, we will highlight some specific sections of

the code. Let’s start with step 6, authentication.

To authenticate the app to Kinvey, the auto-generated credentials must be

copied into the app, replacing “your_app_key” and “your_app_secret” in these

lines:

In step 7 we provide a private handle for calls to Kinvey:

Step 8 is mapping the app data to the model we just configured at Kinvey. You

can use any class that implements the MappedEntity interface, in this app

example, that’s a TestEntity class that has a string name. It looks like this:

Page 27: How to Make an App: Android Edition

25

HOW TO MAKE AN APP: ANDROID EDITION

Now let’s look at the method that does the save. The method is called in the

onClickListener of the save_button. When the activity loads, it will create a new

static TestObject with id "12345". When the save button is clicked the following

method will trigger the dummy TestObject to be saved in the testObjects collection.

Here line 2 creates a handle to the backend collection where the object is saved.

Lines 3 through 21 save the object and displays an alert if the save is either

successful or fails. Likewise, the load method looks like this:

Line 2 creates a handle to the backend collection that has the object to load. Line 3

instantiates an instance of TestObject for the library into which the data is loaded

from the backend. And lines 4 through 23 load the object and display an alert if the

load is either successful or fails.

Page 28: How to Make an App: Android Edition

26

HOW TO MAKE AN APP: ANDROID EDITION

Welcome To Our World!

The key takeaway from all this is that

“making an app” is a worthwhile thing to

do for anyone with a clever idea and a

working knowledge of Java. The proof is

in the hundreds of apps succeeding in

the market every day, many written by

people working on their own and who

never wrote an app before in their lives.

As with anything else, practice does

make perfect. But hopefully with all the

resources available, and the knowledge

gained from this ebook, you have

already started down the path toward

joining the growing legions of Android

app developers. If so, we welcome you!

Page 29: How to Make an App: Android Edition

Written by

Designed by

Jake McKibben and Lauren Pedigo

Randall Cronk

Since 1990, Randall has helped over 250

high-tech companies convey the value

of what they do through white papers,

web content, brochures, case studies

and articles. Based today in downtown

Boston, he was previously a vice

president with Regis McKenna where he

ran the Digital Equipment account.

Morgan Bickle

On any given day you'll find Morgan

programming mobile SDKs, building

backend APIs, and discussing the future

of web and mobile technology. As part of

the core founding team at Kinvey and

now the CTO, he owns Kinvey’s

technology vision. Prior to Kinvey, he

wrote enterprise software for a decade.

What is Kinvey? Kinvey makes a fully-featured Backend as a

Service solution, offering 3rd party data integrations, multi-plat-

form support, push notifications, and custom business logic on a

platform where it's free to get started and you only pay when your

app is successful.

Build your backend today