1

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!

dorien
  • 5,265
  • 10
  • 57
  • 116
  • Just some extra debugging I already tried: Without the generate() function, the app works fine. If I change the LoadLibrary file to another name, I get an error, so I guess he loads the library fine. I am guessing I made a package/class/naming error somewhere as I am not used to java... been bending my head over it... – dorien Jul 02 '12 at 15:04
  • Have you tried putting the System.loadLibrary at the top of the class declaration? – Force Jul 02 '12 at 15:21
  • It looks like you may have be having issues with the differences between the jni C api vs the C++ api. Perhaps take a step back to the hello-jni C example, test it, then do only one of customizing the package name or translating to C++, then do the other only once that works. – Chris Stratton Jul 02 '12 at 15:52

3 Answers3

8

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 !");
}
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
0

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

Community
  • 1
  • 1
0gravity
  • 2,682
  • 4
  • 24
  • 33
0

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!");

    }
}
Emiswelt
  • 3,909
  • 1
  • 38
  • 56
dorien
  • 5,265
  • 10
  • 57
  • 116