Porting an Android app to Android TV and Nexus Player

Original author: Xavier Hallade
  • Transfer

As Intel wrote on its blog at the end of last year, Google released a new Nexus Player based on the Intel Atom processor. In this article you will find tips on how to adapt your Android application for this device in particular and Android TV in general.

If you want your application to be accessible for Android TV devices and seen as their loader, you must first provide an activity that will handle intent action.Main for the LEANBACK_LAUNCHER category , secondly add TV-specific resources and, thirdly, indicate that your application does not require a touch display. As you can see, everything is very simple. Now let's see how it looks in practice.

Like the classic Android bootloader, the Android TV bootloader will look for the activity of your application, which you set in android.intent.action.MAIN intent . But in the case of the Android TV bootloader, the intent category will not be android.intent.category.LAUNCHER , but android.intent.category.LEANBACK_LAUNCHER .

Here's how you should declare the Main Activity of your Android TV app:

AndroidManifest.xml:

            
                
                
            

Comment. Leanback Launcher has two categories of applications: Apps and Games. If you want to appear in the list of games, you need to set the isGame property to true:

AndroidManifest.xml:

  ...
  android:isGame="true"
  ...>

At the top of the specific intent filter, also located in this activity, there is an android: banner property that defines the banner that will be displayed in the launcher. The banner is a drawing of 320x180 pixels in size, which should be placed in the drawable-xhdpi resources folder. The figure should include the name of your application (localized if necessary) and not have an alpha channel:



All this will allow the application to be visible and run in Android TV launcher.

However, in order for the application to be compatible with Android TV, you also need to provide and declare support for Android devices without a touch screen. The first step is to implement navigation support using the cross-button (D-pad). The standard Android interface elements themselves support it, but in practice some configuration is required.
Specify the element that the original focus will be directed to in your view using requestFocus () in Java or in XML markup:

my_activity.xml:

      

The android: focusable property must be true if you want the focus request to work when the view is enlarged.

Sometimes you need to customize the navigation inside the view. You can choose how the focus will move from one element to another using the nextFocus property :

my_activity.xml:

Once D-pad navigation works in your application, you can declare it in AndroidManifest.xml. Just announce that we don’t need sensory capabilities:


Fulfilling the above requirements is enough for your application to install and run on Android TV. Now let's talk a little about the convenience of its use.

The design of the application should not contain menu bars and toolbars (even if the menu bar works with the D-pad, this is definitely a bad idea in terms of usability). Two skins you can use are Theme.Leanback from the Leanback and android support library : Theme.NoTitleBar . These themes do not include borders around the edges of the screen, so they should be added additionally. Notice that fields are already included in the Leanback elements.


The Leanback Support Library is available starting with API Level 17 and contains ready-to-use elements and themes for applications. If you plan to support earlier versions of the API, you can store all links to Leanback themes and elements in the TV-specific part of the application and then use the gradle manifest merge function to avoid the minSdkVersion upgrade:

AndroidManifest.xml:

   ...

See an example of using the Leanback support library. I also suggest reading a good introductory article by Sebastiano Gottardo (musixMatch).

Android TV games can be greatly improved by adding multiplayer mode and gamepad support. In the manifest, you can indicate that the game supports a gamepad:


Set this option as required only if the gamepad is required for your application; remember that it is not included in the package of devices like Nexus Player. Additional Android devices can be used as controllers, but at the moment they can only act as a D-pad.

You can learn a variety of game controllers at this training .

The block of recommendations takes up half the space in the launcher. To appear there, send a notification with the established category “recommendation”, as described in the official training .
Using the functional filtering system in the Play Store, you can restrict the distribution of your application to Android TV compatible devices only by setting the desired value android.software.leanback:

AndroidManifest.xml:

If required, your application will be available only to Android TV compatible devices.
You can distribute your application as a separate item in the Play Store, there is also the option to download it as a TV version of an existing application using the multiple APK function in the Play Store. In the second case, the TV version should have a different versionCode than the classic application.

Nexus Player is a fully 64-bit platform and the core of the system, of course, is also 64-bit, but the user space is still x86 32-bit. On the device, you can safely run compiled for ARM code, but it is better to use x86 binaries. You can read about compatibility aspects for x86 devices in my previous article .

A summary of the programming principles for Android TV can be found on the website of Android developers .
If android: screenOrientation = ”portrait” is specified in any activity of your application , the property android.hardware.screen.portrait will be forced to set to true . You need to force it to override false so that your application is accessible on Android TV devices:


The same is necessary for:
  • android.hardware.location.android.permission.ACCESS_FINE_LOCATION
  • android.hardware.camera.autofocus and android.hardware.camera , implicit android.permission.CAMERA
  • android.hardware.microphone implicated by android.permission.RECORD_AUDIO
  • android.hardware.telephony implicated by various telephone permissions

When your application is ready and the ARC is uploaded to the Play Store developer page, you need to add a screenshot and a banner:



And check the box to request distribution of the application on Android TV:



It will be checked before it appears in the Play Store, available for Android TV.

Also popular now: