The Android Development Environment Adam Porter
Today s Topics Getting started on the Android Platform Installing required libraries Programming Android in Eclipse The Android emulator Debugging Android applications Other tools
Installation See http://developer.android.com/sdk/index.html#quickstart Steps Java Platform SE Eclipse IDE Android SDK starter package Eclipse ADT plugin Add Android platform & other comps to SDK
Java Download Java Development Kit http://java.sun.com/javase/downloads/
Eclipse Preferred IDE, but you can use others Requires version 3.4 or higher http://www.eclipse.org/downloads/
Android SDK starter package Core tools needed to get started http://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/ 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.2)
Create an Android Virtual Device AVD is a configuration of emulator options Each AVD includes Hardware profile Platform Other options e.g., Skin, screen dimensions, SD card size Storage area Can be created in Android SDK and AVD Manager
Hello Android Create a new Android project Project name: HelloAndroid Application name: Hello, Android Package name: edu.umd.cs.cmsc498g Create activity: HelloAndroid Min SDK: 8
Hello Android (cont.) package edu.umd.cs.cmsc498g; 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 Can run Android applications in a simulated environment Advantages Fast turnaround Doesn t require actual hardware Uses faster CPU on emulation computer Disadvantages Not all features are not available e.g., no bluetooth emulation Performance & user experience can be misleading
The Android Emulator Emulates telephone calls & SMS messages % telnet localhost 5554 > send sms 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 For more details, see: developer.android.com/guide/developing/tools/ emulator.html
Running in the Debugger
TraceView Automatically traces method entries and exits Add trace code to your application as follows: // start tracing to "/sdcard/calc.trace" Debug.startMethodTracing ("calc"); // stop tracing Debug.stopMethodTracing(); Will need to set permissions (disc. in later classes) <uses-permission android:name="android.permission.write_external_storage" />
TraceView To view results after execution % adb pull /sdcard/calc.trace /tmp % traceview /tmp/calc
Logcat The Android platform logs many events by default Developers can use the android.util.log to add application-specific logging View log entries with % adb logcat Or inside Eclipse Window -> Show View -> Other -> Android -> LogCat
Hierarchy Viewer % hierarchyviewer Layout inspector Shows runtime UI layout as a graph Pixel Perfect View Zoomable view of individual UI units Can manually force view refreshes
UI/Application Exerciser Monkey Send random events to applications Allows events to be scripted Feature is undocumented. // 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 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 http://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.) http://developer.android.com/resources/tutorials/ testing/activity_test.html
Running on a Device See http://developer.android.com/guide/developing/ device.html 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