androidandroid-ndkshared-librariesandroid-assets

Android NDK: read file from assets inside of shared library


In my app I have to pass a file from assets folder to shared library. I cannot do it with use of jni right now. I'm using precompiled shared library in my project, in which I have hardcoded path to my file, but I'm getting error "No such file or directory". So in my .apk file I have .so file in libs/armeabi-v7a folder and my file in /assets folder.

I have tried to do it like this:

char *cert_file = "/assets/cacert.cert";
av_strdup(cert_file);

And some other paths, but it doesn't work.

Is it possible at all?


Solution

  • You can simply use the AAssetManager class in C++.

    Basically you need to:

    You can find the official ndk documentation here.

    Edit: To get the AAssetManager object:

    • In a native activity, you main function as a paramater android_app* app, you just need to get it here: app->activity->assetManager
    • If you have a Java activity, you need so send throught JNI an instance of the java object AssetManager and then use the function AAssetManager_fromJava()