About Share Targets and Magic Names

If you are trying to use androidx.sharetarget:1.0.0-alpha01 and are wondering why it’s not working correctly, you may not have used the right magic name.

androidx.sharetarget is a new AndroidX library that offers support for Android Q-style share targets on older devices. It supplies its own ChooserTargetService implementation (androidx.sharetarget.ChooserTargetServiceCompat) that you point to, and that in turn implements the Android 6.0-9.0 Direct Share API. It builds the ChooserTarget objects based on your Android Q share targets.

Android Q’s share targets are based on a combination of dynamic shortcuts using ShortcutManager and a new <share-target> element in a static shortcuts XML resource. Static shortcuts are identified in the manifest by adding a android.app.shortcuts <meta-data> element to your launcher activity, pointing to an XML resource.

Normally, you can name that XML resource whatever you want, so long as it is a valid resource name.

For androidx.sharetarget, at least for 1.0.0-alpha01, you must name that resource shortcuts.xml.

That is because 1.0.0-alpha01 hard-codes that name, with a TODO comment to fix it:

    private static XmlResourceParser getXmlResourceParser(Context context) {
        // TODO: Parse the main manifest to find the right Xml resource for share targets
        Resources res = context.getResources();
        return res.getXml(res.getIdentifier("shortcuts", "xml", context.getPackageName()));
    }

So, with luck, this restriction will be lifted in a future update to the androidx.sharetarget library.

In general, try to avoid magic names (such as shortcuts.xml). And where you need a magic name, make sure you document it.