views:

82

answers:

1
+1  Q: 

Android C++ NDK

I am trying to compile the following for the android ndk

#include <jni.h>
#include <string.h>

extern "C" {
    JNIEXPORT jstring JNICALL Java_com_knucklegames_helloCpp_testFunction(JNIEnv * env, jobject obj);
};

JNIEXPORT jstring JNICALL Java_com_knucklegames_helloCpp_testFunction(JNIEnv *env, jobject obj) {
 return env->NewStringUTF(env, "Hello from native code!");
}

but it is giving the following error

Compile++ thumb: helloCpp <= /cygdrive/c/workspace/helloCpp/jni/main.cpp
/cygdrive/c/workspace/helloCpp/jni/main.cpp: In function '_jstring* Java_com_knucklegames_hello
Cpp_testFunction(JNIEnv*, _jobject*)':
/cygdrive/c/workspace/helloCpp/jni/main.cpp:10: error: no matching function for call to '_JNIEn
v::NewStringUTF(JNIEnv*&, const char [24])'
/cygdrive/d/android/android-ndk-r4b/build/platforms/android-8/arch-arm/usr/include/jni.h:839: note: candidates
 are: _jstring* _JNIEnv::NewStringUTF(const char*)
make: *** [/cygdrive/c/workspace/helloCpp/obj/local/armeabi/objs/helloCpp/main.o] Error 1
+1  A: 

The NewStringUTF function only takes one argument, a c-string:

env->NewStringUTF("Hello from native code!");

There is a C version that goes like this:

NewStringUTF(env, "Hello from native code!");

But you are obviously using the C++ version.

PigBen
Thanks this solved the error, but it is now force closing when trying to load the libary. It is in lib folder under the name of libhelloCpp.so and static { System.loadLibrary("helloCpp"); } is the problem
Will03uk
If you have another question, you should make another post. More people are likely to see it that way.
PigBen
Make sure that the .so is under libs/armeabi ; when the APK is installed to the device, you should see it under /data/data/com.knucklegames.helloCpp/lib/libhelloCpp.so
I82Much