Android NDK help?

mwin

2[H]4U
Joined
Jun 24, 2004
Messages
3,377
First off, I'm not a Java guy, by any stretch of the imagination. I had an intro class back in college, probably 8 or 9 years ago. I'm way more comfortable with C/C++/C#. I'm trying to figure out the Android NDK so we can port over some existing code (static libraries written in C) to run on mobile devices. Currently, I'm just trying to get Hello World working and get a feel for the development environment.

From ndkhello.c:
Code:
#include <string.h>
#include <jni.h>

jstring Java_helloWorld_namespace_HelloWorld_HelloWorldActivity_hello(JNIEnv
* env, jobject this)
{
return (*env)->NewStringUTF(env, "Hello from native code!");
}

From HelloWorldActivity:
Code:
package helloWorld.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends Activity {
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("HelloWorld");
}
 
// declare the native code function - must match ndkhello.c
private native String hello();
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
String fromNative;
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);

fromNative = hello();
tv.setText(fromNative);
setContentView(tv);
}
}

I get an UnsatisfiedLinkError out of the call to fromNative = hello(); I'm suspicious that I miss-named my function in ndkhello.c. Can anyone verify this? Help me to understand the naming convention? Any help is appreciated.
 
i've not delved into jni but from my reading, you can run

javah -jni fully.qualified.ClassName

and it generates a C header file with the proper signature for you. i'm assuming you have to already have compiled the java file and make sure you run javah with the proper classpath

if i were to guess though, i think you have an extra helloworld in your signature. try deleting the red
Code:
jstring Java_helloWorld_namespace_[color=red]HelloWorld_[/color]HelloWorldActivity_hello(JNIEnv
* env, jobject this)
{...}
 
This shit is so frustrating. Why is it so hard to incorporate native files into your projects? What is with these crazy-ass function names that you have to make up? Why can't it just work? I'm missing Visual Studio. Damn InvocationTargetExceptions.
 
Back
Top