Search code examples
androidkotlinandroid-instant-appsandroid-shortcut

ShortcutManager crash in instant apps


When I run my instant app I'm experiencing a crash due to the shortcut feature, please see the image with the stack trace, this is the only thing I can provide as I'm not able to attach the debugger, another problem :(

stacktrace

**GroupApplication.class**
override fun onCreate() {
    ...
    ShortcutsHelper.init(this)
    ShortcutsHelper.addSearchShortcut()
}
object ShortcutsHelper {
    lateinit var appContext: Context
    lateinit var shortcutManager: ShortcutManager

    fun init(context: Context) {
        this.appContext = context.applicationContext
        this.shortcutManager = context.getSystemService(ShortcutManager::class.java)
    }

    fun addSearchShortcut() {
        val shortcut = ShortcutInfo.Builder(appContext, SEARCH_SHORTCUT_ID)
                .setShortLabel(appContext.getString(R.string.search_shortcut_short_label))
                .setLongLabel(appContext.getString(R.string.search_shortcut_long_label))
                .setIcon(Icon.createWithResource(appContext, R.drawable.ic_search_black))
                .setIntent(GroupHomeActivity.getStartIntent(appContext, NavigationScreen.SEARCH))
                .build()

        shortcutManager.addDynamicShortcuts(listOf(shortcut))
    }

I understand that a shortcut does not make any sense in an Instant App, is there a better solution than having all the code related to shortcuts commented when creating an IA?


Solution

  • I understand that a shortcut does not make any sense in an Instant App, is there a better solution than having all the code related to shortcuts commented when creating an IA?

    So yes, Instant Apps is restricted from running some APIs for security purposes. There used to be a FAQ page for it that indicated this, but I can't find it anymore.

    But anyways, you don't have to comment it out. You can use:

    /instantapps/PackageManagerCompat.html#isInstantApp()