首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >多种风格的静态android快捷键?

多种风格的静态android快捷键?
EN

Stack Overflow用户
提问于 2016-11-01 21:43:07
回答 4查看 2.5K关注 0票数 25

有没有可能在不复制shortcuts.xml的情况下为多种风格定义静态快捷方式?我有两种口味:

免费

  • main (软件包:com.test)
  • (软件包: com.test.free)

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="com.test"/>
</shortcut>

问题是意图中的包名不能引用字符串资源,并且必须是xml中的硬编码的

为了提供免费版本的快捷方式,我必须复制 shortcuts.xml,并将targetPackage更改为com.test.free,这是一个糟糕的解决方案。

EN

回答 4

Stack Overflow用户

发布于 2017-10-29 20:25:29

我创建了一个插件,它使得在资源中使用manifestPlaceholders成为可能,并且可以与版本3.0.0的android gradle插件一起使用

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>
票数 6
EN

Stack Overflow用户

发布于 2018-07-25 20:16:29

在各自的build variants文件夹中,您可以有多个具有不同targetPackageshortcuts.xml (根据您的应用程序in )。例如:

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

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

这对我很管用。

票数 6
EN

Stack Overflow用户

发布于 2017-06-30 14:57:26

重要提示:由于资源处理方式的改变,此解决方案仅适用于3.0之前的android gradle插件版本。

所以现在就来解决这个问题,因为我们的应用程序id上有.debug后缀,用于调试构建。这是我们的变通方法(请注意,这是我们代码库中未经测试的改编):

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 {
  ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40361296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档