Android JNI未定义对错误C ++方法的引用

时间:2019-07-11 10:49:50

标签: android c++ cmake android-ndk java-native-interface

我正在尝试使用Android JNI从AVS SDK调用方法。 源代码是C ++。 但是在构建apk时,我面临以下错误。

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
Build command failed.
Error while executing process /home/useradm/Androidsdk/cmake/3.10.2.4988404/bin/cmake with arguments {--build /root/AndroidStudioProjects/alexaProtoApp/app/.externalNativeBuild/cmake/debug/x86 --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so
FAILED: /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so 
: && /home/useradm/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=i686-none-linux-android24 --gcc-toolchain=/home/useradm/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/useradm/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libnative-lib.so -o /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o  /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/x86/libSampleApp.so /home/useradm/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/24/liblog.so /home/useradm/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/24/liblog.so -latomic -lm && :
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:25: error: undefined reference to 'my_initialize(_JavaVM*, _jobject*)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

这是我的

native-lib.cpp

#include <jni.h>
#include <ostream>
#include "custom_main.h"
#include <android/log.h>

JavaVM *my_jvm;
jobject activity;

extern "C"
JNIEXPORT jint JNICALL
Java_myapp_avs_activities_MainActivity_initialize(JNIEnv *env, jobject instance) {

activity = env->NewGlobalRef(instance);
return my_initialize(my_jvm, activity);


}extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *aReserved) {

my_jvm = vm;
if (vm != NULL)
    __android_log_print(ANDROID_LOG_INFO, "AVSapp", "Assigned");
return JNI_VERSION_1_6;

}extern "C"
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
   __android_log_print(ANDROID_LOG_INFO, "AVSapp", "OnUnload");
}

custom_main.h

#ifndef CUSTOM_MAIN_H
#define CUSTOM_MAIN_H
#include "jni.h"

int my_initialize(JavaVM *, jobject);

#endif

main.cpp

#include "custom_main.h"
int my_initialize(JavaVM* myJavaVM, jobject instance) {

}

注意:由于限制,我无法共享完整代码,因此无法共享代码段。

我的问题是,这个链接器相关的问题吗? ,请指导我解决此问题。

1 个答案:

答案 0 :(得分:0)

在custom_main.h中使用extern c,它将起作用

#ifdef __cplusplus
extern "C" {
#endif

#ifndef CUSTOM_MAIN_H
#define CUSTOM_MAIN_H
#include "jni.h"

int my_initialize(JavaVM *, jobject);

#endif

#ifdef __cplusplus
}
#endif

Build->刷新链接的c ++项目,然后再次重建您的项目