Reputation:
I am working on android-ndk in which i try to pass string from my C code to my java and then in activity. My codes are given below but i am getting the following errors. Please help me out here
#include <com_testing_ndk_FibLib.h>
JNIEXPORT jstring JNICALL Java_Sample1_stringMethod(JNIEnv *env, jobject obj,
jstring string) {
const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string
char msg[60] = "HelloWorld";
jstring result;
strcat(msg, name);
(*env)->ReleaseStringUTFChars(string, name);
puts(msg);
result = (*env)->NewStringUTF(msg); // C style string to Java String
return result;
}
Error log:
jni/com_testing_ndk_FibLib.c: In function 'Java_Sample1_stringMethod':
jni/com_testing_ndk_FibLib.c:7:55: error: 'NULL' undeclared (first use in this function)
const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string
^
jni/com_testing_ndk_FibLib.c:7:55: note: each undeclared identifier is reported only once for each function it appears in
jni/com_testing_ndk_FibLib.c:7:21: error: too few arguments to function '(*env)->GetStringUTFChars'
const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string
^
jni/com_testing_ndk_FibLib.c:11:2: warning: incompatible implicit declaration of built-in function 'strcat'
strcat(msg, name);
^
jni/com_testing_ndk_FibLib.c:12:2: warning: passing argument 2 of '(*env)->ReleaseStringUTFChars' discards 'const' qualifier from pointer target type
(*env)->ReleaseStringUTFChars(string, name);
^
jni/com_testing_ndk_FibLib.c:12:2: note: expected 'jstring' but argument is of type 'const char *'
jni/com_testing_ndk_FibLib.c:12:2: error: too few arguments to function '(*env)->ReleaseStringUTFChars'
jni/com_testing_ndk_FibLib.c:14:11: warning: passing argument 1 of '(*env)->NewStringUTF' from incompatible pointer type
result = (*env)->NewStringUTF(msg); // C style string to Java String
^
jni/com_testing_ndk_FibLib.c:14:11: note: expected 'const struct JNINativeInterface **' but argument is of type 'char *'
jni/com_testing_ndk_FibLib.c:14:11: error: too few arguments to function '(*env)->NewStringUTF'
make: *** [obj/local/arm64-v8a/objs/com_testing_ndk_FibLib/com_testing_ndk_FibLib.o] Error 1
My Java Code:
public class FibLib {
static {
System.loadLibrary("com_testing_ndk_FibLib");
}
public static native String stringMethod(String text);
public static void main(String[] args)
{
System.loadLibrary("Sample1");
FibLib sample = new FibLib();
String text = sample.stringMethod("world");
System.out.println("stringMethod: " + text);
}
}
Upvotes: 0
Views: 2869
Reputation: 14473
All your JNI calls are missing the first parameter which should be env
, as you're in compiling C code.
If your code was in C++, you could make calls like env->NewStringUTF("xx")
. But in C, JNIEnv* isn't an object, hence you have to pass it as first argument, like so: (*env)->NewStringUTF(env, "xx")
As for NULL being undefined, you can solve this by including the header that defines it: #include <stddef.h>
Upvotes: 1
Reputation: 3963
The second parameter does not seem to be optional:
jboolean iscopy;
const char *name = (*env)->GetStringUTFChars(string, &iscopy);
Upvotes: 0