Other NDK/JNI (C/C++) Tutorial Request

wonder

Expert
Licensed User
Longtime User
Hey guys!

I've seen a few threads about this subject, but I'd really like to see a tutorial on how to integrate C/C++ code in a B4A project. To my understanding so far, the C/C++ code has to be compiled into an .so lib, which in turn should be called by a Java lib, in B4A.

I understand the basic concept, but I'm not sure about the steps to be taken. Should I code in C or C++? Which C/C++ compiler should I use? How should I compile? How to create the Java lib? Should it be created in Eclipse?

Those are some of the questions I'd like to see answered in a tutorial.
So if, there is anyone out there who has the time and patience, I'd be eternally grateful. :)
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Example:

I have the following C project (http://goo.gl/dhE1IZ):
B4X:
#include <stdio.h>

int main()
{
    printf("%d\n", sum(5,4));
    return 0;
}

int sum(int a, int b)
{
    return a + b;
}

How do I compile it into an .so lib, so that it can be part of a Java lib called "NDKSum"?
The final goal is to have the following B4A code:
B4X:
Dim Test as NDKSum
Log(Test.Sum(2, 4))

'The output should be 6.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Ahahha, alright, alright! I can use google as well... ;) :D
What I'm requesting on this thread is a specific B4A tutorial. In my opinion, this kind of "know-how" would be a great addition for both our community and the whole B4A ecosystem. :)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
It's alright, the Android Studio link you provided seems to be actually helpful. I'll keep trying to figure it out on my own...
Most of the basic structure of C is quite similar to Java...
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
The general syntax of C (and C++) is similar to Java but C (and C++) is (are) an entirely different animal(s). You can do a LOT more damage with poorly coded C (and C++) than you can with poorly coded Java/B4A. If you don't have any really compelling reasons to run native code, then don't. And note that performance isn't as much a good reason to run native code on Android as it used to be. On Android 5.0+, apps are compiled ahead-of-time by the Android Runtime, which means they run as machine code directly on the processor, just as native code would. This doesn't necessarily mean that ART code will perform as well as NDK code, but ART code will most likely outperform Dalvik code by a significant amount. This means the performance boost you'd get by switching to the NDK is less significant on newer Android versions, but the danger of using the NDK is the same.

This article has a good summary of the possible performance benefits: http://www.learnopengles.com/a-performance-comparison-redux-java-c-and-renderscript-on-the-nexus-5/ The author notes the performance of gcc 4.6 over gcc 4.8 and the performance of gcc, in general, over clang. Be sure to check out the comments where a commenter talks about how, on ARM, armcc is a lot faster than gcc and clang. Interesting stuff. The article doesn't get much into it but if your algorithms can be massively parallelized and they have sufficient arithmetic complexity (and not much branching), then they are prime candidates for being executed on the GPU via RenderScript.

This tutorial is where I started with the NDK: You'll probably conclude, as I did, that writing a generic NDK library for B4A is more trouble than it's worth (if it's even possible). But wrapping a Java library that makes NDK calls shouldn't be too much harder than wrapping a pure Java library.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Thank you so much for your reply, @Roycefer. You explanation was really detailed, very much appreciated. :)

I've have started to watch this youtube series earlier today, I'm on the 4th video now.
I'm going to have a look at the article you shared as well.

The code I want to (re)write in C/C++ is almost entirely composed of math operations, namely geometry and trigonometry. My main goal is being able to perform ~32768 operations somewhere in between 2 and 8 milliseconds. For this kind of code, what would you suggest? C or C++?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I've read that there's an extremely slight advantage in speed to C over C++. However, that slight advantage is probably outweighed by the inconvenience of not having classes.

Have you tried parallelizing your code in B4A before trying to port it to C/C++? If you're running single-threaded right now, you could cut your execution time down by a factor equal to the number of processors on your device, provided the complexity of your calculations is high enough.

Also, if your calculations are what are called "SIMD" (single instruction, multiple data), you can get massive speed improvements by running them on the GPU with RenderScript. For example, if you are performing essentially the same transformations on 32768 geometrical points and you can accept single-precision floating point results, you can perform those transformations on all your points simultaneously by running them on the GPU. On the desktop, I've seen performance improvements of 50-100x going from single-threaded CPU to the GPU (via OpenCL). I haven't done any GPU computing on an Android device however I'd imagine the GPU/CPU performance ratios are probably similar. And the effective ratio is probably a good deal better since Dalvik/ART are so much slower than Oracle's HotSpot JVM.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Thank you so much for the detailed explanation. I'm not trying to perform geometric transformations, but rather perform much simpler calculations, namely determining the distance between two spheres in 3D space.

Not only I want to see how much difference it makes to perform such calculations in native code, (re)learning C/C++ will also help me advance my career in the company I currently work for. Basically I was told, if I learn Linux/C/C++ I will get a promotion by the end of the year. :)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Oh, by the way, check out Derek Banas' channel on YouTube. He has lots of great programming tutorials. Here, he crams in the entire C++ language in an hour++: .
This should be easy to follow since this material won't be entirely new to you.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Thanks a lot! I've watched the whole video and subscribed to his channel. I've learned C back in high-school, but this is way more complex. I got a little bit lost once he reached the Classes part, but I believe I'll be able to work it out. :)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I think he has a longer, more gradual, more involved series of C++ tutorials, as well. You'll have to search way back in his videos, though. Or maybe check his playlists.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi Wonder, have you been able to get the NDK set up properly, i'm trying to build some projects but i'm having a hard time setting up the NDK files, i would like to hear about your experience with this as far as setting it up, it would really be helpful.

So far i've downloaded the NDK folder but i'm stuck in setting up the environment variable, i've tried several tutorials but they all say different things, i rather get some advice from someone who has done this already.

Thanks,
Walter
 
Upvote 0
Top