Static android shortcuts for multiple flavors?

时光总嘲笑我的痴心妄想 提交于 2020-08-21 02:02:07

问题


Is it possible to define static shortcuts for multiple flavors without duplicating the shortcuts.xml ? I have two flavors:

  • main (package: com.test)
  • free (package: com.test.free)

The shortcuts.xml looks like this:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:enabled="true"
    android:icon="@drawable/ic_shortcut_add_photo"
    android:shortcutId="new_photo"
    android:shortcutLongLabel="@string/new_photo"
    android:shortcutShortLabel="@string/new_photo">

    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.test.MainActivity"
        android:targetPackage="com.test"/>
</shortcut>

The problem is that the package name in the intent can not refer to a string resource and must be hardcoded in the xml.

To also provide the shortcuts for the free flavor i have to copy the shortcuts.xml and change the targetPackage to com.test.free which is a bad solution.


回答1:


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
    android:enabled="true"
    android:icon="@drawable/ic_shortcut_add_photo"
    android:shortcutId="new_photo"
    android:shortcutLongLabel="@string/new_photo"
    android:shortcutShortLabel="@string/new_photo">

    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.test.MainActivity"
        android:targetPackage="${applicationId}"/>
</shortcut>



回答2:


You can have multiple shortcuts.xml with different targetPackage (according to your app ids) in the respective build variants folder. For example :

app/src/debug/res/xml/shortcuts.xml

app/src/staging/res/xml/shortcuts.xml

It works for me.




回答3:


When investigating this, I came across this library, which seems to do the trick:

https://github.com/Zellius/android-shortcut-gradle-plugin

Downside: You cannot have your shortcuts.xml within the res folder of your app, as the plugin takes the file, modifies it to automatically add the targetPackage, and then drops it in during build time, and if you already have one defined, it will cause a duplicate resources error.

Other than that downside, seems to work great!




回答4:


IMPORTANT: This solution only works for android gradle plugin versions prior to 3.0 due to changes in the way resources are processed.

So just hit this problem now because of the .debug suffix on our application id for debug builds. This is our workaround (note this an untested adaption from our codebase):

src/main/res/shortcuts.xml:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:enabled="true"
    android:icon="@drawable/ic_shortcut_add_photo"
    android:shortcutId="new_photo"
    android:shortcutLongLabel="@string/new_photo"
    android:shortcutShortLabel="@string/new_photo">

    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.test.MainActivity"
        android:targetPackage="@string/application_id"/>
</shortcut>

<android module name>/build.gradle:

apply plugin: 'com.android.application'

//region: Fix shortcuts.xml by manually replacing @string/application_id

final String APPLICATION_ID_STRING_RES_KEY    = "application_id"

android.applicationVariants.all { variant ->

  // Add the application id to the strings resources
  // We do this so that in the future if google fixes the 
  // processing of the shortcuts.xml we can leave this
  // and remove the `mergeResources.doLast` block below
  resValue "string", APPLICATION_ID_STRING_RES_KEY, variant.applicationId

  // Manually replace @string/application_id with `variant.applicationId`
  variant.mergeResources.doLast {
    println("variant = ${variant.applicationId}")

    final File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/xml/shortcuts.xml")
    final String content = valuesFile.getText('UTF-8')
    final String updatedContent = content
        .replace("@string/${APPLICATION_ID_STRING_RES_KEY}", variant.applicationId)

    valuesFile.write(updatedContent, 'UTF-8')
  }
}

//endregion  

android {
  ...
}


来源:https://stackoverflow.com/questions/40361296/static-android-shortcuts-for-multiple-flavors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!