Post on 19-Dec-2015
Programming the Android Platform
The Android Development Environment
Topics
Getting started on the Android Platform Installing required libraries Programming Android using the Eclipse
IDE The Android emulator Debugging Android applications Other tools
Installation
See: developer.android.com/sdk/
index.html#quickstart Software to download & install
1. Java Development Kit (JDK)2. Eclipse IDE3. Android SDK starter package4. Eclipse ADT plugin5. Add Android platform & other comps to SDK
Java
Download Java Development Kit www.oracle.com/technetwork/java/javase/
downloads/index.html
Eclipse
Preferred IDE, but you can use others Requires version 3.5 or higher
Eclipse Classic recommended http://www.eclipse.org/downloads/
Android SDK starter package
Core tools needed to get started developer.android.com/sdk/
Unpack files to a directory of your choice By default: android-sdk-<machine-
platform> Add this directory to your path to use
Android tools from the command line
Eclipse ADT plugin
Download the ADT plugin Use the Help -> Install New Software
function URL for Eclipse 3.5+▪ https://dl-ssl.google.com/android/eclipse/
In the Available Software dialog, select the checkbox next to Developer Tools and click Next
Configure plugin Open preferences, click on “Android” and
set the SDK location
Add Components to SDK
Launch the Android SDK and AVD Manager
Select at least the latest platform (2.3)
Create an Android Virtual Device An Android Virtual Device (AVD) is a
configuration of emulator options An AVD includes:
A hardware profile A platform Other options▪ e.g., emulator skin, screen dimensions, SD card size
A storage area Create using Android SDK and AVD
Manager tool
HelloAndroid
Start Eclipse Create a new Android Project
Hello Android
Project name: HelloAndroid Application name: Hello, Android Package name: course.examples Create activity: HelloAndroid Min SDK: 9
Hello Android (cont.)
package course.examples;
import android.app.Activity;import android.os.Bundle;import android.widget.TextView;
public class HelloAndroid extends Activity {public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); }}
The Android Emulator
Applications can run in an emulator Advantages
Fast turnaround Doesn’t require an actual phone Uses faster CPU on emulation computer
Disadvantages Some features unavailable▪ e.g., no support for bluetooth or USB connections
Performance & user experience can be misleading
The Android Emulator
Emulates telephone calls & SMS messages% telnet localhost 5554 > sms send 5554 “This is a text message”
Will emulate typical speeds & latencies for different networks (e.g., edge, gprs, etc.)% telnet localhost 5554 > set speed gprs
Can interconnect multiple emulators% emulator -avd Android_2.2_primary% emulator -avd Android_2.2_secondary In one dialer app, dial port num of other emulator
The Android Emulator
Many more options See:
developer.android.com/guide/developing/tools/emulator.html
Debugger
Execution Tracing
Execution Tracing
Can also add trace code into your application
// start tracing to "/sdcard/calc.trace" Debug.startMethodTracing("calc"); …// stop tracing Debug.stopMethodTracing();
Will need to set permissions (discussed later)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE
/>
TraceView
To view results after execution
% adb pull /sdcard/calc.trace /tmp% traceview /tmp/calc
TraceView
Logcat
Log viewer Android logs many events by default Can use android.util.Log &
android.os.Debug class for application-specific logging
View log entries with % adb logcat
Or inside Eclipse Window -> Show View -> Other -> Android
-> LogCat
Hierarchy Viewer
Visual representation of application UI
% hierarchyviewer
Layout inspector
Pixel Perfect View
Monkey & monkeyrunner
Sends psuedorandom events to applications
Can also script events with monkeyrunner API
// send event to browswer% adb shell monkey -p com.android.browser -v 500
// send events to all applications% adb shell monkey 500
Android Unit Testing
Android-specific extensions to Junit MoreAsserts
Additional result checking classes ViewAsserts
Asserts about view layout TouchUtils
Classes for generating touch events Instrumentation
For monitoring application interaction with system
Android Unit Testing (cont.)
Steps for testing Activity Create an Android test project Create one or more Android test classes▪ Add code to each Test class
Test code usually includes test methods for App initialization UI behavior & class functionality Application lifecycle state management
Android Unit Testing (cont.) AndroidTestCase
Test case with access to Activity Context ActivityUnitTestCase▪ isolated testing of a single activity
InstrumentationTestCase Test case with access to system instrumentation &
events ActivityInstrumentationTestCase2▪ functional testing of a single activity
Many other test case types developer.android.com/guide/topics/testing/
testing_android.html
Android Unit Testing (cont.)
Junit Test flow setup()
Put application in known state run()
Create additional data Execute method under test Check result with Assert
tearDown() Return to known state
Android Unit Testing (cont.)
developer.android.com/resources/tutorials/testing/activity_test.html
Running on a Device
Key steps Declare your application as
"debuggable" in your Android Manifest Turn on "USB Debugging" on your device Setup your system to detect your device
See: developer.android.com/guide/
developing/device.html
Lab Assignment