Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed? Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)
-
Welcome on board. Please post some code you have already tried. – amonk Jun 13 '17 at 10:28
-
Why do you think that you need a `Parcelable` object? What's wrong with `int/long/float`? – Michael Jun 13 '17 at 11:20
-
I haven't started with coding yet as I do not have the idea how to do it. Is it possible to create such an app that communicates with C++ program in first place? – deepCode Jun 13 '17 at 11:21
-
The C++ part would be a shared object (i.e. a library), not a standalone program. But yes, it's possible. Read up on the Android NDK. – Michael Jun 13 '17 at 11:31
-
Note that executable programs on Linux don't usually have **.exe** suffix. If you expect to reuse a Windows executable, you will find that these cannot run on Android. – Alex Cohn Jun 13 '17 at 13:23
2 Answers
To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2")
. There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder
, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.

- 56,089
- 9
- 113
- 307
You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.

- 12,464
- 3
- 23
- 45