15

我正在尝试使用 AAssetManager 从 android apk 访问资产。但是,即使我已经包含了asset_manager.h 和asset_manager_jni.h,我仍然不断收到“对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 进行编译。我在这里做错了什么吗?

4

5 回答 5

17

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.

于 2012-09-23T12:29:15.893 回答
16

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})

它应该工作!

于 2017-04-04T13:45:13.510 回答
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.
)
于 2018-08-31T02:24:44.693 回答
3

我将以下内容添加到 gradle.build

android.ndk { ldLibs.addAll(["android", "log"]) }

于 2016-01-18T17:38:05.943 回答
1

我通过将以下内容添加到 Android.mk 来修复它

LOCAL_SHARED_LIBRARIES += libandroid
于 2014-01-03T18:14:58.307 回答