CMake,JNI 提升读取 json 文件 - Android

CMake, JNI boost read json file - Android

boost::property_tree::json_parser::read_json

打开文件的正确方法是什么

当前树:

boost::property_tree::ptree config;
boost::property_tree::json_parser::read_json("conf/file.json", config);

但是我收到找不到文件的错误

terminating with uncaught exception of type boost::wrapexceptboost::property_tree::json_parser::json_parser_error: conf/file.json: cannot open file

我将如何继续将文件复制到设备中并能够打开它?

我使用这段代码让它工作,基本上你将文件从 Android raw 目录复制到 C++ JNI 代码可以看到的设备上的某个位置。

MainActivity代码:

private static final String RES_RAW_CONFIG_PATH_ENV_VAR = "RES_RAW_CONFIG_PATH";
private static final String RES_RAW_CONFIG_FILE_NAME = "res_raw_config.json";

@Override
protected void onCreate(Bundle savedInstanceState) {
    initResRawConfig(true); // this will copy the file
    ...
}

private boolean existsInFilesDir(String fileName) {
    val file = File(filesDir, fileName)
    return file.exists()
}

private void initResRawConfig(boolean forceCopy) {
    if (existsInFilesDir(RES_RAW_CONFIG_FILE_NAME) && !forceCopy) {
        Log.d(TAG, "Config file: " + RES_RAW_CONFIG_FILE_NAME + " already exists in " + getFilesDir().toString());
    }
    else {
        copyFileFromResToFilesDir(R.raw.res_raw_config, RES_RAW_CONFIG_FILE_NAME);
        Log.d(TAG, "Config file: " + RES_RAW_CONFIG_FILE_NAME + " copied to " + getFilesDir().toString());
    }

    // Set Environment Variable to get path from native
    try {
        Os.setenv(RES_RAW_CONFIG_PATH_ENV_VAR, getFilesDir().getAbsolutePath(), true);
    } catch (ErrnoException e) {
        e.printStackTrace();
    }
}

private void copyFileFromResToFilesDir(int fromResId, String toFile) {
    InputStream is =  getResources().openRawResource(fromResId);
    byte[] buffer = new byte[4096];
    try
    {
        FileOutputStream fos = openFileOutput(toFile, Context.MODE_PRIVATE);

        int bytes_read;
        while ((bytes_read = is.read(buffer)) != -1)
            fos.write(buffer, 0, bytes_read);

        fos.close();
        is.close();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

public String getResRawConfigDir()
{
    return getFilesDir().toString();
}

JNI代码:

// get file path from java method
std::string getResRawConfigDirFromJava(JNIEnv *env, jobject obj) {
    jclass clazz = env->GetObjectClass(obj); // or env->FindClass("com/example/myapp/MainActivity");
    jmethodID method = env->GetMethodID(clazz, "getResRawConfigDir", "()Ljava/lang/String;");
    jobject ret = env->CallObjectMethod(obj, method);

    auto jConfigDirPath = (jstring) ret;

    return std::string(env->GetStringUTFChars(jConfigDirPath, nullptr));
}

extern "C" JNIEXPORT jstring
sampleFunc(JNIEnv *env, jobject thiz) {
    std::string configPath = getResRawConfigDirFromJava(env, thiz);
    std::string filePath = configPath + "/" + "res_raw_config.json";
}

参考:https://github.com/nkh-lab/ndk-config-provider/blob/master/app/src/main/java/com/example/myapp/MainActivity.java