How one can Construct Your First App for Android Automotive OS – Grape Up

Android Automotive OS is getting extra recognition as automotive firms wish to present their prospects with a extra tailor-made expertise. Right here we share our information to constructing the primary app for AAOS.

Earlier than you begin, learn our first article about AAOS and get to know our overview to pay attention to what to anticipate. Let’s strive making a easy Hi there World app for android automotive. To get an IDE, go to Android Studio Preview | Android Developers and get a canary construct:

AAOS Hello World: How to Build Your First App for Android Automotive OS

Within the subsequent step, put together SDK, examine and obtain the Automotive system picture in SDK supervisor. You will get any from api32, Android 9, or Android 10, however I don’t advocate the latest one as it is vitally laggy and crashes quite a bit proper now. There are additionally Volvo and Polestar photographs.

For these you must add hyperlinks to SDK Replace Websites:

https://developer.volvocars.com/sdk/volvo-sys-img.xml

https://developer.polestar.com/sdk/polestar2-sys-img.xml

Begin a brand new mission, go to File> New Mission and select automotive with no exercise

Android Automotive OS

A pleasant and clear mission ought to be created, with none lessons: Go to construct.gradle and add the automobile app library into dependencies, refresh the mission to make it get

AAOS Hello World

our new dependency:

implementation "androidx.automobile.app:app-automotive:1.2.0-rc01"

Let’s write some code, first our display screen class. Title it as you need and make it lengthen Display screen class from android.automobile.app package deal and make it implement required strategies:

public class GrapeAppScreen extends Display screen 

   public GrapeAppScreen(@NonNull CarContext carContext) 
       tremendous(carContext);
   

   @NonNull
   @Override
   public Template onGetTemplate() 
       Row row = new Row.Builder()
.setTitle("Thats our Grape App!").construct();

       return new PaneTemplate.Builder(
               new Pane.Builder()
                       .addRow(row)
                       .construct()
       ).setHeaderAction(Motion.APP_ICON).construct();
   

That ought to create a easy display screen with our icon and title, now create one other class extending CarAppService from the identical package deal and as nicely make it implement the required strategies. From createHostValidator() technique return a static one that permits all hostnames for the aim of this tutorial and return model new session with our display screen in onCreateSession(), go CarContext utilizing Session class getCarContext() technique:

public class GrapeAppService extends CarAppService 

   public GrapeAppService() 

   @NonNull
   @Override
   public HostValidator createHostValidator() 
       return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR;
   

   @NonNull
@Override
public Session onCreateSession() 
   return new Session() 
       @Override
       @NonNull
       public Display screen onCreateScreen(@Nullable Intent intent) 
           return new GrapeAppScreen(getCarContext());
       
   ;



Subsequent, transfer to AndroidManifest and add varied options inside the primary manifest tag:

<uses-feature
   android:title="android.{hardware}.sort.automotive"
   android:required="true" />
<uses-feature
   android:title="android.software program.automobile.templates_host"
   android:required="true" />
<uses-feature
   android:title="android.{hardware}.wifi"
   android:required="false" />
<uses-feature
   android:title="android.{hardware}.display screen.portrait"
   android:required="false" />
<uses-feature
   android:title="android.{hardware}.display screen.panorama"
   android:required="false" />

Contained in the Software tag add our service and exercise, don’t neglect minCarApiLevel as lack of this may throw an exception on app begin:

<utility
   android:allowBackup="true"
   android:appCategory="audio"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@type/Theme.GrapeApplication">

<meta-data android:title="androidx.automobile.app.minCarApiLevel"
   android:worth="1"
/>

   <service
       android:title="com.grapeup.grapeapplication.GrapeAppService"
       android:exported="true">
       <intent-filter>
           <motion android:title="androidx.automobile.app.CarAppService" />
       </intent-filter>
   </service>

   <exercise
       android:title="androidx.automobile.app.exercise.CarAppActivity"
       android:exported="true"
       android:label="GrapeApp Starter"
       android:launchMode="singleTask"
       android:theme="@android:type/Theme.DeviceDefault.NoActionBar">

       <intent-filter>
           <motion android:title="android.intent.motion.MAIN" />
           <class android:title="android.intent.class.LAUNCHER" />
       </intent-filter>
       <meta-data
           android:title="distractionOptimized"
           android:worth="true" />
   </exercise>
</utility>

Now we are able to add our utility to the machine, confirm that you’ve an automotive emulator created, use automotive configuration, and hit run. The app is run in Google Automotive App Host, so if it’s your first utility on this machine, it might require you to get to the play retailer and get it.

That’s the way it seems:

Build Your First App for Android Automotive OS

The very last thing, we’ll add a navigation button that may pop a Toast. Modify onGetTemplate() in Display screen class, add Motion and ActionStrip:

Motion motion = new Motion.Builder()
       .setOnClickListener(
               () -> CarToast.makeText(getCarContext(), "Hi there!", CarToast.LENGTH_SHORT).present())
       .setTitle("Say hello!")
       .construct();

ActionStrip actionStrip = new 

Add it to PaneTemplate:

return new PaneTemplate.Builder(
       new Pane.Builder()
               .addRow(row)
               .construct()
)       .setActionStrip(actionStrip)
       .setHeaderAction(Motion.APP_ICON)
       .construct();

That’s our HelloWorld app:

Now you could have the HelloWorld instance app up and working utilizing Automobile App Library. It takes care of displaying and arranging every little thing on the display screen for us. The one accountability is so as to add screens and actions we want to have(and a little bit of configuration). Test the Automobile app library to discover extra of what might be accomplished with it, mess around with creating your app, and positively examine our weblog quickly for extra AAOS app creation content material.

Android Automotive OS