Child pages
  • Getting started with the Android Developer Tools (Android 5.1)
Skip to end of metadata
Go to start of metadata

This page contains instructions for getting started with the Android IDE and developer tools provided by the Android Open Source Project (AOSP). It is intended for student with no previous experience with developing application on the Android platform, and as a reference for students with some prior experience.

See also: Android developer documentation and API reference.

The instructions and are divided into seven parts:

  • Install the Android IDE and SDK - This will guide you through the initial installation of the Android IDE and developer tools.
  • Download the latest SDK tools and platform using the SDK Manager - This will tell how to keep the SDK tools and platforms images up-to-date and how to install optional features to the SDK.
  • Create an Android Virtual Device and start the emulator - This will guide you through creating an Android Virtual Device (AVD) and starting the virtual device in the Android emulator.
  • Create and run an application project - This will guide you through the creation of an application project and running the application in an Android emulator instance.
  • Setup a project to use Google Play services - This will guide you through setting up a project to use the Google Play services library.

  • Mock GPS location in the Android emulator - This will guide you through mocking GPS location in the emulator.
  • Troubleshooting - For when things go horribly wrong...

Install the Android IDE and SDK

$ unzip android-studio-<version>-<os>.zip -d /path/to/android-studio/parent/dir/

  • Launch Android Studio from the extracted android-studio/bin directory:

$ /path/to/android-studio/bin/studio.sh &

  • Follow  the on-screen instructions to fetch the Android SDK

Download the latest SDK tools and platform using the SDK Manager

  • Start the SDK Manager, using one of the following ways:
    • in Android Studio, by selecting the Tools > Android > SDK Manager menu entry and clicking the Launch Standalone SDK Manager link in the dialog that appears.
    • on Windows, double-click the SDK Manager.exe file in the C:\Users\<USERNAME>\AppData\Local\Android\sdk directory.
    • on Mac or Linux, in a terminal execute:

$ /path/to/android-studio/sdk/tools/android sdk

  • In the SDK Manager window, select the packages you want to download by toggling the checkboxes on the left, then click Install to install the selected packages.

You should make sure you install at least the following packages for Android 5.1.1:

    • SDK Platform
    • Google APIs
    • Google APIs Intel x86 Atom System Image

You should also install the following packages from the Extras category:

    • Android Support Repository
    • Android Support library
    • Google repository

 

Create an Android Virtual Device and start the emulator

  • Start the Android Virtual Device Manager, using one of the following ways:
    • in Android Studio, by selecting Tools > Android > AVD Manager from the menu of by clicking the  icon in the toolbar.
    • on Windows, double-click the AVD Manager.exe file in the C:\Users\<USERNAME>\AppData\Local\Android\sdk directory.
    • on Mac or Linux, in a terminal execute:

$ /path/to/android-studio/sdk/tools/android avd

  • In the Android Studio AVD Manager panel, create a new AVD by selecting Create Virtual Device...

  • In the Select Hardware dialog of the Virtual Device Configuration wizard, select the Nexus 5 device definition and click Next.

  • In the System Image dialog, select the x86 Lollipop system image for the Android 5.1 (with Google APIs) target and click Next.

  • In the final dialog, enter an AVD Name, ensure that Use Host GPU is selected and complete the AVD creation by clicking Finish.

  • Launch the AVD from the AVD Manager by clicking the  icon beside the AVD.
  • Wait until the emulator boots up, then unlock the emulator screen.

Create and run an application project.

  • In Android Studio, select File > New Project...
  • In the Create New Project dialog enter an Application Name, Company Domain (which determines in the package name).
  • If you wish to create the project directory in a custom location, enter the desired location in the Project location box.
  • Proceed by clicking Next.

  • In the following dialog, check the Phone and Tablet checkbox and click Next.

  • In the next dialog, select Empty Activity and proceed by clicking Next.

  • In the next dialog, enter an Activity Name, if you wish, and finish the wizard by clicking Finish.

The newly created Android project includes a default set of "Hello World" source files that allow you to immediately run the app:

  • Make sure you have created an AVD and started the AVD in the emulator.
  • Open one of your project's files and select Run > Run 'app' from the menu or click the icon in the toolbar.
  • In the Choose Device window that appears, select the running device and click OK.

Android Studio will proceed to build the application, install it to the connected device and start it.

Setup a project to use Google Location services

To make the Google Play services Location APIs available to your app, perform the following steps:

  • Open the build.gradle file for the app module:
  • Add a new build rule under dependencies for the latest version of play-services-location. For example:
    apply plugin: 'com.android.application'
    ...
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services-location:8.1.0' }
  • Open the application's AndroidManifest.xml and add the following tag as a child to the <application> element:
    <meta-data
     android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Mock GPS location in the Android emulator

  • In a running AVD, touch the circle on the home screen to to see all apps.

  • From the apps menu, start the Settings app and select the Location settings.
  • In Location settings, make sure the Location slider is set to ON.

  • Touch the Back button twice, until you are back at the apps menu.

  • From the apps menu, start the Google Settings app.
  • Touch Location and touch Yes in the dialog shown to enable Google apps to access your location.

  • Obtain a GPS trace in the GPS eXchange Format (GPX) (e.g from OpenStreetMap Public GPS traces).
  • Launch the Android Device Monitor in Android Studio by selecting Tools > Android > Android Device Monitor or by clicking the  icon in the toolbar.
  • In the Android Device Monitor, select the Emulator Control tab.
  • Make sure the emulator is running, then scroll down to Location Control in the Emulator Control view and click the GPX tab.
  • Click the Load GPX... button
  • In the the Load GPX File dialog browse and select a .gpx file , then click OK.
  • In the Emulator Control  tab, select the GPS trace from the list.

  • Click play  to start playback of recorded GPS trace to emulator.

Troubleshooting

You might encounter the following error when starting the SDK Manager:

$ /path/to/adt-bundle-<os_platform>/sdk/tools/android sdk
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-gtk-3550 or swt-gtk in swt.library.path, java.library.path or the jar file
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
    at org.eclipse.swt.internal.Converter.wcsToMbcs(Unknown Source)
    at org.eclipse.swt.internal.Converter.wcsToMbcs(Unknown Source)
    at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
    at com.android.sdkmanager.Main.showSdkManagerWindow(Main.java:346)
    at com.android.sdkmanager.Main.doAction(Main.java:320)
    at com.android.sdkmanager.Main.run(Main.java:120)
    at com.android.sdkmanager.Main.main(Main.java:103)

Despite the error message, which hints at missing library dependencies, this error might also be caused by the /tmp filesystem being mounted with the noexec option set. If this is the case, remounting the /tmp filesystem with the exec option set should solve this issue:

$ sudo mount -o remount,exec /tmp


Starting the emulator fails with the following error shown in the Console output: PANIC: Could not open: MyVirtualDevice

This indicates that the emulator was unable to find the previously created Android Virtual Device image.

  • On Linux, the images are typically stored under /home/<username>/.android/avd
  • On Windows, the images are typically stored under C:\Users\<username>\.android/avd

The path from which the Eclipse Android Virtual Device Manager lists AVDs is shown at the top of the Android Virtual Devices tab.

To make sure that the emulator looks for the device images at the correct location, create and environment variable called ANDROID_SDK_HOME and point it to the parent of the .android directory.


Unfortunately MyAndroidApp has stopped, and the LogCat view in Eclipse displays a stack trace similar to the following:

FATAL EXCEPTION: main
Process: fi.helsinki.cs.mobisec.mylocationapp, PID: 1637
java.lang.RuntimeException: Unable to start activity ComponentInfo{fi.helsinki.cs.mobisec.mylocationapp/fi.helsinki.cs.mobisec.mylocationapp.MyLocationActivity}:
  java.lang.SecurityException: Not allowed to bind to service Intent { cmp=fi.helsinki.cs.mobisec.mylocationservice/.MyLocationService }

  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  at android.app.ActivityThread.access$800(ActivityThread.java:135)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5017)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=fi.helsinki.cs.mobisec.mylocationservice/.MyLocationService }
  at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1580)
  at android.app.ContextImpl.bindService(ContextImpl.java:1544)
  at android.content.ContextWrapper.bindService(ContextWrapper.java:517)
  at fi.helsinki.cs.mobisec.mylocationapp.MyLocationActivity.doBindService(MyLocationActivity.java:173)
  at fi.helsinki.cs.mobisec.mylocationapp.MyLocationActivity.onStart(MyLocationActivity.java:148)
  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
  at android.app.Activity.performStart(Activity.java:5241)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
  ... 11 more

This indicates that the application in quest does not have sufficient privileges to bind to the indicated service. To remedy this, make sure that the use of the appropriate permissions is declared in the AndroidManifest.xml for the affected application. Also make sure, that the application which declares the permissions is deployed to the device before the application which uses the permissions.


A call to GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable( ) fails with SERVICE_VERSION_UPDATE_REQUIRED error code and logcat shows shows e.g.

01-08 12:25:03.920 2457-2457/fi.aalto.cs.mss.mylocationservice W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8487000 but found 8115270

This indicates that the version of Google Play Services available on the target device is older than the com.google.android.gms:play-services-location client library linked to your application. Check the version of Google Play Services from Settings > App > Google Play Services on the device (8.1.15 on the 5.1 emulator images at the time of writing). 

Make sure the major and minor version number in  compile 'com.google.android.gms:play-services-location:<version>'   in the application modules build.gradle matches the version on the target device. A list of available client library versions can be found at /path/to/android-studio/sdk/extras/google/m2repository/com/google/android/gms/play-services-location/.

  • No labels