SERIOUS ABOUT SOFTWARE
Android App Development
Juha-‐Ma5 Liukkonen
Contents
• IntroducBon to the Android PlaDorm
• Android Concepts for SW Designers
• App Elements
• CreaBng and Deploying an App, step-‐by-‐step
• Advanced programming topics
• DistribuBng your App
Extensive documentaBon available at:
hNp://developer.android.com
IntroducBon to Android
Android Basics
• Android is a Linux based plaDorm
• PlaDorm = kernel, libraries, system services, user interface framework
• Linux kernel provides hardware abstracBon and core services
• Standard libraries: WebKit, SQLite, OpenSSL, …
• The C library is BSD based – Linux “standard” is GNU glibc
• Primary applicaBon environment: Java VM
• NaBve components for performance
• Can use all system resources
• Must be compiled for each hardware target separately
• Java applicaBons for easy portability
• Google’s Dalvik VM performs run-‐Bme translaBon to machine code
• Limited memory footprint, limited access to hardware resources
Android Architecture
Android APIs and compaBbility
• API = ApplicaBon Programming Interface
• Sets of library rouBnes you can call from your applicaBon
• Provide access to system services and applicaBon framework uBliBes
• Android has 7 API levels in acBve use
• API 4 = Android 1.6, …, API 11 = Android 3.0
• API levels are forwards compa,ble, i.e. an app wriNen for API level 4 will work on Android 1.6+
• Varying device features
• CPU speed, screen sizes, touchscreen/keypad, microphone, …
• Your app must declare what features it requires, so that it can only be installed on devices with the required features present
This is what is meant by
Android Next GeneraBon
• Android <3.0 is intended for touch-‐screen mobile phones
• Screen sizes up to 4”, resoluBon up to 800x480
• Somewhat clunky support for keypad-‐only soluBons
• Usually 3 hardware keys – back, home, menu
• Physical keyboard supported as an addiBonal input method
• Android 3.0 is intended for touch-‐screen tablets
• Screen sizes from 7” up, resoluBon from 800x480 up
• Quite different UI paradigm from <3.0
• Hardware keys implemented as touch keys
• Merge of the two lines ~ 3.1 (“Ice-‐cream Sandwich”)
This does not exactly help with the fragmentation part.
Android UI basics
Home screen contains widgets and shortcuts. Top of screen
Application menu contains launcher icons for all apps installed into the device.
Long press usually opens a pop-up context menu.
Apps versus widgets
• ApplicaBons in Android are full-‐screen
• In Android 3.0, they may be shown in “thumbnail” size in the task switcher
• No concept of overlapping windows
• Widgets are parBal-‐screen mini-‐applicaBons
• Limited layout opBons, limited UI elements
• Usually show a minimal view to some app’s status or data, e.g. currently playing music, weather, or news headlines
• An applicaBon may include also a widget
• Basically, a mini-‐view to the app content, or a controller interface to a background service
Android Concepts
for SW Designers
App model for mobile world
• Android apps are not regular Java SE/ME/EE apps
• Language used is Java
• System libraries are Android specific
• There is no public sta,c void main()
• ApplicaBon components derived from 1 of 4 Android base classes
• AcBvity: represents an UI view
• Service: a background component doing e.g. network acBvity
• Content provider: manages shared app data, e.g. an SQLite database
• Broadcast receiver: reacts to system state changes
• An app may have 1 or more of each kind of components
• The system may suspend or kill components based on system resource state (~component-‐level garbage collecBon)
Google is using some Java sources in conflict with the original license. Lawsuit with Oracle is in progress.
Key for mobile app: save power.
Do work only when you have to.
SDK and NDK
• SDK = Sorware Development Kit
• Consists of Java compiler, libraries, ant, packaging tools
• Used to build installable applicaBon packages (.apk) from Java source files
• NDK = NaBve Development Kit, add-‐on to the SDK
• Consists of C/C++ compiler, libraries, headers, make, packaging tools
• Used to build installable applicaBon packages (.apk) from C/C++ source files
• Both available to mulBple plaDorms
• Linux, Mac OS X, Windows
NDK until Android 2.3 was rather limited.
Now can build shared libraries etc.
Eclipse
• IDE = Integrated Development Environment
• Android SDK integrates with and is managed from Eclipse
• Eclipse is a Java-‐based IDE available for all common plaDorms
• Code compleBon, online help, debugger, deployment to target device, …
They used to say Emacs comes with the kitchen sink…
Ge5ng ready to develop
• Follow instrucBons in hNp://developer.android.com/sdk/index.html
• Install Eclipse (from hNp://www.eclipse.org/downloads/ )
• Install the SDK starter package (from the developer.android.com URL)
• Install the ADT (Android Developer Tools) plugin from Eclipse
• Configure the Eclipse ADT plugin to find your SDK installaBon
• Use the ADT to install SDK components, e.g. support for different API levels
• OpBonal: download and install NDK
• There is a lot to download and install
• Reserve an hour or so to get everything set up Step 1: Get started Step 2: …
Step 3: Victory!
App Elements
The Manifest
• AndroidManifest.xml defines your applicaBon
• Java package name of your applicaBon
• AcBviBes and other components your applicaBon may contain
• Device features that your applicaBon may require
• Intent filters – will your app appear in the app menu, which acBons to associate to, et cetera
• Permissions your app needs in order to work
• Permissions your app requires from others to serve their intent requests
• Minimum API level required
• List of libraries your app needs to be linked against
• Crucial to understand for all but the most trivial apps
• Eclipse creates a skeleton for you, enough for Hello World
Resources
• Stored in subdirectory res in your applicaBon source tree
• Drawables: icons, bitmaps, animaBons, colors
• Layout: XML view definiBons
• Menu: XML menu definiBons
• Strings: text strings that appear in your user interface
• LocalizaBon
• Provide localized resources in a subdirectory res/resourcetype-‐localename, e.g.
res/values-‐fi
• Hardware variance adaptaBon
• Provide variant resources in a subdirectory res/resourcetype-‐variant, e.g. res/
drawables-‐xlarge
• Variants recognized for API levels, DPI, primary input method, …
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
</resources>
res/values/strings.xml
Manifest example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.home">
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:persistent="true"
android:label="@string/home_title"
android:icon="@drawable/ic_launcher_home">
<activity android:name="Home"
android:theme="@style/Theme"
android:launchMode="singleInstance"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="Wallpaper"
android:label="Wallpaper"
android:icon="@drawable/bg_android_icon">
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Refers to the home_title string in localized res/values
Refers to the ic_launcher_home drawable in res/drawables variant
View DefiniBon XML
• Defines your AcBvity view layout
• WYSIWYG editor in Eclipse to make life easier
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
“R” is the class for accessing resources from Java code.
res/layout/main_layout.xml
Java Code
• Defines your component behavior
• Classes derived from AcBvity, or other Android base classes
• “TradiBonal” Java programming to put the resources into use
package com.example.android.miniapp;
import android.app.Activity;
…
public class MiniApp extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
} }
com/example/android/miniapp/MiniApp.java
CreaBng and Deploying an App
Basic steps
• In Eclipse, File -‐> Create New Project, Android ApplicaBon
• At this point you fill in a lot of the manifest content, e.g. API level
• Eclipse generates the directory structure and skeleton files for you
• Add resources
• For example, a picture (right-‐click on project, choose Import)
• Open the layout/main.xml and add some user interface elements
• E.g. a buNon; take note which resource idenBfiers the elements get
• Add funcBonality to your Java code
• For example, a buNon click handler
• Run your app!
• Either in emulator, or over USB on your Android device
Eclipse: create Android project
IniBal Manifest content
Several example apps that can be used as templates – utilize them!
Android API level. “Google APIs” = APIs to access Google cloud services such as Maps.
App name, Java package name.
The wizard can create an Activity skeleton for you.
ResulBng manifest
Eclipse: visual UI builder
UI elements available
Add UI elements
Simply drag-and-drop an element from the palette to your layout.
This is the identifier you use in Java code to refer to the element (R.id.button1).
Add resources
And voilá!
LocalizaBon
When the UI is complete, copy-paste the values directory to values-locale and localize the string values.
Eclipse: create Java code
Skeleton Activity created by the Wizard. Note how UI elements are referenced using their id.
Eclipse: run on Target!
Click the “Run” button in the Eclipse toolbar.
Be patient, the emulator takes a while to start.
If you have an Android device connected using USB, your app will run directly in your device.
Advanced programming topics
State saving
• Android will kill your components if resources are scarce
• Your app should implement the onSaveInstanceState() and
onRestoreInstanceState() methods to save e.g. currently entered data
• You can be proacBve and handle content in onPause() and onResume() (app going to background / resuming to foreground)
• State is saved into a Bundle
• As name-‐value pairs
• Using the putString() APIs (putArray, putInt, putBoolean, …)
• The Bundle is provided to your onCreate() method as parameter
• Is null if there is no saved state
• Otherwise can be accessed using the getString() APIs
Game programming
• Simple games can be wriNen using Java
• Limited amount of memory available
• Limited support for hardware acceleraBon
• Possibly accessing naBve components using JNI
• Graphics or memory intensive apps require naBve code
• Can uBlize all memory
• Can access GL/ES directly
• Can uBlize hardware features such as NEON (ARM vector instrucBons)
• Differences in Android 3.0
• UI acceleraBon with a boolean flag in manifest, no naBve code required
• Full OpenGL based games sBll need to go naBve
DistribuBng your App
The Android Market
• An on-‐line app store
• Accessible to most Android devices out there, via the Market app
• Hosts around 200.000 Btles
• Most Btles are ringtones, wallpapers, assorted uselessness – but some real gems are included
• Has exceeded iPhone app store revenues for some developers in 2011
• Ge5ng your app to Market is easy
• Test your app thoroughly first!
• Export your app from Eclipse, your .apk gets digitally signed and opBmized for distribuBon
• Go to hNp://market.android.com/publish
• You can also use other means to deliver .apk files to customers
• InstallaBon not limited to Market
In Conclusion
• Android is a funcBonal mobile device plaDorm
• Many aspects perpetually in classic Google “beta” state
• But it’s out there, in rapidly increasing numbers
• The fragmentaBon causes some headaches to app developers
• Developer tools are funcBonal
• If you are familiar with Eclipse, developing for Android should be a breeze
• Android online documentaBon is comprehensive and clear
• Lots of examples provided with the SDK
• Android offers rich APIs
• MulBmedia, VoIP, Google services, …
• Easy to create reasonably complex apps, possible to create even very demanding ones