• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps NDK Newbie Getting Started

paulscode

Member
Mar 18, 2010
86
14
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 ;) ). 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:
#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:
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.
 
Thanks for the tip. I'll use your suggestion here to figure out where the crash is occurring.

Ultimately, I will be using Sequoyah for debugging through Eclipse (allowing me to stop at break points in the code). But before I try to figure any of that out, I'll see if I can get this simple Hello World app to work first so I'm not trying to figure out two things at once.
 
Upvote 0
Ok, I altered the HelloAndroid.java to add in some logging:
Code:
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:
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.
 
Upvote 0
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:
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" );
    }
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones