Can I use C ++ exceptions in the JNI library on Android?

Is there a way that I can use C ++ exceptions in the Java Native Interface (JNI) library on Android?

EDIT: I'm talking about C ++ exception handling, which is completely internal to the JNI library. That is, the exception is thrown and gets into the library, and it never leaves the library.

According to the Android documentation (docs / CPLUSPLUS-SUPPORT.html), exceptions are only supported if I use "GNU libstdC ++" as the C ++ runtime instead of the standard one.

The problem is that the documentation also states that all parts of the program should use the same C ++ runtime:

"You can only choose one C ++ runtime that your code will depend on. It is not possible to combine common libraries compiled against different C ++ runtimes."

According to my interpretation, this means that I am forced to use the same C ++ as Dalvik (Java VM on Android).

So, if Dalvik does not use “GNU libstdc ++”, is there any other way that I could use in my JNI lib library?

What is C ++ runtime - is Dalvik compiled against?

EDIT: I have to assume that no matter what Java application uses my JNI library, you might also need to use other JNI libraries that I do not control. Does this limit my options in any way?

+4
source share
1 answer

Yes, you can use exceptions with

APP_STL := gnustl_static 

or gnustl_shared . The document uses a cool intimidating language, but they want you to not mix libraries that await different STL implementations in your application . Therefore, the setting has the prefix APP_ , not LOCAL_ .

Dalvik VM fully supports any of the STL implementations listed in the document.

Usually you control both sides of your JNI and you can make sure that all native components are compiled for gnustl_shared. This is the preferred scenario.

If your company needs to provide a black box library that other people might use that might also include other libraries, it is safer to choose gnustl_static to support C ++ exceptions in your code. Thus, you are not dependent on the goodwill of others. You should be careful when designing your API so that your own objects (and especially exceptions) are never exposed to other components.

+3
source

All Articles