在 Android 本机库中尝试 Catch 失败

Try Catch in Android native library fails

当我在本机库中执行 throw 命令时,我的 Android 应用程序崩溃了。 Android 不支持这些调用吗?它永远不会到达陷阱。

try
{
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "throw");
    throw;
}
catch (...) {
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "catch");
}

我最近从 gnustl_shared 切换到 c++_shared,我不确定这是否与我的问题有关。

开发人员指南说 gnustl_shared 默认启用异常,但 c++_shared 没有。我已经包含了指南中描述的 -fexceptions 标志。 https://developer.android.com/ndk/guides/cpp-support.html

这是预期的行为。来自 C++ 标准:

If no exception is presently being handled, executing a throw-expression with no operand calls terminate()(15.5.1).

如果你想让它抓住你需要扔东西:

try
{
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "throw");
    throw new std::exception();
}
catch (...) {
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "catch");
    env->ExceptionCheck();
}

这似乎是一个已知问题:

https://developer.android.com/ndk/guides/cpp-support.html

兼容性

NDK 的 libc++ 不稳定。并非所有测试都通过,测试套件也不全面。一些已知问题是:

•在 ARM 上使用 c++_shared 可能会在抛出异常时崩溃。

•对 wchar_t 的支持和区域设置 API 是有限的。

此严重错误是否已在 Android 开发人员社区中确定为官方错误?如果没有,它应该。这确实是设计中的一个重大缺陷。灾难性的。