Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

How to Use the Intel® Hardware Accelerated Execution Manager (Intel® HAXM) Android* Emulator with Google APIs

$
0
0

Abstract

Developers have been using the Intel® Hardware Accelerated Execution Manager (Intel® HAXM) based Android* emulator for developing Android* apps, but it had a limitation when it came to testing apps that use Google APIs. The x86 based Google APIs system image was added to Android SDK recently. In this article we look at how to install, configure and test the HAXM based emulator with Google APIs. We will create a sample app to demonstrate how you can use the Google APIs and test them with the HAXM based emulator.

Contents

Overview

The Intel HAXM Android emulator is extremely fast as it uses hardware accelerated virtualization. Developers can see great productivity gains in their Develop-Debug-Test cycle of app development. Until recently we were not able to test Android apps that use Google APIs, as there were no x86 based Google APIs emulator images.

We now have standard x86 Google API emulator images available in Android SDK. The article below on the Intel Developer Zone goes into more detail about how to setup and create an AVD configuration for running x86 Google API image.

https://software.intel.com/en-us/blogs/2014/03/06/now-available-android-sdk-x86-system-image-with-google-apis

There were some issues reported in the comments section. On some systems if you enable “Use Host GPU” checkbox for the emulator image, some Google apps (e.g. Maps) that use OpenGL related technologies seem to crash. Disabling the GPU emulation may fix the issue.

Install The Latest x86 based Google APIs Image

An updated bug fix version (v5) for the image has been released recently. Select and install the image as shown below.


Figure 1: Google APIs x86 Image in Android SDK

Create Android Virtual Device (AVD) Configuration and Test

Create an Android Virtual Device configuration for your target device profile of choice. The following screenshot shows the AVD configuration for Nexus 4 profile. Please note the “Target” option showing the x86 System with Google APIs with API level 19.


Figure 2: Selecting the correct target for AVD configuration

Start the emulator image, the “Starting Android Emulator” window will show the message “HAX is working and emulator runs in fast mode”, if HAXM is being used successfully.


Figure 3: Shows HAXM is being used successfully

Once the Android emulator image is booted, the apps screen should show some standard Google apps like Maps and Google Settings. Please refer to the following screenshot.


Figure 4: Google APIs x86 Image with Google Apps

Click on Google Maps to verify that your HAXM based x86 Google APIs image is working properly.

If you do not see the Maps displaying properly, check to see if your emulator has network connectivity by opening the browser app and testing your favorite website. By default, the emulator should automatically get network access and internet connectivity (if your host system has it). Using proxy configuration on a host system can sometimes cause network related issues in emulator.

http://developer.android.com/tools/help/emulator.html

Resolve all the network issues, and verify your android emulator can connect to the internet.

The following screenshot shows the Google Maps app running inside the Intel HAXM based Android emulator. It uses the AVD configuration (x86_19g) we created earlier.


Figure 5: Google Maps App running inside x86 Google APIs image

Install Google Play Services SDK

Before we can use Google APIs in our Android app, we first need to download Google Play Services SDK in Android SDK Manager.

The below article goes into detailed setup instructions.

http://developer.android.com/google/play-services/setup.html

Below is a screenshot of Android SDK Manager showing Google Play Services selected for installation. It is listed under the “Extras” section.


Figure 6: Install the Google Play Services using Android SDK Manager

At a higher level, Google APIs on Android depend on two main components:

  • The Google Play Services, which runs as a background system service on the Android OS. It is automatically updated by Google Play Store just like any other APK.
  • Client Library, which is a thin API wrapper with a consistent interface. This is part of the Google Play Services SDK we downloaded earlier. Developers need to make sure they use the latest, correct version in their apps.

The following article goes into more detail on how these 2 pieces work together to provide seamless access to Google APIs for Android Apps.

http://developer.android.com/google/play-services/index.html

Create a Sample App To Test Google APIs in x86 based Emulator

Next, we will create a basic app to try out Google APIs with x86 Android Emulator Image under HAXM.

Android Studio is used for creating the sample app. Click on File -> New Project to create a basic app with a single main activity. As our x86 Google API image is using API level 19, make sure to use the same one for Compile and Target options. The following screenshot shows all the options chosen for our sample app.


Figure 7: Creating a default app in Android Studio with correct API levels for x86 Google API image

Go ahead and build the app, and run it inside the emulator AVD configuration (x86_19g) we created earlier. By default, the app should display a "hello world” message.

We will now add Google Play service Client library to our app. It is easier with an Android Studio project; we just need to modify the build.gradle file under our app module (not the main project’s build.gradle). Simply add the reference to the latest google play services under the dependencies, highlighted in the following code snippet.


apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        packageName "com.gapisample.app"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:4.4.52'
}

Code Snippet 1 ++

The reference documentation goes into more detail: http://developer.android.com/google/play-services/setup.html

Google Play Services and Client Library provide access to several Google APIs – Maps, Drive, Location, Google+, Ads among others.

For our sample test app, we will use the Google Maps Android APIs. We need an API key to access the Google Maps Android API. Please follow the instructions in the following reference article to create your API key: https://developers.google.com/maps/documentation/android/start#obtain_a_google_maps_api_key

Once you have obtained the API key, modify the App manifest to add your API key, replace the text (<ADD YOUR KEY>) highlighted in green with your API key.

We have a few other additions to the app manifest: Google Play Service version, and depending on APIs used, the required permissions.

For Google Maps, we need internet, network_state, and external_storage at a minimum. Please refer to https://developers.google.com/maps/documentation/android/start#specify_app_settings_in_the_application_manifest for a detailed explanation.

The following code snippet shows the entire app manifest file used for our test sample app.

<?xml version=”1.0” encoding=”utf-8”?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gapisample.app"><uses-feature android:glEsVersion="0x00020000" android:required="true"/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"><activity
            android:name=".MainActivity"
            android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" /><meta-data android:name="com.google.android.maps.v2.API_KEY"
            android:value="<ADD YOUR KEY>"/></application></manifest>

Code Snippet 2 ++

We should now be able access Google APIs and invoke Google Map objects.

https://developers.google.com/maps/documentation/android/map

For a simple Google Maps invocation we can just use the MapFragment and replace our “hello world” layout with it. Modify the contents of the default “activity_main.xml” with the following code snippet, as mentioned in https://developers.google.com/maps/documentation/android/start#add_a_map

<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/>

Code Snippet 3 ++

Rebuild the app, and run it on the emulator. Instead of the “hello world” message, we should now see the entire space occupied by the MapFragment displaying basic google maps with default controls.


Figure 8: Google maps API working and tested with HAXM based Android emulator using sample app

Users can drag the map around and click on zoom controls to interact with the map, just like on a real device.  It provides a smooth experience with no noticeable artifacts, latencies or delays.

Summary

This article discussed installation, configuration and setup of x86 based Google APIs system image on the Intel HAXM based Android Emulator. We also discussed how to setup Google Play service SDK and created a basic app to test accessing of the Google APIs from the Emulator.

About the Author

Ashok Emani is a Software Engineer in the Intel Software and Services Group. He currently works on the Intel® Atom™ processor scale enabling projects.

Optimization Notice
Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel.
Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.
Notice revision #20110804

++This sample source code is released under the Intel OBL Sample Source Code License (MS-LPL Compatible)


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>