Android Development

Agenda

  • Android background
  • Install the environment
  • Create your first app in Eclipse
  • Launching and debugging

What is Android?

History

  • 2003 Android, Inc founded by Andy Rubin and others
  • 2005 Google buys Android, Inc
  • 2007 Apple launches the iPhone
  • 2007 Google announces Android
  • 2008 First phone available for purchase (HTC Dream)
  • 2010 Apple launches the iPad
  • 2011 Android tablet version and big update 4.0

Versions

Version Features

  • 2.1 Eclair, 2.2 Froyo, 2.3 Gingerbread
    • Live wallpapers. Account Manager.
    • Store apps on SD card. Cloud to device messaging.
    • Multiple cameras. Gyroscope and more sensors.
  • 4.0.3 Ice Cream Sandwich
    • Fragments. Action bar. New animation framework.
    • GridLayout. Resizable widgets. WiFi Direct.
  • 4.1+ Jelly Bean
    • Multi-user support. Lock screen widgets. Second display support.
  • 4.4 KitKat
    • WebViews uses Chromium, Immersive mode, Translucent ActionBar
  • 5.0 Lollipop
    • Material design

Installation

  • Always start with Java JDK (recommend latest version of Java SE)
  • Choose your development environment
    • Eclipse with ADT plugin, or
    • Android Studio
  • Eclipse with ADT plugin options
    • Download bundle from android.com where ADT and Android SDK is included
    • Download latest Eclipse from eclipse.org and install ADT and Android SDK manually
  • Always finish install with
    • Start Android SDK Manager to download platform(s)
    • Start Android Virtual Device Manager and create a virtual phone for the emulator

Installation problems

  • No Java found
    • Create environment variable JAVA_HOME and point to the directory where Java is, and add the Java directory + bin to the PATH.
    • It's not enough to have JRE (Java Runtime Environment). You need to install the JDK (Java Development Kit).
  • Incorrect bit-version of Java 32/64
    • Both Java and Eclipse come in different 32- and 64-bit versions. Don't mix them!
    • If you have both 32- and 64-bit Java installed, make sure JAVA_HOME and the PATH points to the corrent one (depending on your Eclipse choice).

Tools in the Android SDK

  • Android SDK Manager
    • Download platform versions and other components and updates
  • Android Virtual Device Manager (AVD)
    • Create and run virtual phones with different screen sizes and other features where you test your apps
  • Android Emulator
    • Run a virtual phone created with the Virtual Device Manager
  • DDMS
    • Show and investigate the insides of a running phone, physical or virtual

Android SDK Manager

  • Start
    • Menu/button in Eclipse
    • Run "android sdk" from the command line inside the tools directory of the SDK
    • On Windows, you can also start "SDK Manager.exe"
  • Default list
    • Latest version of Android automatically selected
    • Accept this and Install
  • Extras
    • Intel x86 Emulation Accelerator (HAXM)
    • Android SDK Manager only downloads the install!
    • You need to start the install yourself

What's in a platform version?

  • SDK Platform
    • Libraries for building Android apps to run on any device
  • Google APIs
    • Libraries for building Android apps with access to Google APIs to run on Google licensed devices
    • Specifically Google Maps and the Play Services library
  • ARM EABI v7a System Image
    • A binary for running virtual phones in the emulator, emulating the ARM processor (slow)
  • Intel x86 Atom System Image
    • A binary for running virtual phones in the emulator using native Intel processor (faster). For this to work you also need to install the Intel x86 Emulation Accelerator.

Android Virtual Device Manager

  • Start
    • Button in Eclipse
    • Run "android avd" from the command line inside the tools directory of the SDK
  • Create new virtual devices
    • Choose an Android version for the phone
    • Choose an image (ARM or possibly x86 emulation)
    • Choose memory and other features
  • Start a virtual device in the Android Emulator
    • Use scaling to fit the emulator on your screen (recommend 8 inch ~ 0.5 scale)

Android Emulator

  • Very close to a real device
    • Slow to boot. Ok to use (hopefully). Keep it open!
    • Specify "Hardware keyboard" to type using your computer keyboard
    • Physical buttons on a physical device will be extra buttons in the Android Emulator window
  • 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

Android Emulator can be slow

  • Use x86 emulation (not ARM)
    • 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 Intel x86 Emulator Accelerator (HAXM)
    • Create virtual phone using Intel x86 image
  • Genymotion is an alternative based on Virtual Box

Eclipse

  • Open source IDE written in Java for developing all kinds of applications, not just Android apps
  • For latest version, download from http://eclipse.org
    • Eclipse for Java Developers is fine
    • Install ADT manually
  • Or download bundle of Eclipse (older version) and the Android SDK

Android Plugin for Eclipse

  • The ADT (Android Developer Tools) for Eclipse are essential to develop Android apps inside Eclipse
  • Manual install
    • 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.

Create a new project (1/3)

  • Choose "File, New, Android Application Project"
  • Application name
    • The title shown to users when installing, below icon etc. Example: My Test Application
  • Project Name
    • The name of the project folder in Eclipse workspace. Example: MyTest
  • Package Name
    • Unique (reverse) domain name that identifies the application to the platform. Does not have to be a registered domain or site. Example: se.mobileapplab.mytest

Create a new project (2/3)

  • Minimum Required SDK
    • Oldest supported Android version where installable
  • Target SDK
    • The newest Android version that the developer is aware of and might have optimized for
  • Compile With
    • Should always be the latest installed Android version to get the most recent tools/compilers
  • Theme
    • Default styles that will be applied to different components in the user interface
  • Use defaults for all other options and dialogs

Create a new project (3/3)

  • Sufficient to accept the defaults for all other options
  • Configure Launcher Icon
    • Choose Clipart, Choose and possibly change Background Color
  • Blank Activity
    • Will be used for all examples in the course. Other types of activities can be added later.

Eclipse basics

  • Workspace
    • A folder on disk that contains projects in subfolders and has a ".metadata" folder with settings
  • Project / Package
    • A package in Java is a collection of one or more classes with the same package name. Usually the same as a project, but a project can link to other packages/libraries
  • Perspective
    • The contents of the Eclipse window, optimized for Writing code (Java) or debugging (Debug)
  • View
    • Rectangular area/panel in the perspective with specific content, commands etc. As tabs and dockable panels

Essential folders and files

  • src
    • Java source files
  • res
    • Important resource files in both xml and binary format. Different subfolders for different types of devices using qualifiers
  • AndroidManifest.xml
    • Configuration of the app like title, included activities, permissions etc

Essential app objects

  • Activity
    • The basic object of all Android apps. Without an Activity, nothing will be displayed on the screen. An app must contain at least one Activity, but can contain more.
  • Context
    • Generalization of the resources and other objects that relates to the application. You will use it as an argment in many calls, but you don't need to know anything more about it and you can always use the Activity (this) for the course's examples.

Essential visual objects

  • View
    • The basic visual object when building the user interface. All specific visual components are derived from the View. Can be buttons, input fields, formatted text, images etc.
  • ViewGroup (layouts)
    • A special View that contains other Views layed out on screen according to some rule(s). Can be a RelativeLayout, a LinearLayout or some other ViewGroup-derived class.
  • Fragment
    • Also a layout that contains Views, but with added intelligence that makes it behave between a ViewGroup and an Activity. Was introduced in Android 4.0.

Eclipse keyboard shortcuts

  • Ctrl+1
    • Quick fix. Display menu with options for automatic changes
  • Ctrl+Space
    • Show list of possible members for current object
  • Ctrl+O
    • Navigate inside of current file. Show list of class members
  • Ctrl+E
    • Navigate between opened files. Show list of all opened files (windows)
  • 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
  • Start your app in Debug mode with F11
    • Will only work when java file is open! (not xml file)
    • First time, right click on the project and select "Debug As, Android Application"
    • Let Eclipse automatically switch to the Debug Perspective
      • Switch back when you're done with the "Java" button in the top right corner

Get out of an Eclipse jam

  • Have you tried turning it off and on again?
    • File, Restart
  • Project, Clean will delete all temporary files
    • If it claims that an R.id constant doesn't exist, when it does
    • When building errors doesn't make sense
  • 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
  • Delete ".metadata" folder in workspace
    • Eclipse settings will be lost - only last resort!

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

Debugging 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

Logging

  • Console View in Eclipse
    • Commands that Eclipse runs when building and launching
    • Make sure to select the Android view (not DDMS)
  • LogCat View in Eclipse
    • Messages from the running Emulator or a connected device
    • Verbose, but can be filtered
  • 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

Deciding on an Android version

  • One single application
    • Can run on many different Android versions
    • Can check and adjust itself to take advantage of newer features on newer versions
    • If you call a member that does not exist on older version, app will crash
  • Multiple versions of the application
    • Build separate applications with different "Minimum Required" versions
    • Some/much shared code
  • Statistics of versions in use

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. Case sensitive. Note "android:" prefix
  • Wrong onClick method
    • Validity of "android:onClick" is not checked and app crashes if not matched to method in Activity. Method must be public void and take View argument
  • Forgetting to request a permission
  • Forgetting to declare extra Activities in AndroidManifest.xml

The resulting app

  • Single apk file
    • Stored in bin subfolder of project
    • Standard zip file with compiled Java code, resources etc
  • Deploy app to phone (without Eclipse integration)
    • Transfer to a physical phone via USB and the DDMS tool
    • Download from any web site, if allowed in system settings (Unknown Sources)