AAssetManager_fromJava的未定义引用

17
我正在尝试使用AAssetManager从Android apk中访问资源。然而,即使我已经包含了asset_manager.h和asset_manager_jni.h,我仍然不断收到“Undefined reference to AAssetManager_fromJava”的错误提示。来自asset_manager.h的其他函数,例如AAssetManager_openDir(mgr,“”)等也无法被引用。
下面是完整的代码:
#define EXPORT_API

#include <string.h>
#include <jni.h>
#include <android\log.h>

#include <sys\types.h>
#include <android\asset_manager.h>
#include <android\asset_manager_jni.h>

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "com.devin - native", __VA_ARGS__)

JNIEnv* env=0;

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* pvt){
    LOGD("JNI_OnLoad() called");
    vm->AttachCurrentThread(&env, 0);
    return JNI_VERSION_1_2;
}

EXPORT_API void LoadAsset(char* filename, jobject assetManager){
    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
        /* More stuff */
}

#ifdef __cplusplus
};
#endif

这段代码在一个.cpp文件中,并且使用NDK R8编译。我做错了什么吗?

5个回答

19

如果你在 Android Studio 中有一个名为 "CMakeList.txt" 的 ExternalNativeBuild 文件,那么你必须将以下代码添加到 CMakeList.txt 文件中。

find_library( # Sets the name of the path variable.
          android-lib

          # Specifies the name of the NDK library that
          # you want CMake to locate.
          android )
target_link_libraries( 
                   ${log-lib}
                    ${android-lib})

如果您也有本地库,可以像这样轻松添加:

target_link_libraries( native-lib
                   ${log-lib}
                    ${android-lib})

它应该可以工作!


18

我的错误。我没有将"android"库添加到链接器中。实际上,我在Visual Studio Express上设置了NDK开发环境,并且"android"库并没有默认添加到我的项目中。

如果您正在使用makefiles,请确保在使用本地AssetManager时将-landroid添加到您的LOCAL_LDLIBS中。


我遇到了同样的问题,即使我在Android.mk文件中添加了-landroid到你的LOCAL_LDLIBS。 - Ege
#救命稻草 非常感谢这个。 - FrickeFresh
如何在Android Studio 3.1.3上实现这个?我找不到Android.mkLOCAL_LDLIBS - zwcloud

3
find_library( # Sets the name of the path variable.
    log-lib

    # Specifies the name of the NDK library that
    # you want CMake to locate.
    log
)

find_library(android-lib android)

target_link_libraries( # Specifies the target library.
    hll
    ${log-lib}
    ${android-lib}
    # Links the target library to the log library
    # included in the NDK.
)

3
我在gradle.build中添加了以下内容: android.ndk { ldLibs.addAll(["android","log"]) } 这将添加android和log库到您的NDK构建中。

1
我通过在Android.mk中添加以下内容进行修复。
LOCAL_SHARED_LIBRARIES += libandroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接