Skip to content Skip to sidebar Skip to footer

Cannot Resolve Corresponding Jni Fuction

I am making an app to send data through serial port. this requires to call methos from a native library I have two native methods 'open' 'close' I have generated to .so libraries u

Solution 1:

As I explained elsewhere, don't expect Android Studio to resolve magically the native method declarations into a prebuilt library (even if it is correctly copied into src/main/jnLibs).

You can simply ignore this error message: your APK will still install the prebuilt library, and the native method will be resolved at run time.

You can add @SuppressWarnings("JniMissingFunction") annotation for these method, or for the entire class:

@SuppressWarnings("JniMissingFunction")
private native static FileDescriptor open(String path, int baudrate);

@SuppressWarnings("JniMissingFunction")
public native void close();

If you can install the APK on any device, or even on emulator, and the SerialPort class is loaded, then your JNI wrapper is configured correctly. When the system fails to load a native library, it writes helpful error messages to logcat.

Let me expand a bit on "the SerialPort class is loaded". In Java, the classloader may (rather 'should') defer loading a class until it is really necessary. So, simply having the class in your APK will not trigger its static constructor. But if you have a field

private SerialPort m_serialPort = new SerialPort();

in your MainActivity, then the class will be loaded, and the JNI will be initialized even if you don't actually touch this m_serialPort at all. Note that I added an do-nothing default constructor to SerialPort class:

public SerialPort() {}

This does not test the JNI code itself, like conversion of the parameters and such. If you don't have a real device that can be used to test your code, you should design some mock interfaces that will play the role of the actual serial port.


Post a Comment for "Cannot Resolve Corresponding Jni Fuction"