java.lang.UnsatisfiedLinkError:找不到实现?

时间:2018-03-09 07:33:51

标签: java android c++ android-ndk

我正在尝试学习在android studio中使用本机代码。但是,当我尝试调用返回整数值的本机函数并将其传递给我的java类时,我遇到错误。我收到这个错误 java.lang.UnsatisfiedLinkError:找不到int com.mobi.MainActivity.randomintFromJNI()的实现(尝试过Java_com_mobi_MainActivity_randomintFromJNI) 我能做错什么?

package com.mobi;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Example of a call to a native method
            TextView tv = (TextView) findViewById(R.id.sample_text);

            tv.setText(Integer.toString(randomintFromJNI()));
        }

        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
        public native static int randomintFromJNI();

    }

c++
#include <jni.h>
#include <string>
#include <cstdlib>
using namespace std;

extern "C"
JNIEXPORT jstring

JNICALL
Java_com_mobi_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    string hello = "Hello from C++";

    return env->NewStringUTF(hello.c_str());
}


JNIEXPORT jint

JNICALL
Java_com_mobi_MainActivity_randomintFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    int val = rand()*100;

    return val;
}

1 个答案:

答案 0 :(得分:1)

你在Java_com_mobi_MainActivity_randomintFromJNI上错过了一个extern“C”