//kotlin层
external fun listFiles(filePath:String): String
//native层
// 定义LOGD类型
// 定义LOGI类型
// 定义LOGW类型
// 定义LOGE类型
// 定义LOGF类型
extern "C"
JNIEXPORT jstring JNICALL
Java_com_suyf_ndkdev_MainActivity_listFiles(JNIEnv *env, jobject thiz, jstring file_path) {
const char *path = env->GetStringUTFChars(file_path, 0);
DIR *dirPtr = opendir(path);
struct dirent *ptr;
char base[1000];
if (dirPtr == NULL) {
LOGE("opendir fail.....................");
exit(-1);
}
while ((ptr = readdir(dirPtr)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 ||
strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
continue;
else if (ptr->d_type == 8) ///file
LOGE("d_name:%s/%s\n", path, ptr->d_name);
else if (ptr->d_type == 10) ///link file
printf("d_name:%s/%s\n", path, ptr->d_name);
else if (ptr->d_type == 4) { ///dir
memset(base, '\0', sizeof(base));
strcpy(base, path);
strcat(base,"/");
strcat(base,ptr->d_name);
LOGE("d_dir:%s/%s\n", path, ptr->d_name);
jstring newDir = env->NewStringUTF(base);
Java_com_suyf_ndkdev_MainActivity_listFiles(env,thiz,newDir);
}
}
closedir(dirPtr);
return file_path;
}
错误记录:
Fatal signal 5 (SIGTRAP) code 1 (TRAP_BRKPT)
native层函数缺少return返回值