Building an Android NDK application in Eclipse

The previous recipe discusses how to build an Android NDK application in the command line. This recipe demonstrates how to do it in the Eclipse IDE.

Getting ready

Add NDK Preferences. Start Eclipse, then click on Window | Preferences. In the Preferences window, select NDK under Android. Click on Browse and select the NDK root folder. Click on OK.

Getting ready

How to do it…

The following steps create an NDK project using Eclipse:

  1. Create an Android application named HelloNDKEclipse. Set the package name as cookbook.chapter3. Create an activity named HelloNDKEclipseActivity. Please refer to the Loading native libraries and registering native methods recipe of Chapter 2, Java Native Interface, if you want more detailed instructions.
  2. Right-click on the project HelloNDKEclipse, select Android Tools | Add Native Support. A window similar to the following screenshot will appear. Click on Finish to dismiss it:
    How to do it…

    This will add a jni folder with two files (HelloNDKEclipse.cpp and Android.mk) inside, and switch Eclipse to C/C++ perspective.

  3. Add the following content to HelloNDKEclipse.cpp:
    #include <jni.h>
    
    jstring getString(JNIEnv* env) {
      return env->NewStringUTF("Hello NDK");
    }
    
    extern "C" {
      JNIEXPORT jstring JNICALL Java_cookbook_chapter3_HelloNDKEclipseActivity_getString(JNIEnv* env, jobject o){
        return getString(env);
      }
    }
  4. Change the content of HelloNDKEclipseActivity.java to below.
    package cookbook.chapter3;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.widget.TextView;
    
    public class HelloNDKEclipseActivity extends Activity {
      @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           TextView tv = new TextView(this);
           tv.setTextSize(30);
           tv.setText(getString());
           this.setContentView(tv);
       }
       public native String getString();
       static {
           System.loadLibrary("HelloNDKEclipse");
       }
    }
  5. Right-click on HelloNDKEclipse project, and select Build Project. This will build the native library for us.
  6. Right-click on the project, go to Run As, and select Android Application. The phone screen will display something similar to the following screenshot:
    How to do it…

How it works...

This recipe discusses building the Android NDK application at Eclipse.

We have been using C in all previous recipes. Starting from this recipe, we'll be writing our code in C++.

By default, Android provides minimal C++ support. There's no Run-time Type Information (RTTI) and C++ exceptions support, and even the C++ standard library support, is partial. The following is a list of the C++ headers supported by Android NDK by default:

cassert, cctype, cerrno, cfloat, climits, cmath, csetjmp, csignal, cstddef, cstdint, cstdio, cstdlib, cstring, ctime, cwchar, new, stl_pair.h, typeinfo, utility

It is possible to add more C++ support by using different C++ libraries. NDK comes with the gabi++, stlport, and gnustl C++ libraries, besides the system default one.

In our sample code, we used an external "C" to wrap the C++ method. This is to avoid C++ mangling of the JNI function names. C++ name mangling could change the function names to include type information about parameters, whether the function is virtual or not, and so on. While this enables C++ to link overloaded functions, it breaks the JNI function discovery mechanism.

We can also use the explicit function registration method covered in the Loading native libraries and registering native methods recipe of Chapter 2, Java Native Interface, to get rid of the wrapping.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset