简体   繁体   中英

JNI and old C++ code

I am now writing a JNI version of older c/c++ code I wrote earlier in the year, to include in my android application. I wrote a desktop application equivalent of what I am writing in both Java and C++, and the difference in performance is prominent, and I want to therefore take advantage of the ndk.

I am extremely new to JNI, and the way I understand things, it seems that I have to do some type mapping; jdouble to replace double , jint to replace int , and so on. The thing is though, I've tried running some of my old code unchanged, and it worked! I even have some STL libraries included ( list and vector ) and I faced no problems. I just included APP_STL := stlport_static in the Android.mk file.

So my questions are: is there a performance penalty in doing this? Is there any other risk I should be aware of? What is the disadvantage of using C/C++ primitive types in my jni? Can I make the code run faster, or should I not bother?

Thanks,

NAX

Native types in JNI ( jint , ...) are just typedef aliases of C types. They exist for code portability when internal Java representation doesn't exactly match C, namely, regarding bit size. So, there shouldn't be any performance overhead in mixing them.

The risk or disadvantage of mixing them is that, if they turn out to have different bit sizes in some platform, you can lose bits when converting back and forth. You might thus want to do some extra checks when building the code to make sure it is safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM