问题
i need to make linux command from native method by c++ code.
this is my class Test.java
public class Test {
static { System.loadLibrary("NDK1"); }
public native String exec(String cmd);
}
then i create header .h by this command :
$ javah -classpath ../bin/classes com.example.new1.Test
after this the header .h is created in this path com.example.new1 then i create jni file and copy .h file and create .cpp file and include .h in it.
#include <string>
#include <iostream>
#include <stdio.h>
#include <jni.h>
#include "com_example_new1_Test.h"
std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
JNIEXPORT jstring JNICALL Java_com_example_new1_Test_exec(JNIEnv* env, jobject javaThis , jstring cmd) {
const char * res;
//res = env->GetStringUTFChars(cmd ) ;
// const char* utf_string;
jboolean isCopy;
res = env->GetStringUTFChars(cmd, &isCopy);
/* ... use string ... */
if (isCopy == JNI_TRUE) {
(env)->ReleaseStringUTFChars(cmd, res);
}
std::string result = exec(res);
return (env)->NewStringUTF((const char* )result.c_str());
}
then call NDK
$ /cygdrive/c/android-ndk-r4/ndk-build
make: Warning: File `/cygdrive/c/android-ndk-r4/build/core/import-locals.mk' has modification time 140582392 s in the future
Cygwin : Generating dependency file converter script
Compile++ thumb : NDK1 <= NDK1.cpp
Prebuilt : libgnustl_static.a <= <NDK>/sources/cxx-stl/gnu- libstdc++/4.6/libs/armeabi/
SharedLibrary : libNDK1.so
Install : libNDK1.so => libs/armeabi/libNDK1.so
Compile++ thumb : NDK1 <= NDK1.cpp
Prebuilt : libgnustl_static.a <= <NDK>/sources/cxx-stl/gnu- libstdc++/4.6/libs/armeabi-v7a/
SharedLibrary : libNDK1.so
Install : libNDK1.so => libs/armeabi-v7a/libNDK1.so
make: warning: Clock skew detected. Your build may be incomplete.
when i run my app there is no error or exception occurred but there is no value return from native method!! this is the log for run
03-03 22:59:55.343: E/SpannableStringBuilder(9257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 22:59:55.343: E/SpannableStringBuilder(9257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 23:00:08.527: E/Trace(9852): error opening trace file: No such file or directory (2)
03-03 23:00:08.717: D/libEGL(9852): loaded /system/lib/egl/libEGL_mali.so
03-03 23:00:08.727: D/libEGL(9852): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-03 23:00:08.727: D/libEGL(9852): loaded /system/lib/egl/libGLESv2_mali.so
03-03 23:00:08.747: D/OpenGLRenderer(9852): Enabling debug mode 0
03-03 23:00:21.960: D/dalvikvm(9852): Trying to load lib /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8
03-03 23:00:21.980: D/dalvikvm(9852): Added shared lib /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8
03-03 23:00:21.980: D/dalvikvm(9852): No JNI_OnLoad found in /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8, skipping init
what is the problem ? by the way, i pass "iwconfig" as linux command, is it depend on what is the command ? also do i need any permission at AndroidManifest ?
来源:https://stackoverflow.com/questions/15191171/no-jni-onload-and-no-return-value-from-native-function