3

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>
mgcaguioa
  • 1,443
  • 16
  • 19
  • Hi. I'm curious how did you launch app with ```android:name``` attribute in application tag in your manifest? When I created simple project there is no such attribute, and when I tried to add it, Android Studio throws error: "Class was not found...". – Николай Гольцев Aug 03 '20 at 09:02
  • You should add a class that extends Application class. Then set that class on application's `android:name`. Check the documentation: https://developer.android.com/guide/topics/manifest/application-element#nm – mgcaguioa Aug 04 '20 at 06:46

1 Answers1

1

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.

mgcaguioa
  • 1,443
  • 16
  • 19