page.title=Getting Started with Auto page.tags="auto", "car", "automotive" page.article=true page.image=auto/images/assets/icons/auto_app_in_simulator.png @jd:body

Dependencies and Prerequisites

This class teaches you how to

  1. Set Up an Auto Project
  2. Build Auto Apps
  3. Run and Test Auto Apps

You should also read

Android Auto extends the Android platform into the car. When users connect their handheld devices running Android 5.0 or higher to a compatible vehicle, the Auto user interface provides a car-optimized Android experience on the vehicle's screen. Users interact with compatible apps and services through voice actions and the vehicle's input controls (like a touchscreen or dashboard buttons).

Auto currently supports two types of apps:

You can enable your existing audio and messaging apps developed for phones and tablets to work in the car, without having to worry about vehicle-specific hardware differences. To enable your app for Auto, your app must target Android 5.0 (API level 21) or higher. Your app’s manifest must also declare the car capabilities that it uses, such as audio playback or messaging services.

This lesson describes how to start building apps for Auto, including setting up your development environment and meeting the the minimum requirements to enable an app to communicate with Auto.

Set Up an Auto Project

This section describes how to create a new app or modify an existing app to communicate with Auto.

Prerequisites

Before you begin building apps for Auto, you must:

Declare Auto capabilities

The Auto features that your app can access are controlled by the settings in your app manifest and a separate XML configuration file. Before adding Auto features to your app, you must first define the Auto XML configuration file and add a manifest entry referencing your XML file.

Define the Auto XML configuration file

Specify the car capabilities that your app uses in an XML file that you place in your project’s resources directory ({@code res/xml/}). For example, to extend an audio application for Auto, create a file called {@code automotive_app_desc.xml} and store it under your projects’s {@code res/xml/} folder. The {@code automotive_app_desc.xml} file contains the following metadata:

<automotiveApp>
   <uses name="media" />
</automotiveApp>

The {@code <uses>} element declares the Auto capability your app intends to use. Multiple {@code <uses>} tags can be added if your application uses multiple car capabilities. The {@code name} attribute indicates the specific capability your app uses. The values supported are:

Add a manifest entry

In your app’s manifest ({@code AndroidManifest.xml}), provide a reference to the Auto XML configuration file you created in the previous section. Add a {@code "com.google.android.gms.car.application"} metadata entry under the {@code <application>} element that references your Auto XML configuration file. Omit the {@code .xml} file extension when specifying the configuration filename.

The following code snippet shows how to include this reference in your manifest.

<application>

    ...
    <meta-data android:name="com.google.android.gms.car.application"
     android:resource="@xml/automotive_app_desc"/>

</application>

Add Auto Features to Your Apps

After you have completed the steps described above, you're ready to add Auto features to your apps. See these additional topics to help you build apps for Auto:

Important: Google takes driver distraction very seriously. There are specific design requirements your app must meet to qualify as an Auto app on Google Play. By adhering to these requirements, you can reduce the effort for building and testing your app. For more information, see Auto App Quality.

Run and Test Auto Apps

As you develop, you can run and test your app on your development machine using the Desktop Head Unit (DHU). The DHU replaces the existing simulators and enables your development machine to simulate a vehicle dashboard system running Android Auto.

Installing the DHU

  1. Enable developer mode on your mobile device, as described in Enabling On-device Developer Options.
  2. Compile your app in your development environment and install your app on a physical mobile device running Android 5.0 (API level 21) or higher. To check the version of Android on a Nexus device, go to Settings > About phone (or About tablet) > Android version.
  3. Install the Android Auto app on the mobile device.
  4. Open the SDK Manager and download the DHU package Android Auto Desktop Head Unit emulator from the SDK Tools tab. The DHU installs in the <sdk>/extras/google/auto/ directory.
  5. If you are running the DHU on Linux, you must also install the portaudio, libpng, sdl2, and sdl2_ttf libraries. The procedure to do this varies depending on your Linux distribution. For example, on Debian-derived Linux distributions, you can install the libraries with this command:
    $ sudo apt-get install libsdl2-2.0-0 libsdl2-ttf-2.0-0 libportaudio2 libpng12-0
    

Figure 2. Notification that the head unit server is running.

Figure 1. Context menu with developer options.

Connecting the DHU to your mobile device

Run the DHU by connecting your mobile device to a development machine and setting up a connection to the head unit server over Android Debug Bridge (ADB). Follow these steps to set up tunneling and start the DHU:

  1. On the mobile device, enable Android Auto developer mode by starting the Android Auto companion app, and then tapping the header image 10 times. This step is only required the first time you run the companion app.
  2. If the server is not already running, select Start head unit server from the Android Auto menu.

    On the device, a foreground service appears in the notification area.

  3. Connect the mobile device to the development machine via USB. Your device must be unlocked to launch the DHU.
  4. On the development machine, run the following {@code adb} command to forward socket connections from the development machine's port 5277 to the same port number on the Android device. This configuration allows the DHU to connect to the head unit server running on your phone over a TCP socket.
    $ adb forward tcp:5277 tcp:5277
  5. Start the DHU by running the command desktop-head-unit.exe (on Windows) or ./desktop-head-unit (on Mac or Linux) from the <sdk>/extras/google/auto/ directory.
    $ cd <sdk>/extras/google/auto
    $ ./desktop-head-unit

    By default, the head unit server connects over port 5277. To override the host or port (for example, to forward over SSH), use the --adb flag.

Figure 4. DHU launches on the development machine.

Figure 3. Android Auto launches on the mobile device.

After you set up and start the DHU, you can run DHU commands from the command line to run and test your app from the terminal. You can also run these commands by using keyboard shortcuts. For more information about DHU configuration and commands, see Desktop Head Unit.