Will C/C++ libraries work on Android Phone - android

Does anyone have any experience using JNI to call native C/C++ libraries in Android? Is the environment suitable for running C/C++ libraries and if so is there anything specific about the environment which you need to accommodate?
Thanks

My understanding is that Android provides only a subset of the standard C++ runtime library. For example, Android does not support exceptions in native code. I think there are other restrictions as well.
One complication is that, while Android itself might include many native libraries, only some of them are considered stable enough to link against. The Android NDK page lists the libraries which are safe.
libc (C library) headers
libm (math library) headers
JNI interface headers
libz (Zlib compression) headers
liblog (Android logging) header
A Minimal set of headers for C++ support
If your C library only uses those, you should be fine. C++ support sounds a little spottier.

For a good example of C and Java integration, check this out:
https://github.com/jackpal/Android-Terminal-Emulator
Untar the files and you can find a jni/termExec.cpp - which uses normal C API like "exec()", "fork()", and "open(/dev/ptmx)" to implement terminal emulation (http://linux.die.net/man/4/ptmx).
Looking up the jni/Android.mk file, and u can see that the cpp is compiled as a library - libandroidterm.
And then the java application (src/jackpal/androidterm/Exec.java) will load the library via System.loadLibrary("androidterm").
I think this application provide a small enough example for u to extend whichever way u like - either the cpp or the java file. (The cpp file is basically C-based, not C plus plus).
And remember the mapping between them, for example here it is:
static JNINativeMethod method_table[]
= {
{ "createSubprocess", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
(void*) android_os_Exec_createSubProcess },
{ "setPtyWindowSize", "(Ljava/io/FileDescriptor;IIII)V",
(void*) android_os_Exec_setPtyWindowSize},
{ "waitFor", "(I)I",
(void*) android_os_Exec_waitFor},
{ "close", "(Ljava/io/FileDescriptor;)V",
(void*) android_os_Exec_close} };

for a C library, you shouldn't have any trouble. a C++ library might be more fun if it uses much of the standard library, because most of the C++ standard library is missing, but you can always supply your own "mini-STL". that's basically how external/webkit works.
much of Android's java.util.regex, java.nio.charset, java.util, and java.text are implemented by calling ICU4C, for example. (the library's in external/icu4c and the JNI is in dalvik/libcore/icu/src/main/native.) a mix of ICU's C and C++ interfaces are used, so you can rest assured this stuff gets quite a good workout on a daily basis ;-)

Related

Create .a file instead .so using android studio?

How to create .a file instead .so from c source using android studio?
If it is possible , then also I want want to know how to load them and pros and cons of using .a file instead .so
Android relies on Java's System.loadLibrary() to load native code. This call uses the dl* family of functions to load a dynamic library (commonly associated with extension .so).
The dynamic library can specify other libraries as its dependencies, which will be loaded in turn.
After opening the library, the JVM probes it for functions with specially crafted names, which are bound to the corresponding Java methods.
By contrast, a .a file is a static library. It is simply an archive of object files (.o) that are meant for consumption by static linking when compilation.
To answer your question: yes, you could alter your CMakeLists.txt to create static libraries, but eventually you will have to link these together into a dynamic .so in order to load it in Android. See for example the bottom part of this answer where a number of prebuilt static libraries are linked together with a single .cpp file to produce a dynamic library.

How JNI works on the memory level

I have a java file which loads .so files and prints the result coming from .so file. I don't have a source code for my .so file. Can anyone tell how in the memory structure .so results are loaded and from where this Java class is reading the results generated by .so and printing them out??
If u have any code that is already written in native language and reluctant about changing it but you would want to use native calls in your java codes and not to rewrite entirely everything java. JNI comes in handy. it converts all your platform specific implementations to platform independent. Performance- and platform-sensitive API implementations in the standard library allows all Java applications to access this functionality.
the library files .so are converted in such a manner.
JNIEnv contains all types of conversion from a native data types to the java data types. it also supports suitable implementations for native method calls, signals are also handled by the JVM.
Java being platform independent native processss specific to a hardware and operating system platform are all made platform independent by mapping native data types and method calls to java. JNI performs the above using the library files example .so or .a files
For JNI mapping u can refer the below link
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
Your .so have a JNI glue class that binds all your Java native methods (public native void x();) with compiled native code.
I'm assuming it uses JNI, otherwise I have no idea how its working. If it uses JNI, then it actually creates Java objects on the Java heap in the .so, using the JNI libraries. Look for functions marked "native", these functions map directly to functions in the so that are called when th Java code whats to run something in the library.
The Droidfish Android chess game is written in Java. But its underlying chess engine Stockfish is written in C++. At first, I thought there was a JNI layer that connects these two but there isn't. The C++ stockfish executable is launched, and Java and C++ communicates via stream.
You can launch the C++ stockfish and play with this engine in the command line, no graphical interfaces.
In your question, your native .so library is printing something to stdout/stderr and you want to read these outputs in Java?

Can't remove an empty folder in C++

I want to remove an empty folder using remove() in C++ on Windows 7 but I can't. I tried rmdir() instead of remove() then the folder got removed!
Nevertheless, the reason why I don't use rmdir() is due to Android. In a library project for Android, I can't include "direct.h" header so can't use rmdir(), either. Unlike on Windows, the function remove() works well on Android. I don't understand why.
Anybody knows why this is happening?
Or any other functions which will work on both Windows and Android?
This is a pretty common problem when writing cross-platform programs.
Sometimes, a library can provide the abstraction you need. For example, Boost has a filesystem library that can enumerate files, manipulate directories, etc., on multiple platforms using the exact same code.
Also, there are usually symbols defined that allow you to detect which compiler is currently building your code. Even if there isn't one that does what you want, you can define your own.
Let's say you need to build your software for two different fictitional operating systems named FooOS and for BarOS. I'm going to invent two symbols, FOO_OS and BAR_OS. In my code, I can do something like this:
#ifdef FOO_OS
#include <foo_stuff.h>
#elseif BAR_OS
#include <bar_stuff.h>
#endif
void do_something()
{
#ifdef FOO_OS
do_it_this_way();
#elseif BAR_OS
do_it_that_way();
#endif
}
Now, we just need to either define FOO_OS or BAR_OS. This can be done through an IDE's project configuration or on the command line of the compiler. Use Google to find out about your particular situation, since you didn't include those details in your post.
There is a preprocessing step when you compile your code that makes a pass through the source, and applies these conditional statements. A following pass actually compiles the code. Here is some documentation about Visual Studio's preprocessor, for example.

How setup libsvm in Android?

Im trying to setup libsvm in android to detect motion from accelerometer. I have no idea how to setup libsvm in android and how to use it. guys can you give clue for this ?
You do not setup libsvm, simply use the library wrapper for the language you are using to develop an app for android (Java I guess?). Wrapper is included in the official release. And it also includes the sample of usage of this particular library. There is nothing special here - if you know how to develop android app, then using additional library should not be a problem. If you do not know how to develop such an app - then starting from motion recognition is a bad idea. The same applies for ability to use SVM for anything. If you haven't ever used SVM it would be better to start with something simplier, like writing the "non mobile" version of the app and getting familiar with this model. Otherwise, probability of failing is quite big.
Sorry for my previous wrong answer format
Since the libsvm is written in C, you can easily wrap the code via JNI interface and use libsvm in Java.
A wrapper can be found in: https://github.com/yctung/AndroidLibSvm
For example, once you import this project in Android studio, you can call
jniSvmTrain(String options);
to make the svm training with the same interface of original libsvm.
If you look at the code, it is simply a wrapper of original "svm-train.c" in libsvm
#include "./libsvm/svm-train.h"
// helper function to be called in Java for making svm-train
extern "C" void Java_edu_umich_eecs_androidlibsvm_MainActivity_jniSvmTrain(JNIEnv *env, jobject obj, jstring cmdIn){
const char *cmd = env->GetStringUTFChars(cmdIn, 0);
debug("jniSvmTrain cmd = %s", cmd);
std::vector<char*> v;
// add dummy head to meet argv/command format
std::string cmdString = std::string("dummy ")+std::string(cmd);
cmdToArgv(cmdString, v);
// make svm train by libsvm
svmtrain::main(v.size(),&v[0]);
// free vector memory
for(int i=0;i<v.size();i++){
free(v[i]);
}
// free java object memory
env->ReleaseStringUTFChars(cmdIn, cmd);
}
"Setuping" I think you are asking to add the jar file the LIBSVM provide as a library to your android studio project. You can take a look here:
Android Studio: Add jar as library?
In the LIBSVM website you can download a zip file with the JAVA jar file inside and examples of usage.

Replace string in a function at runtime Android NDK

I have an external compiled static C++ library that I'm using in my android application. This library is reading a file. I want to know if there is a way I can "redirect" the function that's reading the file so that it reads another file.
So if it does:
fopen("myfile.txt", "rb");
I want to intercept it and to do this instead:
fopen("myotherfile.txt", "rb");
In Objective-C I use MethodSwizzling. Is there something similar I can do in C++ or the android NDK?
Short of editing the binary (with uncertain results), your best option is to use a symlink... if you're just doing it for development purposes, you could use adb shell into your test device to create the symlink.
http://developer.android.com/guide/developing/tools/adb.html#issuingcommands
How about contact the author of the library and ask them to introduce a parameter? Having hard-coded file paths is a lousy design anyway, the library will be better off.

Categories

Resources