Android 2.3 SDK and Formatted String Resources

The Android 2.3 SDK in Eclipse enforces the optional Argument Index if you use multiple arguments inside a string resource.

Still OK: 

1
"Hello %s."

Flagged as an error: 

1
"Hello $s, %d days have gone by."

Google is taking issue with implicit argument ordering on the grounds that it is not Best Practice to leave it up to the translators to place the argument specifiers if they need to shift around the order due to language grammar. While I feel that taking away our ability to use optional language constructs by making them an error as opposed to a warning is too heavy handed, it is their sandbox and their rules. In the two years I’ve had my apps translated to 14 different languages, I have yet to come across a need to reorder my strings (and even if I did need to, that particular translation would then specify the order). It must be of grave concern for others to have such a forceful step taken to ensure that all our strings comply with such a technique.

If you plan to upgrade to Android 2.3 SDK in Eclipse, you would be wise to ensure all the string resources you currently use comply with this new demand for every format argument used rather than worry if there is multiple on any given line. Search for all your %’s and convert them from implicit indexing to specific by replacing the % with %1$ or %2$, etc. Continuing our example from above:

Single arguments:

1
Hello %1$s.

Multiple arguments:

1
Hello $1$s, %2$d days have gone by.

The good news is that such a change will still work in all Android versions so you don’t have to worry about creating separate resources for earlier versions.
So if you run into the error:

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

you will know precisely how to fix it.

Leave a Reply

Your email address will not be published. Required fields are marked *