对于显示UnsatisfiedLinkError的第二个本机函数:找不到.stringFromJNI2()

时间:2017-06-27 06:36:44

标签: android c++ android-ndk java-native-interface

任何机构都可以告诉我,请问这段代码有什么问题。这与stacktrace崩溃

    06-27 12:02:12.842 20619-20619/? E/art: No implementation found for java.lang.String com.rana.nativesupport.MainActivity.stringFromJNI2() (tried Java_com_rana_nativesupport_MainActivity_stringFromJNI2 and Java_com_rana_nativesupport_MainActivity_stringFromJNI2__)
06-27 12:02:12.843 20619-20619/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.rana.nativesupport, PID: 20619
                                                   java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.rana.nativesupport.MainActivity.stringFromJNI2() (tried Java_com_rana_nativesupport_MainActivity_stringFromJNI2 and Java_com_rana_nativesupport_MainActivity_stringFromJNI2__)
                                                       at com.rana.nativesupport.MainActivity.stringFromJNI2(Native Method)
                                                       at com.rana.nativesupport.MainActivity.onCreate(MainActivity.java:30)
                                                       at android.app.Activity.performCreate(Activity.java:6672)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

我的活动类代码是

package com.rana.nativesupport;

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

public class MainActivity extends AppCompatActivity {

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

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


    @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(String.valueOf(stringFromJNI()));
        tv.setText(String.valueOf(stringFromJNI2()));
    }

//    public native String intFromJNI();
}

和native-lib.cpp就在这里

#include <jni.h>
#include <string>

extern "C"

JNIEXPORT jstring JNICALL Java_com_rana_nativesupport_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject obj/* this */) {
    std::string hello = "C++ is goddamn serious";
    return env->NewStringUTF(hello.c_str());
};

JNIEXPORT jstring JNICALL Java_com_rana_nativesupport_MainActivity_stringFromJNI2(
        JNIEnv *env,
        jobject obj/* this */) {
    std::string hello = "C++ is goddamn serious";
    return env->NewStringUTF(hello.c_str());
};

尝试了几乎所有事情,但仍然得到了例外 UnsatisfiedLinkError:找不到java.lang.String的实现com.rana.nativesupport.MainActivity.stringFromJNI2()(尝试了Java_com_rana_nativesupport_MainActivity_stringFromJNI2和Java_com_rana_nativesupport_MainActivity_stringFromJNI2 __)

当我执行应用程序时,它会关闭第二个函数的不满意链接错误,但如果我只使用一个本机方法则工作正常。

1 个答案:

答案 0 :(得分:2)

外部“C”仅计算在其后定义的第一个函数。如果你为它添加大括号并将你的函数放在那里,如下所示,它将起作用

{{1}}