Overview of the Android SDK

Goals

  • Know about the essential tools in the Android SDK
  • Get familiar with Eclipse
  • Create and start a virtual Android phone using the Emulator

Android SDK

  • Contains tools and files necessary to build Android applications
  • Possible to build applications with only the SDK and its command line tools
  • Recommended to use an IDE (Integrated Development Environment) like Eclipse
  • No installation, just unzip the files
  • Many, if not all, tools require Java to run

Android SDK Manager

  • Tool in the SDK to download and update parts of the SDK
    • Menu command in Eclipse under Window menu
    • Run "android sdk" from the command line inside the tools directory of the SDK
    • On Windows, you can also start "SDK Manager.exe"
  • Find and update any new versions
  • Add specific versions of the Android platform
  • Add tools and libraries not installed by default

Android Virtual Device Manager

  • Tool in the SDK to create virtual devices and then launch them inside the Android Emulator
    • Menu command in Eclipse under Window menu
    • Run "android avd" from the command line inside the tools directory of the SDK
    • On Windows, you can also start "AVD Manager.exe"
  • Create new virtual devices
    • Choose an Android version for the phone
    • Choose an image (ARM or possibly x86 emulation)
    • Choose memory and other hardware features (emulated)
  • Start a virtual device in the Android Emulator

Android Emulator

  • Very close to a real device
    • Slow to start. Ok to use (hopefully). Keep it opened.
    • Specify "Hardware keyboard" to type using your computer keyboard
    • Physical buttons on a physical device will be extra buttons in the Android Emulator window
  • Can be scaled/resized when running on computers with smaller screens
  • Each running emulator has a local tcp port number for control/communication
    • adb is a command line tool to communicate with the emulator
    • Multiple running emulators can "call" each other using the port number

Eclipse

  • Open source IDE written in Java for developing all kinds of applications, not just Android apps
    • Most used for Java development, but also supports (via plugins and custom builds) C, C++, HTML, JavaScript and more.
  • Download from http://eclipse.org
    • Different downloads just differs in what default plugins are included. Use "for Java development" when building Android applications, or download the package from the Android site since it nowadays also includes Eclipse (but maybe not the latest version).
  • Customizable
    • Eclipse is very customizable and competent with a lot of features you might find in other IDEs like Microsoft Visual Studio. It does depend a lot of local settings and files created during its use that may be corrupted or introduce errors in the environment. Restart Eclipse or even remove the .metadata folder in your Workspace to "reset" the environment if strange errors occur.

Android Plugin for Eclipse

  • The ADT (Android Developer Tools) for Eclipse are essential to develop Android apps inside Eclipse
  • Included and installed when downloading the Android SDK
  • Manual install
    • If you want the latest version of Eclipse, first download and run Eclipse
    • Choose "Help, Install New Software"
    • Click Add button to add a new plugin listing (called Repository)
    • Set name to whatever you like, for instance "Android"
    • Set url to http://dl-ssl.google.com/android/eclipse/
    • Click Ok, and then select the Developer Tools with a checkbox and continue with Next until Finish

Tell Eclipse where Android SDK is

  • After installing the Android Plugin (ADT), restart Eclipse
  • Tell Eclipse where the Android SDK is on disk
    • Eclipse will ask for this location when restarting the first time after the ADT has been installed
    • Can also be set/changed inside Eclipse
    • Choose "Window, Preferences, Android" and specify "SDK Location"
    • The list below "SDK Location" in this dialog box should contain all downloaded Android versions
    • This setting is stored as part of your Workspace, so changing to an empty Workspace means this might need to be specified again.

Eclipse basics

  • Workspace
    • A folder on disk that contains multiple projects in separate subfolders and has a ".metadata" folder where Eclipse-settings are stored and remembered between Eclipse sessions.
  • Project / Package
    • A package in Java is a collection of one or more classes with the same package name (the name that looks like a reversed domain name). Usually the same as a project, even though a project can also link to other packages/libraries.
  • Perspective
    • The contents of the Eclipse window. Different perspectives are optimized for different stages during development like a "Java" perspective while writing code and a "Debug" perspective when running and finding bugs.
  • View
    • Part of a perspective, a visual rectangular area/panel in the perspective. Can be organized next to each other or as tabs on top of each other in single area

Eclipse keyboard shortcuts while editing code

  • Ctrl+1
    • Quick fix. Display menu with options to fix/add/change problems in the code.
  • Ctrl+Space
    • Show list of possible choices at current position in the code. Like Intellisense in Microsoft Visual Studio.
  • Ctrl+O
    • Navigate inside of current file. Show list of class members (functions). Move with arrows and go to selected member with Enter.
  • Ctrl+E
    • Navigate between opened files. Show list of all opened files (windows). Move with arrows and show selected file with Enter.
  • Ctrl+W
    • Close the current file/window.

Building and running in Eclipse

  • Eclipse is set to build automatically while you are writing code
  • Any build errors are shown continuously in the "Problems" view
    • Note that some errors are just an effect of you not being completely finished with your code
  • Debug
    • You seldom just "Run" your code, usually you "Debug" while running to any errors are caught
    • Keyboard shortcut F11
    • First time, to get Eclipse to recognize running it as an Android app, right click on the project and select "Debug As, Android Application"
    • Let Eclipse automatically switch to the Debug Perspective while running and debugging
      • Switch back when you're done with the "Java" button in the top right corner
    • Keep emulator running for faster start on next Debug

Get out of an Eclipse jam

  • Eclipse can sometimes give unexplainable errors
  • Have you tried turning it off and on again?
    • Choose "File, Restart"
  • Clean build
    • Choose "Project, Clean" and choose all or the current project
    • If it claims that an R.id constant doesn't exist, when it does
    • When building errors doesn't make sense or can't be explained
  • Reset perspective
    • Choose "Window, Reset Perspective"
    • If the user interface is behaving strange, panels missing
    • If it is complaining that a file doesn't exist, that is clearly there

More Eclipse tips

  • Opened files can come from more than one project
    • Common to work in more than one project and open multiple files. Since many files in Android have the same name across projects, it is quite easy to work on a file in another project than what you THINK you are working on.
  • Location of Android SDK
    • If Eclipse have trouble finding the Android SDK, check it under "Window, Preferences, Android, SDK Location". The list below the path must have at least one Android platform version.

Errors while running your app

  • Run-time errors are called Exceptions
    • An Exception is thrown by the platform when a run-time error is discovered
  • When an Exception occurs
    • Eclipse will halt at location where exception was thrown
    • Eclipse switches to Debug Perspective
      • Unfortunately not always showing the location in your own code where the error is (depending on what kind of exception has occured). Look at least at the type of exception and any additional information in the Variables panel of the Debug perspective.
  • Set your own breakpoint (stop) by double-click in left margin before code line

Common errors

  • Syntax errors in layout xml file
    • Typing wrong element or attribute names in the xml file is common and are usually not discovered until starting your application. Note that most names are case sensitive. Also note that most attributes is prefixed with "android:". The exception thrown is seldom helpful.
  • Wrong onClick method
    • If the value of the "android:onClick" attribute does not match the method in your Java class, this is not discovered until the application runs. The name much match exactly, including case, but the method must also be "public void" and take a View as its single argument.
  • Forgetting to request a permission
    • Some operations like performing a network request requires a permission to be specified in your AndroidManifest.xml file. Without it, an exception will be thrown the first time network communication is attempted.
  • Forgetting to declare extra Activities
    • If your app has more than one Activity, it is common to forget that besides creating a layout xml and an Java source code file you also need to declare it in AndroidManifest.xml.

Debugging commands and keyboard shortcuts

  • Step through line-by-line
    • F5 - Step Into, will show/step inside of your own methods
    • F6 - Step Over, will NOT how/step inside of your own methods
    • F8 - Resume, run full speed until next breakpoint, end or an exception occurs

Faster Android Emulator

  • Android Emulator is slow
    • Because it is as close to a physical device as is possible. A full compile of the Android platform for ARM processors. Since it needs to emulate the ARM processor, it will always be slow.
    • Not always a bad thing. The advantage is that the speed of emulator will not fool you with regards to how fast your code will run on physical devices.
  • Use a x86 image instead
    • A full compile of the Android platform, but for Intel x86 processors
    • This will be faster since it means that no ARM processor emulation is necessary
    • Requires installation of "Intel x86 Emulator Accelerator (HAXM)"
      • Install it from the Android SDK Manager (under Extras). Requires administrative access on your computer.
    • Then install "Intel x86 Atom System Image" for the Android version
    • And create a virtual device that uses the "Interl x86..." image instead

Logging

  • Console View in Eclipse
    • This view shows commands that Eclipse runs when building, debugging etc. The source of these messages is usually Eclipse or some other tool in the SDK. Make sure to select the Android view (not DDMS).
  • LogCat View in Eclipse
    • This view shows messages from the running Android Emulator or a physical device (if connected). These messages will be quite verbose, but can filtered to only show errors.
  • Output your own LogCat messages
    • You can also output your own messages to the LogCat View using Log.i, Log.w and Log.e calls from your Java code (corresponding to Information, Warning and Error log levels of importance).

DDMS (Dalvik Debug Monitor Server)

  • Communicates with a running Emulator or actual Phone/Device
  • Take screenshots from the device
  • Transfer files to/from the device
  • Show logs and debugging messages
  • Show status information about processes
  • Show network usage