Sandeep Rana
Sandeep Rana

Reputation: 3441

For second native function showing UnsatisfiedLinkError: No implementation found for .stringFromJNI2()

Can any body tell me please whats wrong with this code. This is crashing with 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)

My Activity class code is

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();
}

and native-lib.cpp is here

#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());
};

Tried almost everything but still getting Exception 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__)

When i execute the app it closes saying unsatisfied link error for the second function but working fine if i am using only one native method.

Upvotes: 2

Views: 907

Answers (1)

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

Extern "C" only counts for the first function that is defined after it. If you add braces to it and put your functions in there, like below, it will work

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

Upvotes: 3

Related Questions