Running C/C++ code using OpenCV in android - android

I don't know whether i make sense or not. But just wanted to now that can i run the c/c++ code in android without the need of writing JNI wrapper or using Native Activity way?
I mean, I am having a simple C++ Template Matching code which contains main function and I use to run it using g++ in terminal.
Now I want this Template Matching code to be run in android usng NDK. Is there any way? I have googled a lot but all they is to either use JNI wrapper or use SWIG which makes JNI wrapper, but can't actually get into any of them. I need more simpler solution.

If you have a rooted phone, you can compile and run programs as on any linux machine, from the adb shell.
However, that requires a bit of knowledge about Linux.
So, to start:
Root your phone (force it to give you admin privileges). That is easy for developer phones/boards, and a kill for regular phones bought at a shop downstreet.
compile your app for android and run it from adb as you would do from a linux shell.
Here is a more detailed answer on the matter
How to compile C into an executable binary file and run it in Android from Android Shell?

Related

How do I execute a Fortran based program in Android

I have coded a program in Fortran an works perfectly in Windows, my question is if there is any way I can use it in Android.
I don't have the reputation to just make a comment, but I will answer the concern you raise of "not wanting to install compilers" to build fortran programs for android. On pretty much any operating system there are no default compilers for any language, so you have to install compilers (luckily for fortran on windows you even have a lot of options). For Android, right now, you have only one choice. That is to build your own custom version of the NDK gcc cross toolchain and force it to build gfortran and libgfortran. As of right now, gcc has been deprecated in the toolchain. Soon that won't even be an option. I have recently built the previous version of the NDK with gfortran using the following link:
https://github.com/buffer51/android-gfortran
The other problem you're going to run into is that depending on what kind of application you have on windows (command line, gui, webapp, etc.), you're not going to be able to run it on a typical android tablet or phone because there is no command line interface by default (you'll probably have to root the device to get one). The easiest way to develop a usable android app is to write a Java app (using the SDK), and then use JNI to interface with a fortran "library". Unfortunately, there is no straightforward way to compile and use your program on Android, and soon even the complicated ways (building a custom compiler) will get even more complicated.

Is there a way to execute adb commands programatically in C++? This C++ part of code is built using ndk build in android studio. There

I referred to this link - Use CreateProcess to execute ADB command. The approach used in the above link requires Windows.h file which is contained in windows SDk. Since i am executing Android Studio in Mac i will not be able to get the include file. I am not sure whether the above mentioned approach can be performed using Android Studio in Mac.
You can use system or popen methods to start an adb process.

Android privileges to execute native applications?

I downloaded the c4droid app for Android and am running commands through system();. I'm learning that somethings work while others don't. Most of the cool stuff don't work and this appears to be due to my user profile not being given the rights to execute such commands at the Linux OS level.
So tried another experiment. I got a special Gnu compiler for the arm processor and compiled a simple hello world app. Then I put on my phone and tried to execute it through the c4droid app like system("./myapp.bin"); . I got a permission denied message.
So I'm just trying to understand what I can do and what I can't do on my phone that paid good money for? Can I execute such a hello world app or not? Do I really need root access to execute an application I made? Is there a way to get my code to run by wrapping it in android/java code? Do I have to go through the Dalvikvm to get this to run?
I'm really looking for a way to do this without rooting it or downloading something like busybox and using su.
Many many different issues.
permission denied is one of the few error messages the primitive shell knows, and it's used for many other types of failures including not finding the requested command.
The toolbox suite is missing many typical unix commands. People sometimes install busybox as a more comprehensive supplement
Some of the things you may want to do will actually require permissions that the shell (or even more so, application user id) accounts do not have. Only on an emulator or engineering device does the adb shell run as root by default, though on a 'rooted' device you may be able to get to a root shell.
You do not need root access to run compiled code, however the code must have a suitable ABI, must have all it's library dependencies satisfied, and must be installed in a file with the executable flag, on a partition which is not mounted with non-executable flag. Some of the issues you face there are glibc (or other) vs Android's bionic libc. Executable scratch directories vary by android version, though the private storage area of an app is an option if the app sets the file to world readable and executable.
The usual (and only "recommended") means of running native code is to build it as a shared library linked against android's Bionic libc, and call into it from a dalvik/java app using JNI.

How to embed C programs in an Android application

I'm working on a software designed to compile and run on Linux. I was recently asked to cross-compile it for Android using the NDK, which was surprisingly straightforward.
To do this quickly, we just compiled the source, without writing any JNI wrapper. I copied the resulting binaries with adb push to an emulator (rooted) and ran it from the adb shell. Everything worked fine.
Now my question is: How can I embed those binaries somehow in an android application and run them (they act like services and must run in the background) ?
I'd like to avoid changing the existing code to support a JNI interface, but writting a small JNI/C code that just exec the other binaries is fine.
Bonus question: once embedded, is there a way to know the path of the installed binaries ?
http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/index.html
i found the nice tutorial in IBM developersworks about Android NDK

Android NDK running native processes

I want to run tcpdump on the NDK using Process.start() and want to capture this output to a buffer.Can anybody show me how i can do this?Also is the process created in the same Dalvik Vm as the JNI call or is it in a different VM?
Running native processes is not really a supported use of the ndk, but you wouldn't be the first person to do it.
Also note that tcpdump may need to be run as root or setuid root to be useful. And you do have a version of tcpdump compiled and linked appropriately for android?
I think that you actually want android.os.Exec.createSubprocess which is um, not public, rather than Process.start. There's a right way to do it using reflection, and a wrong way to do it by copying the Exec.java (which has declarations of the native methods) into your project. You can use a pipe to recover the results, or you can launch it through the shell and redirect the output to a file which you read from java.
See the source of an android terminal emulator with a local shell option for an example of how to do it.
(Actually, I should restate this - the right way to do it is to duplicate the native functionality of the non-public Exec methods in your own jni lib, so that you are immune to changes in that non-public part of the platform).

Categories

Resources