重要提示:由于资源处理方式的变化,此解决方案仅适用于 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>
<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 {
...
}