Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in Android shortcuts feature xml?

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...

like image 998
ktamas Avatar asked Feb 01 '17 16:02

ktamas


2 Answers

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>
like image 57
Tim Freiheit Avatar answered Nov 20 '22 07:11

Tim Freiheit


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.

like image 1
CommonsWare Avatar answered Nov 20 '22 07:11

CommonsWare