这段代码通过使用C++代码调用了Linux命令,但是如何将它转换为通过NDK将它用作lib? 我看到只使用.C文件的示例,以及Jni使用的与C++变量不同的变量。如何转换C++代码为NDK?
这个C++代码,我需要将其转换为然后用它与NDK
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(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;
}
这是合适的呢?
在Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := NDK1
LOCAL_SRC_FILES := NDK1.cpp
include $(BUILD_SHARED_LIBRARY)
假设我的本地方法是执行exec(字符* CMD), 为NDK1.cpp
#include <string>
#include <iostream>
#include <stdio.h>
#include <jni.h>
jstring Java_com_mindtherobot_samples_ndkfoo_MainActivity_exec(JNIEnv* env, jobject javaThis , Jchar* cmd) {
......... same code above
return result;
}
如何安排这些文件有正确的解决方案?
我怀疑你可以使用'std :: string'返回值与JNI进行交互 – 2013-03-02 12:02:27