Android NDK R5 and C ++ exception support

I am trying to use NDK 5 full C ++ gnustl:

In CPLUSPLUS-SUPPORT.html indicated:

The NDK toolchain supports C ++ exceptions, since NDK r5, however, all C ++ sources are compiled with -fno-exceptions support by default, for compatibility with previous versions.

To enable it, use the C ++ compiler flag '-fexceptions'. This can be done by adding the following to each module definition in your Android.mk:

 LOCAL_CPPFLAGS += -fexceptions 

Simply put, add one line to your Application.mk, the configuration will be automatically applied to all your NDK project modules:

 APP_CPPFLAGS += -fexceptions 

sources/cxx-stl/gnu-libstdc++/README :

This directory contains headers and ready-made binaries for GNU libstdC ++ - v3 Standard implementation of the C ++ template library.

They are generated from toolbinding sources with the rebuild-all-prebuilt.sh script command in the build / tools section.

To use it, define APP_STL in gnustl_static in Application.mk. See Docs / CPLUSPLUS-SUPPORT.html for more details.

This implementation fully supports C ++ and RTTI exceptions.

But all attempts to use exceptions fail. An alternative NDK exists at http://www.crystax.net/android/ndk-r4.php . Using the hello-jni example from this NDK does not work. Alignment with NDK 5 works after creating Application.xml with

 APP_STL := gnustl_static 

Setting APP_STL to gnustl_static also automatically includes -frtti and -fexceptions . But he is dying the same horrible death as my own experiments.

I managed to get a minimal code example that crashes for me:

 try { __android_log_write(ANDROID_LOG_DEBUG,"foobar","trhown!"); throw "Wrong object type."; } catch (char* b) { __android_log_write(ANDROID_LOG_DEBUG,"foobar","catched!"); } 

CPLUSPLUS-SUPPORT.html I missing something or is this statement in README and CPLUSPLUS-SUPPORT.html just wrong?

+13
c ++ exception android-ndk
Jan 11 2018-11-11T00:
source share
4 answers

It turns out that exceptions work, but only if the exception is inherited from std :: exception. In my case, the exception hierarchy did not always include std :: exception, which violated catch / throw. Curiously, throwing strings as exceptions work when compiling for x86 / Mac OS. I fixed my problem by changing the exceptions I use.

+8
Jan 16 '11 at 2:05 a.m.
source share

NDK-r5 tools support the use of exceptions and RTTI in C ++ code. However, using an STL other than GNU STL as a static library is not supported with RTTI or exceptions.

The provided STLport cannot be used with exceptions or RTTI.

Note that it may be necessary to clean up assembly objects when replacing between STL implementations.

+7
Jan 13 '11 at 16:19
source share

As far as I know, Android NDK never supported exceptions. libstdC ++ itself supports exceptions, but when compiling for android, exception support is disabled (grep for "-fno-exceptions").

See this thread on the android ndk mailing list.

+1
Jan 12 2018-11-11T00:
source share

I have a similar problem using JNI. All exceptions emanating from the JNI method cause a SIGILL error under Android 1.6 and 2.1. It works fine under Android 2.2+

See my problem (please feel free to vote for it or leave a comment):

http://code.google.com/p/android/issues/detail?id=20176

So, currently Android 1.6 and 2.1 do not support exceptions to JNI methods with the latest NDK.

Perhaps this could be fixed in a future version of NDK ...

+1
Sep 20 '11 at 11:02
source share



All Articles