0

我有一个 android 项目,它在加载本机库后对 native-lib.cpp 进行本机 jni 调用,如下所示。

    static {
            System.loadLibrary("native-lib");
        }

    public native String stringFromJNI(String pInput);

现在,我有一个不属于我的 .so 文件(仅了解要使用的接口)。我需要在我的 native-lib.cpp 中使用 .so 的标头。

#include <jni.h>
#include <string>
#include "Interface.hpp" // This header is present in .so

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_androidcli_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */, jstring jStr) {
    const char *str= env->GetStringUTFChars(jStr, 0);

    std::string res = SendToCpp(str); //This function is in "Interface.hpp" inside .so

    env->ReleaseStringUTFChars(jStr, str);
    return env->NewStringUTF(res.c_str());
}

我如何将 .so 打包到我的项目中,因为它只与我的 .cpp 文件直接连接?

将 .so 文件放在项目的 cpp 文件夹中后,我尝试将其作为库添加到我的 cmake 中。

add_library( # Sets the name of the library.
        shared_lib.so

        # Sets the library as a shared library.
        SHARED
        
        )

然后尝试如下链接:

target_link_libraries( # Specifies the target library.
                       native-lib
                       shared_lib.so

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

但是没有找到“Interface.hpp”。如果我遗漏了什么,请指导。

4

0 回答 0