I have an Android app needs to reference and use some native C++ code. I'm an experienced Java dev, but my C++ is lacking. I'm struggling to get it to run. I'm getting the error below. If I change the name inside of loadLibrary, it crashes immediately, so I'm assuming that the load works fine. How do I fix this?
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 )
On the C side change your function name to
Java_com_example_myapplication_BamBridge_test
As java searches for the function in the specific format.
In your Header file:
extern "C"
{
JNIEXPORT jboolean JNICALL Java_com_example_myapplication_BamBridge_test(JNIEnv *, jobject);
}
In your CPP file:
extern "C"
{
jboolean Java_com_example_myapplication_BamBridge_test(JNIEnv * env, jobject this)
{
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With