I am playing around with Android's shortcut feature and I have the following xml:
<shortcuts
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="some_id"
android:enabled="true"
android:icon="@drawable/ic_icon"
android:shortcutShortLabel="@string/short_label"
android:shortcutLongLabel="@string/long_label"
tools:targetApi="n_mr1">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="my.package"
android:targetClass="my.package.MainActivity" />
</shortcut>
</shortcuts>
Having it like this, it works without a problem.
Can I use variables in the shortcuts xml file as I can do for example in AndroidManifest.xml
:
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
So I would like to have the same behavior with shortcuts, but somehow it doesn't work:
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="${applicationId}"
android:targetClass="my.package.MainActivity" />
Do you have any idea how to use variables here?
The reason I would like to do this is that we have multiple environments with different package names: my.package.test
, my.package.debug
, my.package.hotfix
, etc...
I created a plugin which make it possible to use manifestPlaceholders in resources and is usable with version 3.0.0 of the android gradle plugin
https://github.com/timfreiheit/ResourcePlaceholdersPlugin
src/main/res/shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut ...>
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.test.MainActivity"
android:targetPackage="${applicationId}"/>
</shortcut>
</shortcuts>
Do you have any idea how to use variables here?
That is not supported at the present time. Manifest placeholders are used in the manifest merger process for manifests. There is no equivalent for resources.
The simplest solutions would revolve around setting up dedicated build types or product flavors. In your case, it would seem like build types are the appropriate model. Having a dedicated shortcuts XML resource per build type would work. In theory, you could have dedicated string resources per build type with the application ID (e.g., app_id
, set up using resValue
in Gradle), then use android:targetPackage="@string/app_id"
in a single shortcuts XML resource. However, I am uncertain if we can use string resources there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With