Reputation: 3907
As a Android NDK starter, I try to run helloJni example that I found in ndk website here: https://developer.android.com/ndk/samples/sample_hellojni.html
I managed to compile and run with program with the c code provided in the project. However, I want to convert the code into C++ equivalent. Hours have been spent with no success.
What I have done:
rename jni/hello-jni.c -> jni/hello-jni.cpp
change in jni/android.mk
LOCAL_SRC_FILES := hello-jni.c
to
LOCAL_SRC_FILES := hello-jni.cpp
replacing c code with following c++ code in jni/hello-jni.cpp
#include <string.h>
#include <jni.h>
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *env, jobject obj)
{
return env->NewStringUTF("Hello from C++ over JNI!");
}
Building is successful with output
/home/wenchao/Projects/shared/android-ndk-r10e/ndk-build all
Android NDK: WARNING: APP_PLATFORM android-21 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
[arm64-v8a] Install : libhello-jni.so => libs/arm64- v8a/libhello-jni.so
[x86_64] Install : libhello-jni.so => libs/x86_64/libhello-jni.so
[mips64] Install : libhello-jni.so => libs/mips64/libhello-jni.so
[armeabi-v7a] Install : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
[armeabi] Install : libhello-jni.so => libs/armeabi/libhello-jni.so
[x86] Install : libhello-jni.so => libs/x86/libhello-jni.so
[mips] Install : libhello-jni.so => libs/mips/libhello-jni.so
18:09:12 Build Finished (took 412ms)
But on my device , I got
Unfortunately, HelloJni is stopped.
And very strange, No output in Logcat. Logcat is completely blank. (this might be a separate issue I need to fix , though)
I do not know how to debug at all :(
Upvotes: 2
Views: 548
Reputation: 3907
Got it.
We need the extern "C" wrap arround the c++ code
#include <string.h>
#include <jni.h>
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *env, jobject obj)
{
return env->NewStringUTF("Hello from C++ over JNI!");
}
}
Upvotes: 3