You're in
Iker Hurtado's pro blog
Developer | Entrepreneur | Investor
Software engineer (entrepreneur and investor at times). These days doing performant frontend and graphics on the web platform at Barcelona Supercomputing Center

Creating a new Android project with NDK support

22 Jan 2015   |   iker hurtado  
Share on Twitter Share on Google+ Share on Facebook
With this entry I want to write down the quirks in the process of creating an Android project with NDK support on Eclipse + ADT. Note that my project is a purely native application, that is, a project without Java code.

I write the steps to create a purely native app project on Eclipse + ADT (23.0.4 version) with Android NDK installed(revision 10d):

1. Before starting, ensure that the Eclipse IDE is aware of the location of the NDK. This configuration is hide in Window > Preferences > Android > NDK. If you want to build it from the command line at some point, update your PATH environment variable with the location of the directory that contains the NDK. For example, in bash:

export PATH=$PATH:/NDK_dir/

2. Create the project as usual.

3. Delete all of the default-created activities and layouts; in a purely native app these aren't necessary.

4. Right-click the project icon and go to Android Tools. Select Add Native Support.... The name in the text field will be the native code library which will be generated by the build process, in this case: nativedemo

5. A purely native project requires a special Manifest setup inside the application tag, like this:

<application android:hasCode="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="android.app.NativeActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <meta-data android:name="android.app.lib_name" android:value="nativedemo"/> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

I comment the specific configurations for this case:

- application attribute android:hasCode="false": When the value is "false", the system does not try to load any application code when launching components.

- The activity name to android.app.NativeActivity: NativeActivity is a helper class in order to write a completely native activity. It's a hidden but common class that handles the communication between the Android framework and the native code.

- The android:value attribute of the meta-data tag specifies the name of the shared library containing the entry point to the application:

<meta-data android:name="android.app.lib_name" android:value="nativedemo"/>

4. Build file setup. The previous Add Native Support menu operation has generated the Android build file (Android.mk), but we want to add some configuration lines in order to use key external libraries (More about NDK build system and native libraries):

// Android’s existing libraries linked in dynamically
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2  

// static NDK library to enable us to write a pure native app
LOCAL_STATIC_LIBRARIES := android_native_app_glue 

// This tells the build system to import the static library into the app.
$(call import-module, android/native_app_glue)  

The final Android.mk file will look like this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := nativedemo
LOCAL_SRC_FILES := NativeDemo.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
LOCAL_STATIC_LIBRARIES := android_native_app_glue


$(call import-module,android/native_app_glue)

These is more information in official NDK documentation.
This article can be useful too: Using Eclipse for Android C/C++ Development | Android blog

I have had problems with source files with .c extensions in Elipse. The IDE checker doesn't see the headers content properly when it parses source files with c extension. Maybe it has to do with __cplusplus preprocessor var...but I'm not sure. I'm using .cpp extension in my files for now.