NDK Newbie Getting Started

Put problems with the Android NDK in this forum.

NDK Newbie Getting Started

Postby paulscode » Thu Nov 11, 2010 4:09 pm

I have a (possibly overly) ambitious goal of writing an N64 emulator to sell on the Android market. I have a great deal of current experience programming in Java, and past experience in C++ (I'm a bit rusty, but its like riding a bike ... I hope :P ). I have a little bit of experience writing for the Android with the Android SDK and ADT plug-in for Eclipse ... I've written 3 simple starter apps, my most recent being an animated-texture cube app.

Anyway, my next step is to learn how to compile native C++ code, access it in the Java portion, debug it, etc. I've installed and set up the NDK and Sequoyah in Eclipse, and I'm trying to write a basic "Hello World" app that generates a string in native code and displays it in a basic TextView. The project seems to be set up correctly, it has generated the jni folder with a .cpp and a .mk file inside. I edited the HelloAndroid.cpp File as follows:

Code: Select all
#include <string.h>
#include <jni.h>

extern "C"
{
   jstring Java_paulscode_android_helloandroid_HelloAndroid_sayHi( JNIEnv* env, jobject obj )
   {
      return env->NewStringUTF( "Howdy, this is JNI!" );
   }
}


Then I edited the HelloAndroid.java File as follows:

Code: Select all
package paulscode.android.helloandroid;

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

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


The project compiles fine, but it fails to run in the AVD, generating the message "Sorry! The application Hello, Android (process paulscode.android.helloandroid) has stopped unexpectedly. Please try again."

Is there anything obviously wrong with my code, or are there some Eclipse or AVD settings that I need to check? Let me know if you require any further information about this Hello Android project to help me solve the problem.
paulscode
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Thu Nov 11, 2010 3:57 pm

Top

Re: NDK Newbie Getting Started

Postby paulscode » Thu Nov 11, 2010 5:45 pm

Ok, I altered the HelloAndroid.java to add in some logging:

Code: Select all
package paulscode.android.helloandroid;

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

public class HelloAndroid extends Activity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        Log.e( "HelloAndroid.onCreate()", "Before super()" );       
        super.onCreate( savedInstanceState );
        Log.e( "HelloAndroid.onCreate()", "Before new TextView" );       
        TextView tv = new TextView( this );
        Log.e( "HelloAndroid.onCreate()", "Before call to native sayHi()" );
        String message = sayHi();
        Log.e( "HelloAndroid.onCreate()", "sayHi() SUCCESS!  Before setText()" );               
        tv.setText( message );
        Log.e( "HelloAndroid.onCreate()", "Before setContentView()" );               
        setContentView( tv );
        Log.e( "HelloAndroid.onCreate()", "COMPLETE!" );               
    }
   
    public native String sayHi();   
}


Then I ran DDMS, and got the following output from logcat:

Code: Select all
I/ActivityManager(   60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=paulscode.android.helloandroid/.HelloAndroid }
I/ActivityManager(   60): Start proc paulscode.android.helloandroid for activity paulscode.android.helloandroid/.HelloAndroid: pid=293 uid=10032 gids={1015}
E/HelloAndroid.onCreate()(  293): Before super()
E/HelloAndroid.onCreate()(  293): Before new TextView
E/HelloAndroid.onCreate()(  293): Before call to native sayHi()
W/dalvikvm(  293): No implementation found for native Lpaulscode/android/helloandroid/HelloAndroid;.sayHi ()Ljava/lang/String;
D/AndroidRuntime(  293): Shutting down VM
W/dalvikvm(  293): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  293): FATAL EXCEPTION: main
E/AndroidRuntime(  293): java.lang.UnsatisfiedLinkError: sayHi
E/AndroidRuntime(  293):     at paulscode.android.helloandroid.HelloAndroid.sayHi(Native Method)
E/AndroidRuntime(  293):     at paulscode.android.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:18)
E/AndroidRuntime(  293):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  293):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  293):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  293):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  293):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  293):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  293):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  293):     at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  293):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  293):     at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  293):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  293):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  293):     at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(   60):   Force finishing activity paulscode.android.helloandroid/.HelloAndroid
W/ActivityManager(   60): Activity pause timeout for HistoryRecord{44fcde90 paulscode.android.helloandroid/.HelloAndroid}
W/ActivityManager(   60): Activity destroy timeout for HistoryRecord{44fcde90 paulscode.android.helloandroid/.HelloAndroid}
I/Process (  293): Sending signal. PID: 293 SIG: 9
I/ActivityManager(   60): Process paulscode.android.helloandroid (pid 293) has died.
W/InputManagerService(   60): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44f63478


Obviously the relevant message here is no implementation for the native method. The way it is referenced there does look a bit odd "paulscode/android/helloandroid/HelloAndroid;.sayHi ()", but I don't really have a working example to compare to see if that is a normal syntax. The only other causes I can think of are either a typo in my code somewhere or the native library somehow not being included in the APK or otherwise not being transferred to the AVD at run time.

Hopefully this might help someone with more experience figure out what is wrong.
paulscode
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Thu Nov 11, 2010 3:57 pm

--SOLVED-- NDK Newbie Getting Started

Postby paulscode » Thu Nov 11, 2010 6:03 pm

Haha, so simple (I figured it would be). I totally forgot to load the native library from the Java portion (I even looked at the examples and still missed it). For reference, the working HelloAndroid.cpp looks like this:

Code: Select all
package paulscode.android.helloandroid;

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

public class HelloAndroid extends Activity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        TextView tv = new TextView( this );
        tv.setText( sayHi() );
        setContentView( tv );
    }
   
    public native String sayHi();
   
    static
    {
        System.loadLibrary( "HelloAndroid" );
    }
}
paulscode
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Thu Nov 11, 2010 3:57 pm

Re: NDK Newbie Getting Started

Postby aashishvijayvirgiya » Wed Jun 01, 2011 10:26 am

new to android.
working on android jni(ndk).done some sample projects based on prebuilt library, having same problems
my androi.mk is

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-jni
LOCAL_SRC_FILE:=hello-jni.c
LOCAL_SHARED_LIBRARY := ndk
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ndk
NDK_MODULE_PATH := $(LOCAL_PATH)
LOCAL_SRC_FILES := libndk.so
include $(PREBUILT_SHARED_LIBRARY)

hello-jni.c calls functions from libndk.so(ARM lib.)

this project run successfully several times but when i restarted the eclipse and again run the project from eclipse.
emulator load the project. but having the problem calling funtions from shared library. it stopped unexpectedly. when i debug in logcat view it show unsatisfied linker error.
please help me out this problem.
attached the project also

my email address is ashish_vijayvirgiya@yahoo.com
if you want some additional information then you tell me.

thanks.
aashishvijayvirgiya
Freshman
Freshman
 
Posts: 8
Joined: Tue May 31, 2011 8:30 am

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 3 guests