问题
I am trying to access assets from an android apk using AAssetManager. However, I keep getting "Undefined reference to AAssetManager_fromJava" even though I've included asset_manager.h and asset_manager_jni.h Other functions from asset_manager.h, like AAssetManager_openDir(mgr, "") etc also can't be referenced.
Here's the complete code
#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
This code is in a .cpp file and being compiled with NDK R8. Am I doing something terribly wrong here?
回答1:
My mistake. I didn't have "android" library added to the linker. I have actually setup NDK dev environment on Visual Studio Express and "android" library wasn't added to my project by default.
If you are using makefiles, be sure to add -landroid to your LOCAL_LDLIBS when using native AssetManager.
回答2:
Android Studio developers, İf you have ExternalNativeBuild file which is called "CMakeList.txt" you must append this code to the file 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})
if you also have native lib you can add easily like this
target_link_libraries( native-lib
${log-lib}
${android-lib})
It should work!
回答3:
I added the following to gradle.build
android.ndk {
ldLibs.addAll(["android", "log"])
}
回答4:
I fixed it by adding following to Android.mk
LOCAL_SHARED_LIBRARIES += libandroid
回答5:
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.
)
来源:https://stackoverflow.com/questions/12552208/undefined-reference-to-aassetmanager-fromjava