If an Android game or app requires performance, a close relationship with the OS, and/or a tight interface with the device’s hardware then the Android’s Native Development Kit (NDK) is required for development of that app/game.
A good way to begin learning the NDK is to create (setup) a “Hello World” program. This tutorial (made for beginners of Android development) will cover the steps required to setup a native development environment. The first steps for creating a great Android game or app.
Before beginning this tutorial, verify the computer has been setup and configured properly for native development. (Setting up a development environment for Android App/Game Development on a Windows Platform)
After creating a new work space, the next step is to setup the Eclipse’s Preferences. Eclipse’s Preferences do not automatically transfer from work space to work space. So, when a new work space is created for a project the Preferences will need to be imported from another work space or reconfigured.
To configure Eclipse’s Preferences, go to Eclipse’s File menu and select Window-Preferences.
A fully native application is compiled as a static object file (a *.so file). By default the compiler will only compile the *.so file. However, to run the application an Android Application Package (APK or a *.apk file) is required to manage the application. This is similar to how a Windows executable will call and manage a dynamic link library (DLL or a *.dll file).
To setup the compiler to compile the *.apk file, uncheck the following check-box within the Preferences dialog, under the Preferences tree Android-Build and then click the Apply button, to save the changes.
Within the Preferences dialog, in the Preferences tree select Android-NDK and type the location of the NDK\build directory or browse for it. Once completed, select the Apply button.
At this point there are other items that can be configured within the Preferences dialog, such as editor preferences (using spaces instead of tabs and auto bracket placements), and intellisense preferences. However, set these preferences with caution. Some settings can reconfigure how Eclipse interacts and compile the Android code. Once completed setting up the additional preferences, select the OK button to save the changes and exit the dialog box.
To set up the project, go to the file-menu and select File-New-Project.
Within the dialog, select Android-Android Application Project; and then select the Next button.
After clicking the Next button, the “New Android Application” dialog will pop up. Fill in the text-box “Application Name” with “Hello World – NDK”. The project and package names should automatically fill in with default names. However, if desired these text-boxes can also be modified, by typing different names.
The minimum, target and compiled SDKs refer to the SDKs needed to target a
range of Android OSs and devices the application will run on.
The website, AppBrain is a good reference to figure out the desired entries for these combo boxes.
With the entries configured below, the application should be able to run on
87.6% of the estimated Android devices being used (at the time of this
On the next dialog, leave the default configured project settings, as in the
screen capture below. The check-boxes are used to indicate that the app is going
to use a launcher icon as well as an activity. ”
An activity is a
single, focused thing that the user can do. Almost all activities interact with
the user, so
” for example ”
the Activity class takes
care of creating a window for you…
Android development documentation
The next step within the wizard is configuring the launcher icon. Below, is the default settings using the default launcher icon.
After configuring the launcher icon, it is time to create an Activity. Within the dialog select “Blank Activity with Fragment”. ”
A Fragment represents a behavior or a portion of user interface in an Activity.
” A Fragment ”
has its own life cycle, receives its own input events
” separate but within an Activity – “
sort of like a sub-activity
Android development documentation
The final step of the wizard is to name the Android Activity and the Fragment. Below is the default configured settings.
After configuring a new project, then it is time to open an Eclipse perspective that will allow for simple Android development. By default Eclipse will display an introductory tab, as seen below.
If the tab is up then simply click on the tab’s close icon, next to the text ‘Welcome’. By closing the introduction tab Eclipse will bring up the default Java perspective.
At the bottom of the perspective there is a tab with the text “Console”. If the tab is not selected then please select the tab. If the console is displaying the error message “… AndroidManifest.xml:11: error: Error: No resource found that matches the given name (at ‘icon’ with value ‘@drawable/ic_launcher’).
” then the manifest is not configured to look in the correct set of directories for the launcher icon. The manifest is setup to look in the drawable directories.
The error is normal, given the app is setup to run on many different Android devices. In earlier releases of the Android SDK the launcher icons were stored in the drawable directories, but has since moved into the mipmap folders with the newer releases of the SDK.
To begin to correct the error, double click on AndroidManifest.xml
within the Package Explorer. The manifest will appear within the editor to the right of the Package Explorer. Within the editor there is a set of tabs below the newly opened manifest. Within that set of tabs select the tab labeled AndroidManifest.xml
To correct the issue, change the highlighted line within the image above to android:icon=”@mipmap/ic_launcher”
, as seen below.
The next step is to configure eclipse to build the C/C++ source code into a static object (*.so) file. To perform this, do a right mouse click on the project name within the Package Explorer and then select “Android Tools-Add Native Support..
A pop-up dialog will appear requesting to name the static object file.
After adding native support, note the perspective of the user interface (UI) should change to the C/C++ perspective, (if the previously opened perspective was the Java perspective or another perspective).
Within the next few steps, the Android manifest XML file will need to be changed; so the application, during compilation-time will load the library that is being created through this tutorial. If the project’s Android manifest’s source-code is not open then open AndroidManifest.xml within the source-code editor.
The first step is to remove the title bar from the application. Within the tag application
, change the line android:theme=”@style/AppTheme”
Next, rename the activity so the application will link the compiled binary *.so file into the APK. Change the android:name
within the activity
tag to android:name=”android.app.NativeActivity”
Lastly, add the required tags needed by the application to call the correct binary *.so file. Add a meta-data
tag within the activity
tag. Within the meta-data tag add the elements android:name=”android.app.lib_name”
Since the application is built from a pure native C/C++ project, the configuration settings for building a Java application needs to be removed from the project.
Within the Project Explorer perform a right mouse on the project’s name and select the menu option Properties
Within the properties dialog select Java Build Path
from the properties tree on the left of the dialog; and then within the Source folders on build path:
group-box select the source folder (HelloWorld-NDK/src
) and remove it. After removing the source folder select the OK
After configuring Eclipse to not build the Java source-code, the source-code directory can be removed from the project’s folder, as seen below.
Other files that should be removed from the project are HelloWorld_NDK.cpp
within the jni
within the res/layout folders.
At this point the configuration and setup of a fully native development Eclipse workspace has been completed. It is now time to write the first lines of C/C++ source-code. (Yea!)
To begin, perform a right mouse click on the jni
folder and select from the drop down menu New-Source File
Within the New Source File dialog, create the file Main.cpp. This source-file will compile into the main entry point for the application’s run-time pointer.
The newly created file will open in the source-code editor, right of the Package Explorer tree. Within the editor either type or copy the source-code, below.
// Predefined Android API library
// Loop 100 times
} // End of for(unsigned int ui = 0; ui < 100; ++ui)
} // End of void android_main(android_app *pstDroidAPI)
In short the above source-code during run-time shall print to the logger (LogCat) “Hello World!”, 100 times.
For novice software developers, it is not necessary to completely understand the source-code above to complete this tutorial. However, please read the comments (comments start with // in C/C++) for a brief explanation.
The next step, is outlining to the compiler what libraries to import and what source-code to compile through a make file.
By default the Android configuration wizard that was called earlier to setup the project already created a make file. Within the jni
folder double click on the file Android.mk; and change its contents to as follows.
|LOCAL_PATH := $(call my-dir)
LS_CPP=$(subst $(1)/,,$(wildcard $(1)/*.cpp))
LOCAL_MODULE := HelloWorld_NDK
$(call import-module, android/native_app_glue)
Again, for rookie programmers, it is not necessary to completely understand the compiler instructions above to complete this tutorial. However, in short, the make file is instructing the compiler to compile the code located within the jni folder.
The instructions are also detailing to the compiler to import the android_native_app_glue
library during the build; and to expect the run-time (load or dynamic link) libraries landroid
There is one more make file that needs to be created. The Application.mk
file details to the compiler the targeted machine platforms to build the binary for.
To create the file perform a right mouse click on the jni
folder and select from the drop down menu New-File.
Within the file, type or copy the following.
|APP_ABI := armeabi armeabi-v7a x86|
The line is instructing the compiler to build binaries that are targeted for systems armeabi, armeabi-v7a
Alright! Congratulations! Everything is setup and the source-code is ready to be built.
To build the source-code into binaries so an Android device can run the buitl application, select from the file menu Project-Build All.
To run our program through an Android emulator, change IDE’s perspective to the Android perspective, by selecting from the file menu Window-Perspective-Open Perspective…-Other….
Within the Open Perspective pop-up dialog select the Android perspective.
Assuming this is the first time running an Android app, then an Android emulator needs to be created through the AVD manager.
To begin creating an Android Virtual Device (AVD), select the AVD manager icon from the tool bar, as seen below
Within the pop-up Android Virtual Device (AVD) Manager dialog, select the tab Device Definitions.
Within the Device Definitions
tab, select a prebuilt device. (The Nexus S, is a good one for this tutorial.)
Within the next pop-up dialog, fill in the dialog with similar settings as detailed below and then select the OK button.
Now to run the AVD select the Start… button after selecting the AVD within Android Virtual Device tab.
Another pop-up will appear, select the Launch button.
After the emulator appears, close the AVD manager in order to port the built application to the emulator. After the AVD manager has closed drag and drop the APK file into the emulator from the Package Explorer as seen, below.
If successful then the following pop-up box shall appear.
After the installation has completed then find and run the application within the emulator. After running the application for a few minutes (it may appear to “hang” on a black screen – that is okay), close the emulator.
Open the LogCat tab at the bottom of Eclipse and select Info from the combo-box, as seen below.
Scroll through the list and view the application’s prints! Awesome!
Congratulations! You have created a fully native Android application!
Now, what did we really do? We created the initial work space that is required for any fully native Android project. The steps in this tutorial are required in order to create any C/C++ game or application for an Android device. You will use these steps, if your project requires C/C++ source-code, performance, a close relationship to the hardware and the associate drivers, and/or the Android OS.
Thanks for reading! Please leave your comments and questions, below.