'Get application Id in XML dynamically

I'm using shortcuts in my application and I have multiple product flavors. I want to get the application Id dynamically for target package.

I've given ${applicationId}, but it's not working. I tried with ${packageName} didn't work. any other alternative?

<shortcut
    android:enabled="true"
    android:icon="@drawable/ic_barcode_icon"
    android:shortcutId="bar_code"
    android:shortcutShortLabel="@string/shortcuts_my_bar_code">
    <intent
        android:action="android.intent.action.VIEW.myBarCode"
        android:targetClass="com.appemirates.clubapparel.home.HomeActivity"
        android:targetPackage="${applicationId}" />
</shortcut>



Solution 1:[1]

Manifest placeholders like ${applicationId} are only supported for the manifest, not arbitrary other XML files, such as shortcut metadata resources.

You could try:

  • Have a copy of your shortcut XML in each product flavor's source set, where that copy has the hardcoded application ID

  • Use resValue in Gradle to set up a string resource with the application ID (per flavor), and use @string/... notation to refer to it in the shortcut XML

The first one definitely works but is tedious due to the code duplication. I have not tried the second one.

Otherwise, you would be looking at building your own Gradle plugin or similar processor that could dynamically generate these resources from a template.

Solution 2:[2]

you can create BuildConfig parameter through gradle. You need to add below in specific build flavor

resValue "string", "APP_ID", "com.example.myapp.india"

that will generate string resource as per your build flavor.And you can directly use it as below in your code.

<shortcut
    android:enabled="true"
    android:icon="@drawable/ic_barcode_icon"
    android:shortcutId="bar_code"
    android:shortcutShortLabel="@string/shortcuts_my_bar_code">
    <intent
        android:action="android.intent.action.VIEW.myBarCode"
        android:targetClass="com.appemirates.clubapparel.home.HomeActivity"
        android:targetPackage="@string/APP_ID" />
</shortcut>

Let me know for more help :)

Solution 3:[3]

In your app's build.gradle.kts (Gradle Kotlin DSL) you could add:

android {
  // your setup...
  
  androidComponents {
    onVariants { variant ->
      variant.resValues.put(
        variant.makeResValueKey("string", "app_id"),
        ResValue(variant.applicationId.get())
    }
  }
}

Which will write a string into the resources with the name app_id and use the application ID of the variant. You will then be able to reference the string resource using @string/app_id inside your XML resource files. ${applicationId} unfortunately only works in Manifest files, as has been mentioned in the other answers.

This solution should also work with flavours and build types which augment the application ID by using applicationIdSuffix, and thus answers the comment from Get application Id in XML dynamically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CommonsWare
Solution 2 Hardik Bambhania
Solution 3 Martin