android - 对于显示 UnsatisfiedLinkError : No implementation found for . stringFromJNI2() 的第二个 native 函数

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

任何人都可以告诉我这段代码有什么问题吗?这与堆栈跟踪崩溃了

    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)

我的 Activity 类代码是

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__)

当我执行应用程序时,它会关闭,并显示第二个函数的链接错误,但如果我仅使用一种 native 方法,则工作正常。

最佳答案

Extern“C”仅计算其后定义的第一个函数。如果您向其中添加大括号并将函数放入其中,如下所示,它将起作用

extern "C" { 
    /* Your JNI functions */ 
}

关于android - 对于显示 UnsatisfiedLinkError : No implementation found for . stringFromJNI2() 的第二个 native 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773688/

相关文章:

php - 在 PHP 和 Android 中将时间戳转换为可读时间

C++ 内存泄漏,Valgrind 消息误导?

c - fprintf Android NDK

android - 是什么导致我的 C++ JNI 崩溃?

安卓文件下载慢

android - 我的应用被 Google Play 拒绝 "APK REQUIRES PROMINENT DISCLOSURE"

android - 如何从 Assets 文件夹加载 webview 中的 pdf 文件

c++ - "stable_sort()ing"C++ 中的 STL <列表>

c++ - 没有错误,但没有输出

android - 奇怪的 "stutter"在不同的 android 设备上的 box2D