dorien
dorien

Reputation: 5397

UnsatisfiedLinkError for native cpp function in Android app (ndk)

I am trying to run an easy Android ndk app in cpp, but I get UnsatisfiedLink Error for the Generate() function.

Any help would be appreciated. I am quite fluent in c++, but my java is a little bit rusty. I have been trying a lot of tips from the web concerning naming, but so far no luck. Here are my files:

native.cpp:

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

jstring Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, jobject thiz){
    return env->NewStringUTF("Hello from JNI !");
}

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native
LOCAL_SRC_FILES := native.cpp

include $(BUILD_SHARED_LIBRARY)

I compile this with ndk-build and all goes well, it provides me with a libnative.so, that is located in the project directory. I use eclipse for the rest.

OptimuseAppActivity.java:

package com.optimuse.app;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;

public class OptimuseAppActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        tv.setText( generate() );
        setContentView(tv);
    }

    public native String generate();

    static {
        System.loadLibrary("native");
    }
}

And the automatically generated AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.optimuse.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:hasCode="true">
        <activity
            android:name="com.optimuse.app.OptimuseAppActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you for any suggestions, been searching for a few hours now!

Upvotes: 1

Views: 1970

Answers (3)

dorien
dorien

Reputation: 5397

Excellent! It works like this:

extern "C" {
    JNIEXPORT jstring JNICALL Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, jobject thiz) {

        return env->NewStringUTF("Hello from JNI cpp!");
        //return (*env)->NewStringUTF(env, "Hello from JNI cpp!");

    }
}

Upvotes: 0

Chris Stratton
Chris Stratton

Reputation: 40357

You are mostly likely running into C++ name mangling. For example, here is what objdump -T on the library gives when I build hello-jni.c:

00000c28 g DF .text 0000002c Java_com_example_hellojni_HelloJni_stringFromJNI

And here's what I get if I translate to C++ in the way you did:

00000c94 g DF .text 00000024 _Z48Java_com_example_hellojni_HelloJni_stringFromJNIP7_JNIEnvP8_jobject

To prevent the mangling and make them visible to jni, declare your native functions within an extern "C" {} block, ie

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

extern "C" {
    jstring Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, 
                                                               jobject thiz);
}

jstring Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, 
                                                           jobject thiz){
    return env->NewStringUTF("Hello from JNI !");
}

Upvotes: 8

0gravity
0gravity

Reputation: 2762

You have probably tried these, but anyways it looked very similar to your question and I thought it might help:

How to resolve the java.lang.UnsatisfiedLinkError in NDK in Android?

android ndk UnsatisfiedLinkError when using a prebuilt shared library

Upvotes: 0

Related Questions