And*_*Roß 20 xml android android-shortcut
是否可以为多种口味定义静态快捷方式而无需复制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>
Run Code Online (Sandbox Code Playgroud)
问题是intent中的包名称不能引用字符串资源,必须在xml中进行硬编码.
为了还为自由的味道,我必须提供快捷方式复制的shortcuts.xml并更改targetPackage到com.test.free这是一个坏的解决方案.
您可以在相应的构建变体文件夹中拥有多个shortcuts.xml不同的targetPackage(根据您的应用程序 ID)。例如 :
app/src/debug/res/xml/shortcuts.xml
app/src/staging/res/xml/shortcuts.xml
Run Code Online (Sandbox Code Playgroud)
这个对我有用。
小智 5
我创建了一个插件,该插件可以在资源中使用manifestPlaceholders,并且可与android gradle插件的3.0.0版本一起使用
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>
Run Code Online (Sandbox Code Playgroud)
重要提示:由于资源处理方式的变化,此解决方案仅适用于 3.0 之前的 android gradle 插件版本。
现在就解决这个问题,因为.debug我们的应用程序 ID 上有用于调试版本的后缀。这是我们的解决方法(请注意,这是我们代码库中未经测试的改编):
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>
Run Code Online (Sandbox Code Playgroud)
<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 {
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1679 次 |
| 最近记录: |