Multiple substitutions specified in non-positional format

as3.0以上在gradle.properties使用android.enableAapt2=true,as就会提示将要过期了,请设置成
android.enableAapt2=false。当我设置成false啦,这时候编译出错误。

Multiple substitutions specified in non-positional format; did you mean to add the formatted="false"

就是当你的string.xml上含有要格式的字符串比如

<string name="time_picker">%s时</string>

这时候会提示上述错误,因为sdk采用了更加严格的aapt编译,正确的用法如下

If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource:


In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application like this:


 <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

解决方法:

之前我们用的%s 改为%1$s,如果在一个字符串有两个%s,%s。现在要改为%1$s,%2$s,重新编译没ok了。

拓展

如果字符串含有%,而不需要格式化可以有三种方法解决

  1. 加上formatted="false"
<string name="time_picker" formatted="false">%s时</string>
  1. 用两个%%
<string name="time_picker">%%s时</string>
  1. 用转义字符\
<string name="time_picker" >\%s时</string>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容