Android jni如何交付字符串数组?
如何传递字符串数组 我发布代码: xx.cppAndroid jni如何交付字符串数组?,android,java-native-interface,Android,Java Native Interface,如何传递字符串数组 我发布代码: xx.cpp JNIEXPORT jstring JNICALL Hello_Native(JNIEnv *env, jobject obj,jstring string) { const char *str = env->GetStringUTFChars(string, 0); return env->NewStringUTF( "Hello from JNI !"); } static JNINativeMethod gMeth
JNIEXPORT jstring JNICALL Hello_Native(JNIEnv *env, jobject obj,jstring string)
{
const char *str = env->GetStringUTFChars(string, 0);
return env->NewStringUTF( "Hello from JNI !");
}
static JNINativeMethod gMethods[] = {
{"JniHello",const_cast<char*>("(Ljava/lang/jsting)Ljava/lang/jsting;"),(void*)Hello_Native}
当在gMethods
中声明JniHello
时,系统总是提示存在问题,并且参数不正确
javah
将为您正确生成它。如果类MyHello
中的Java名称是JniHello
,并且包是com.hello
,那么JNICALL函数必须是Java\u com\u hello\u MyHello\JniHello
。它不可能是Hello_Native
,是你编的jniationalMethod
struct的最后一个成员中java/lang/jsting
这样的类。如果我为您添加缺少的r
,甚至没有java/lang/jstring
。您需要的是JAVA签名,而不是JNI。所以它必须是java/lang/String
javap-s
的输出GetStringUTFChars
时,您还必须在返回之前调用'ReleaseStringUTFChars',否则您就有漏洞。但你迟早会发现这一点
javah
将为您正确生成它。如果类MyHello
中的Java名称是JniHello
,并且包是com.hello
,那么JNICALL函数必须是Java\u com\u hello\u MyHello\JniHello
。它不可能是Hello_Native
,是你编的jniationalMethod
struct的最后一个成员中java/lang/jsting
这样的类。如果我为您添加缺少的r
,甚至没有java/lang/jstring
。您需要的是JAVA签名,而不是JNI。所以它必须是java/lang/String
javap-s
的输出GetStringUTFChars
时,您还必须在返回之前调用'ReleaseStringUTFChars',否则您就有漏洞。但你迟早会发现这一点。const_cast(((Ljava/lang/jsting)Ljava/lang/jsting;)
拼写错误,应该是
const_cast(((Ljava/lang/jsting)Ljava/lang/jstring;)
jsting==>jstringconst_cast(((Ljava/lang/jsting)Ljava/lang/jsting;)
Copy some android source code to help you(JNI):
static jobject osNetworkSystem_getHostByNameImpl(JNIEnv* env, jclass clazz,
jstring nameStr, jboolean preferIPv6Addresses) {
}
static void osNetworkSystem_setInetAddressImpl(JNIEnv* env, jobject obj,
jobject sender, jbyteArray address) {
}
static jobject osNetworkSystem_inheritedChannelImpl(JNIEnv* env, jobject obj) {
}
/*
* JNI registration.
*/
static JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{ "getHostByNameImpl", "(Ljava/lang/String;Z)Ljava/net/InetAddress;", (void*) osNetworkSystem_getHostByNameImpl },
{ "setInetAddressImpl", "(Ljava/net/InetAddress;[B)V", (void*) osNetworkSystem_setInetAddressImpl },
{ "inheritedChannelImpl", "()Ljava/nio/channels/Channel;", (void*) osNetworkSystem_inheritedChannelImpl },
};
int register_org_apache_harmony_luni_platform_OSNetworkSystem(JNIEnv* env) {
return jniRegisterNativeMethods(env,
"org/apache/harmony/luni/platform/OSNetworkSystem",
gMethods,
NELEM(gMethods));
}
拼写错误,应该是
const_cast(((Ljava/lang/jsting)Ljava/lang/jstring;)
jsting==>jstring好建议。我将添加“停止为JNI签名使用错误的手动字符串,而是使用javap-s的输出”。这就解决了(3)+1我更喜欢与您自己的观点1相一致的“停止使用”措辞。@EJP我想避免听起来过于强烈。但我承认,除了模糊的人文情怀之外,没有任何明显的原因。好的建议。我将添加“停止为JNI签名使用错误的手动字符串,而是使用javap-s的输出”。这就解决了(3)+1我更喜欢与您自己的观点1相一致的“停止使用”措辞。@EJP我想避免听起来过于强烈。但我承认,除了模糊的人文情怀之外,没有任何明显的原因。
Copy some android source code to help you(JNI):
static jobject osNetworkSystem_getHostByNameImpl(JNIEnv* env, jclass clazz,
jstring nameStr, jboolean preferIPv6Addresses) {
}
static void osNetworkSystem_setInetAddressImpl(JNIEnv* env, jobject obj,
jobject sender, jbyteArray address) {
}
static jobject osNetworkSystem_inheritedChannelImpl(JNIEnv* env, jobject obj) {
}
/*
* JNI registration.
*/
static JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{ "getHostByNameImpl", "(Ljava/lang/String;Z)Ljava/net/InetAddress;", (void*) osNetworkSystem_getHostByNameImpl },
{ "setInetAddressImpl", "(Ljava/net/InetAddress;[B)V", (void*) osNetworkSystem_setInetAddressImpl },
{ "inheritedChannelImpl", "()Ljava/nio/channels/Channel;", (void*) osNetworkSystem_inheritedChannelImpl },
};
int register_org_apache_harmony_luni_platform_OSNetworkSystem(JNIEnv* env) {
return jniRegisterNativeMethods(env,
"org/apache/harmony/luni/platform/OSNetworkSystem",
gMethods,
NELEM(gMethods));
}