combine Java code with C++ code?

Community Forums/General Help/combine Java code with C++ code?

Captain Wicker (crazy hillbilly)(Posted 2013) [#1]
I know how to combine BlitzMax code with C/C++ because C code compiles within BlitzMax, but anybody know if it is possible to use C++ code in a Java (android) application with existing Java source code? I'm trying to compile Java and C++ code into the same application. I've seen some applications on Google Play (new android market) that claim to have been written using C++ and need to know how to do that??

Thanks!


Yasha(Posted 2013) [#2]
The thing you're looking for is called the Android NDK: http://developer.android.com/tools/sdk/ndk/index.html

Note that this has nothing to do with the way you would combine Java and native code on the desktop. The Android Java platform is different in a lot of ways.


However, you should seriously consider your reasons for wanting to use C++ on Android. There are a lot of reasons why this isn't a great idea, and very few reasons why this would be a good idea. It will make your app less portable and may damage its performance.

In the highly unlikely event that you need something other than the Java environment to run your code, you would likely do well to completely ignore the NDK and use RenderScript: http://developer.android.com/guide/topics/renderscript/index.html


Captain Wicker (crazy hillbilly)(Posted 2013) [#3]
Thanks for the information, Yasha.
you should seriously consider your reasons for wanting to use C++ on Android

My reason for wanting to use C++ with Java is because I found a port of Irrlicht that is compatible with Android, and assumed it would require C++, since it was originally written using C. I don't know what all is involved in setting up the Render Script you linked to, but looks like could be useful for my current project (on my home page), which I am in the process of porting (or mostly rewriting completely) for use on mobile platforms (psp, android, etc). From what I am understanding, the port requires the Android NDK that you mentioned in your first response post. (thanks for the link btw)
The port and post I found is here (if anyone is interested): http://irrlicht.sourceforge.net/forum/viewtopic.php?t=37235


azrak(Posted 2013) [#4]
Java JNI.
Maybe HaxeNme can help you.


D4NM4N(Posted 2013) [#5]
I agree with Yasha, Java is much safer and more suitable for doing 99% of normal android coding, however there might be an occasion where you have to break out of the box. I had a problem last year (to cut a long story short) involving enormous strings, a 32Mb heap and GC... which we solved by writing a native C++ lib. But that is the only time it has been needed up until now.

On the other hand i do reccommending installing and playing about with the NDK for academic reasons, however if your actual real-world app starts to see too much of it then you are doing something wrong.

Of course if you want to do anything cutting edge graphically that is not covered by a 3rd party engine then you probably will be using it a lot, but tbh if you want to actually -produce- anything app-wise it is not a route i reccommend.