swprintf() 不适用于 Android Kitkat 及以下版本?

swprintf() doesn't work on Android Kitkat and below?

大部分时间我在 Android 上使用 UTF-8,但我最近也在测试 wchar_t。 我知道 wchar_t 在早期很糟糕,但我不在乎太旧的设备,所以认为现在使用 wchar_t 会很好。

测试于:

物理设备:Samsung Galaxy Note2 (4.4.2)、Galaxy S7 Edge (7.0)

软件模拟器:HTC One (4.3)、Samsung Galaxy S6 (5.0、6.0)、S8 (7.0)、Google Pixel C (7.1.0)

在我的简单测试中,wcslen()wcscpy()wcscat() 按预期工作, 但是 swprintf()HTC One (4.3) 和 Note2(4.4.2) 上什么也没做。 我无法测试那里的所有设备,但我的结论是 swprintf() 在 Android Kitkat 及以下版本上不起作用。它可能只是 return 什么都不做。

有人可以证实我的理论吗?有什么解决方法吗?

实用不实用,我就是想知道

不要问我为什么,但直到 L Android 只有一个 vswprintf 的存根实现,它只是 returns -1.

https://android.googlesource.com/platform/bionic/+/kitkat-release/libc/bionic/wchar.cpp#74

解决方法是使用 libandroid_support。如果您使用的是 libc++(这是 NDK r16+ 的最佳选择),这是自动的。