简体   繁体   中英

Android calling C++ function getting UnsatisfiedLinkError

I have following function in my native-lib.cpp file:

JNIEXPORT jlong JNICALL
Java_com_example_z_myapplication_MainActivity_convert32to64(JNIEnv *env, jobject instance,
                                                             jlong l) {

// TODO
l = l + 76561197960265728L;
return l;
}

In my MainActivity.java:

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    public static native long convert32to64(long l);

    ...
}

I have a CMakeList.txt:

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

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/cpp/native-lib.cpp )        

However I am getting this error:

java.lang.UnsatisfiedLinkError: No implementation found for long com.example.z.myapplication.MainActivity.convert32to64(long) (tried Java_com_example_z_myapplication_MainActivity_convert32to64 and Java_com_example_z_myapplication_MainActivity_convert32to64__J)

can anyone tell me whats wrong here?

You need extern "C" for C++ JNI.

extern "C" JNIEXPORT jlong JNICALL
Java_com_example_z_myapplication_MainActivity_convert32to64(
        JNIEnv *env, jobject instance, jlong l) {
    // ...
}

It seems like jni library is builded wrongly. Please check jni library file to confirm there is exact symbol link in library file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM