有没有可能在不复制shortcuts.xml的情况下为多种风格定义静态快捷方式?我有两种口味:
免费
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,这是一个糟糕的解决方案。
发布于 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>
发布于 2018-07-25 20:16:29
在各自的build variants文件夹中,您可以有多个具有不同targetPackage
的shortcuts.xml
(根据您的应用程序in )。例如:
app/src/debug/res/xml/shortcuts.xml
app/src/staging/res/xml/shortcuts.xml
这对我很管用。
发布于 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 {
...
}
https://stackoverflow.com/questions/40361296
复制相似问题