未找到实施

No implementation found

我有一个 Android 应用程序需要引用和使用一些本机 C++ 代码。我是一位经验丰富的 Java 开发人员,但我的 C++ 很欠缺。我正在努力让它达到 运行。我收到以下错误。如果我在 loadLibrary 中更改名称,它会立即崩溃,所以我假设加载工作正常。我该如何解决这个问题?

No implementation found for boolean com.example.myapplication.BamBridge.test() (tried Java_com_example_myapplication_BamBridge_test and Java_com_example_myapplication_BamBridge_test__)


public class BamBridge implements IBamBridge {

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

    private native boolean test();
}

BAM.h:

#ifndef BAM_H
#define BAM_H
#define JNIIMPORT
#define JNIEXPORT  __attribute__ ((visibility ("default")))
#define JNICALL
#include <set>
#include <vector>
#include <string>



extern "C" JNIEXPORT JNICALL bool test();

#endif

BAM.cpp

#include <cstdio>
#include <stdint.h>
#include <iostream>
#include <map>
#include "BAM.h"

#define SWAP_UINT16(val)  ((val << 8) | (val >> 8))






JNIEXPORT JNICALL    bool test()
{
     return true;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.6.0)



add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/BAM.cpp )

在 C 端将您的函数名称更改为

Java_com_example_myapplication_BamBridge_test

As java 搜索特定格式的函数。

在你的头文件中:

extern "C" 
{
    JNIEXPORT jboolean JNICALL Java_com_example_myapplication_BamBridge_test(JNIEnv *, jobject);
}

在您的 CPP 文件中:

extern "C"
{
    jboolean Java_com_example_myapplication_BamBridge_test(JNIEnv * env, jobject this)
    {
        return true;
    }
}