Search code examples
androidandroid-ndk

Android NDK: Why is AAssetManager_open returning NULL


I have some code:

AAsset* pAsset = AAssetManager_open(pAssetManager, "asset_test.txt", AASSET_MODE_STREAMING);
DebugPrint(pAsset?"pAsset not NULL\n":"pAsset NULL");
if (pAsset)
{
    char buf[1024];
    AAsset_read(pAsset, buf, sizeof(buf));
    DebugPrint(buf);
    AAsset_close(pAsset);
}

This code always prints "pAsset NULL" in logcat.

I put the asset_test.txt file in my assets directory, and I looked in the .apk to make sure it exists by renaming the .apk to .zip and opening it with 7zip.

I have some more code:

AAssetDir* pAssetDir = AAssetManager_openDir(pAssetManager, sDirectory.c_str());

if (!pAssetDir)
{
    DebugPrint("pAssetDir NULL\n");
    return;
}

const char* pszDir;
while ((pszDir = AAssetDir_getNextFileName(pAssetDir)) != NULL)
{
    DebugPrint(pszDir);
}

AAssetDir_close(pAssetDir);

This code prints nothing. In other words, no files are ever found in the assets directory, regardless of what paths I pass into it.

Note: DebugPrint is just a prettier looking wrapper around __android_log_print().


Solution

  • I passed the Activity into AAssetManager_fromJava(), while I should have passed the AssetManager in. If you pass the wrong class into AAssetManager_fromJava() it will fail without printing anything to logcat.

    How to get the asset manager with JNI:

        JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
    
        jobject activity = (jobject)SDL_AndroidGetActivity();
    
        jclass activity_class = env->GetObjectClass(activity);
    
        jmethodID activity_class_getAssets = env->GetMethodID(activity_class, "getAssets", "()Landroid/content/res/AssetManager;");
        jobject asset_manager = env->CallObjectMethod(activity, activity_class_getAssets); // activity.getAssets();
        global_asset_manager = env->NewGlobalRef(asset_manager);
    
        pAssetManager = AAssetManager_fromJava(env, global_asset_manager);
    

    Stash that asset manager pointer somewhere and use it for all your AAssetManager_*() functions from now on.