1page.title=Getting Started with Auto
2page.tags="auto", "car", "automotive"
3page.article=true
4page.image=auto/images/assets/icons/auto_app_in_simulator.png
5
6@jd:body
7
8<div id="tb-wrapper">
9<div id="tb">
10  <h2>Dependencies and Prerequisites</h2>
11  <ul>
12    <li>Android 5.0 (API level 21) or higher</li>
13  </ul>
14
15  <h2>This class teaches you how to</h2>
16  <ol>
17    <li><a href="#dev-project">Set Up an Auto Project</a></li>
18    <li><a href="#build-it">Build Auto Apps</a></li>
19    <li><a href="#test-it-dhu">Run and Test Auto Apps </a></li>
20  </ol>
21
22 <h2>You should also read</h2>
23 <ul>
24   <li><a href="{@docRoot}design/auto/index.html">Designing for Auto</a></li>
25   <li><a href="{@docRoot}training/auto/audio/index.html">Providing Audio Playback with Auto</a></li>
26   <li><a href="{@docRoot}training/auto/messaging/index.html">Providing Messaging for Auto</a></li>
27 </ul>
28</div>
29</div>
30
31<p>Android Auto extends the Android platform into the car. When users connect
32their handheld devices running Android 5.0 or higher to a compatible vehicle,
33the Auto user interface provides a car-optimized Android experience on the
34vehicle's screen. Users interact with compatible apps and services through
35voice actions and the vehicle's input controls (like a touchscreen or dashboard
36buttons).</p>
37
38<p>Auto currently supports two types of apps:</p>
39
40<ul>
41<li><em>Audio apps</em> that allow users to browse and play music and spoken
42audio content in the car.</li>
43<li><em>Messaging apps</em> that receive incoming notifications, read messages
44  aloud via text-to-speech, and send replies via voice input in the car.</li>
45</ul>
46
47<p>You can enable your existing audio and messaging apps developed for
48phones and tablets to work in the car, without having to worry about
49vehicle-specific hardware differences. To enable your app for Auto, your
50app must target Android 5.0 (API level 21) or higher. Your app’s manifest must
51also declare the car capabilities that it uses, such as audio playback or
52messaging services. </p>
53
54<p>This lesson describes how to start building apps for Auto, including
55setting up your development environment and meeting the the minimum requirements
56to enable an app to communicate with Auto.</p>
57
58<h2 id="dev-project">Set Up an Auto Project</h2>
59<p>This section describes how to create a new app or modify an existing app to
60communicate with Auto.</p>
61
62<h3 id="prerequisites">Prerequisites</h3>
63<p>Before you begin building apps for Auto, you must:</p>
64
65<ul>
66<li><strong><a href="{@docRoot}sdk/installing/create-project.html">Create or
67update your app project</a></strong> - Android 5.0 (API level 21) provides new
68APIs for implementing audio playback and messaging that is compatible with Auto.
69To access the new APIs, create a project or modify an existing project to target
70Android 5.0 (API level 21) or higher. This means you must set the manifest
71<a href="{@docRoot}topics/manifest/uses-sdk-element.html">{@code targetSdkVersion}</a>
72to 21 or higher.
73</li>
74<li><strong><a href="{@docRoot}tools/support-library/setup.html">Install the
75support library</a></strong> - If you are building messaging apps for Auto, you
76need the {@link android.support.v4.app.NotificationCompat.CarExtender} class
77contained in the
78<a href="{@docRoot}tools/support-library/features.html#v4">v4 support library</a>.
79This class allows you to create notifications that are compatible with Auto
80devices.</li>
81</ul>
82
83<h3 id="auto-metadata">Declare Auto capabilities</h3>
84<p>The Auto features that your app can access are controlled
85by the settings in your app manifest and a separate XML configuration file.
86Before adding Auto features to your app, you must first define the Auto
87XML configuration file and add a manifest entry referencing your XML file.</p>
88
89<h4 id="auto_xml">Define the Auto XML configuration file</h4>
90<p>Specify the car capabilities that your app uses in an XML file that you
91place in your project’s resources directory ({@code res/xml/}). For example, to
92extend an audio application for Auto, create a file called
93{@code automotive_app_desc.xml} and store it under your projects’s
94{@code res/xml/} folder. The {@code automotive_app_desc.xml} file contains the
95following metadata:</p>
96<pre>
97&lt;automotiveApp&gt;
98   &lt;uses name="media" /&gt;
99&lt;/automotiveApp&gt;
100</pre>
101<p>The {@code &lt;uses&gt;} element declares the Auto capability your app
102intends to use. Multiple {@code &lt;uses&gt;} tags can be added if your
103application uses multiple car capabilities. The {@code name} attribute indicates
104the specific capability your app uses. The values supported are:</p>
105<ul>
106<li>{@code media} - The app uses the Android framework APIs to play music in
107a vehicle. Set this value if you are enabling an audio app for Auto.</li>
108<li>{@code notification} - The app displays message notifications in the car’s
109Overview screen, allows users select a message to be read aloud, and lets them
110respond through voice input. Set this value if you are enabling a messaging
111app for Auto.
112</ul>
113
114<h4 id="auto_xml">Add a manifest entry</h4>
115<p>In your app’s manifest ({@code AndroidManifest.xml}), provide a reference to
116the Auto XML configuration file you created in the previous section. Add a
117{@code "com.google.android.gms.car.application"} metadata entry under the
118<a href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application&gt;}</a>
119element that references your Auto XML configuration file. Omit the {@code .xml}
120file extension when specifying the configuration filename.</p>
121<p>The following code snippet shows how to include this reference in your
122manifest.</p>
123<pre>
124&lt;application&gt;
125
126    ...
127    &lt;meta-data android:name="com.google.android.gms.car.application"
128     android:resource="@xml/automotive_app_desc"/&gt;
129
130&lt;/application&gt;
131</pre>
132
133<h2 id="build-it">Add Auto Features to Your Apps</h2>
134<p>After you have completed the steps described above, you're ready to add Auto
135features to your apps. See these additional topics to help you build apps for
136Auto:</p>
137
138<ul>
139<li><a href="{@docRoot}training/auto/audio/index.html">Providing Audio Playback for Auto</a>
140- Create apps that let users browse and play music in the car.</li>
141<li><a href="{@docRoot}training/auto/messaging/index.html">Providing Messaging for Auto</a>
142- Enable users to receive and reply to messages in the car.</li>
143</ul>
144
145<p class="caution"><strong>Important:</strong> Google takes driver distraction
146very seriously. There are specific design requirements your app must meet to
147qualify as an Auto app on Google Play. By adhering to these
148requirements, you can reduce the effort for building and testing your app. For
149more information, see
150<a href="{@docRoot}distribute/essentials/quality/auto.html">Auto App Quality</a>.</p>
151
152
153
154<h2 id="test-it-dhu">Run and Test Auto Apps </h2>
155
156<p>
157  As you develop, you can run and test your app on your development machine
158  using the <em>Desktop Head Unit</em> (DHU). The DHU replaces the existing
159  simulators and enables your development machine to simulate a vehicle
160  dashboard system running Android Auto.
161</p>
162
163<h3 id="installing-dhu">Installing the DHU</h3>
164
165<ol>
166  <li>Enable developer mode on your mobile device, as described in
167    <a href="{@docRoot}tools/device.html#developer-device-options">Enabling On-device
168    Developer Options</a>. </li>
169  <li>Compile your app in your development environment and install your app on
170    a physical mobile device running Android 5.0 (API level 21) or higher. To check the
171    version of Android on a Nexus device, go to
172    <strong>Settings &gt; About phone</strong> (or <strong>About tablet</strong>) <strong>&gt;
173    Android version</strong>.</li>
174
175  <li>Install the
176     <a class="external-link"
177     href="https://play.google.com/store/apps/details?id=com.google.android.projection.gearhead&hl=en"
178     >Android Auto app</a> on the mobile device.</li>
179  <li>Open the <a href="{@docRoot}tools/help/sdk-manager.html">SDK Manager</a> and
180    download the DHU package <strong>Android Auto Desktop Head Unit</strong> from the
181    <em>SDK Tools</em> tab. The DHU installs in the <code>&lt;sdk&gt;/extras/google/auto/</code>
182    directory.</li>
183  <li>If you are running the DHU on Linux, you must also install
184    the portaudio, libpng, sdl2, and sdl2_ttf libraries.
185    The procedure to do this varies depending on your Linux distribution. For example, on
186    Debian-derived Linux distributions, you can install the libraries with this command:
187
188<pre class="no-pretty-print">
189$ sudo apt-get install libsdl2-2.0-0 libsdl2-ttf-2.0-0 libportaudio2 libpng12-0
190</pre>
191
192  </li>
193</ol>
194
195<div class="figure" style="width:330px">
196  <img src="{@docRoot}images/training/auto-desktop-head-unit-server-running.png"
197      alt="" >
198  <p class="img-caption">
199    <strong>Figure 2.</strong> Notification that the head unit server is running.
200  </p>
201</div>
202<img src="{@docRoot}images/training/auto-desktop-head-unit-context-menu-enabled.png"
203    alt="" >
204<p class="img-caption">
205  <strong>Figure 1.</strong> Context menu with developer options.
206</p>
207
208<h3 id="connecting-dhu">Connecting the DHU to your mobile device</h3>
209
210<p>Run the DHU by connecting your mobile device to a development machine and setting up a connection to
211  the head unit server over <a href="{@docRoot}tools/help/adb.html">Android Debug Bridge
212  (ADB)</a>. Follow these steps to set up tunneling and start the DHU:</p>
213
214<ol>
215  <li>On the mobile device, enable Android Auto developer mode by starting the Android Auto
216    companion app, and then tapping the header image 10 times.
217    This step is only required the first time you run the companion app.
218  </li>
219  <li>If the server is not already running, select <strong>Start head unit server</strong>
220     from the Android Auto menu.
221    <p>On the device, a foreground service appears in the notification area. </p>
222  </li>
223
224  <li>Connect the mobile device to the development machine via USB. Your device must be unlocked to
225    launch the DHU.
226  </li>
227  <li>On the development machine, run the following {@code adb} command to
228    forward socket connections from the
229    development machine's port 5277 to the same port number on the Android device.
230    This configuration allows the DHU to connect to the head unit server running on your phone over
231    a TCP socket.
232    <pre class="no-pretty-print">$ adb forward tcp:5277 tcp:5277</pre>
233  </li>
234
235  <li>Start the DHU by running the command <code>desktop-head-unit.exe</code> (on Windows)
236    or <code>./desktop-head-unit</code> (on Mac or Linux) from the
237    <code>&lt;sdk&gt;/extras/google/auto/</code> directory.
238
239<pre class="no-pretty-print">$ cd &lt;sdk&gt;/extras/google/auto
240$ ./desktop-head-unit</pre>
241
242    <p>
243      By default, the head unit server connects over port 5277. To override the host or port
244      (for example, to forward over SSH), use the <code>--adb</code> flag.
245    </p>
246
247  </li>
248</ol>
249
250<div class="figure" style="width:432px">
251
252    <img src="{@docRoot}images/training/auto-desktop-head-unit-wkst-launch.png"
253        alt="" >
254    <p class="img-caption">
255      <strong>Figure 4.</strong> DHU launches on the development machine.
256    </p>
257</div>
258
259    <img src="{@docRoot}images/training/auto-desktop-head-unit-launch.png"
260        alt="" >
261    <p class="img-caption">
262      <strong>Figure 3.</strong> Android Auto launches on the mobile device.
263    </p>
264
265<p>
266  After you set up and start the DHU, you can run DHU commands from the command line to run and
267  test your app from the terminal. You can also run these commands by using keyboard shortcuts. For
268  more information about DHU configuration and commands, see <a href=
269  "{@docRoot}tools/help/desktop-head-unit.html">Desktop Head Unit</a>.
270</p>
271
272
273