1

I'm trying to add a custom action to the media session's playback state, so it can show an addition button on Android Auto. The code is indeed simple, the button is added and I can receive its callback, but the problem is the button doesn't have an icon:

The code:

val builder = PlaybackStateCompat.Builder()
builder.addCustomAction(
    "ACTION_TEST",
    "TEST LABEL",
    R.drawable.ic_test // I tried vector drawable, png,... nothing works
)
// ...
mediaSession.setPlaybackState(builder.build())

The result: the added button doesn't have an icon, only shows a ripple effect (the circle in the screenshot) when I click it, otherwise it's invisible. enter image description here

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Khang .NT
  • 1,504
  • 6
  • 24
  • 46

2 Answers2

0

I found out the root cause. It's because Android Studio auto-optimizes the build process to produce minimal APK that can run on current ADB-connected devices.

In my case, AS included only xhdpi resources in the debug APK, therefore Android Audio HeadUnit couldn't load the drawable. Basically, AS silently changed the Gradle config to somewhat like this:

android {
    ...
    productFlavors {
        dev {
            resConfigs "xhdpi" // <- root cause
        }
        ...
    }
}

I can simply run my app from the terminal to get rid of this problem.
If you have the same problem, just ensure the drawable is available for all screen densities (hdpi, xhdpi,...), and check your APK whether it's packaged with resources in all screen densities.

Khang .NT
  • 1,504
  • 6
  • 24
  • 46
0

For me, the problem was resolved when I cleared the data of Android App application on my phone.

Was using it to run DHU emulator on desktop. Somehow its caching went awry.

Starwave
  • 2,352
  • 2
  • 23
  • 30