Android app icon shorcut not launching activity

370 Views Asked by At

I'm trying to implement app icon shortcut on Android. I followed the documentation but I'm having problem launching the app from the shortcut. Every time I click the app icon shortcut, nothing happens. Here's the code on my AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.myapp">

<application
    android:name=".MyApp"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar"
    android:usesCleartextTraffic="${usesCleartextTraffic}"
    tools:ignore="ExportedService,GoogleAppIndexingWarning,UnusedAttribute">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
    </activity>

    ...

</application>
</manifest>

Here's the shortcuts.xml file:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedAttribute">

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_send"
        android:shortcutId="send_funds"
        android:shortcutLongLabel="@string/LBL_SEND_FUNDS"
        android:shortcutShortLabel="@string/BTN_SEND">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.package.myapp.MainActivity"
            android:targetPackage="com.package.myapp" />
    </shortcut>

</shortcuts>
1

There are 1 best solutions below

0
mgcaguioa On

So the issue was on the buildType. When running on buildTypes other than the release build, I'm having the issue because the package name is different from what was indicated on the android:targetPackage of shortcut's intent. So what I did was, I created multiple shortcuts.xml with different targetPackage in the respective build folder:

app/src/debug/res/xml/shortcuts.xml
app/src/dev/res/xml/shortcuts.xml
app/src/staging/res/xml/shortcuts.xml

And set the android:targetPackage respectively:

android:targetPackage="com.package.myapp.debug"
android:targetPackage="com.package.myapp.dev"
android:targetPackage="com.package.myapp.staging"

Thanks to this SO question which is almost similar to my issue. And to Rakesh's answer.