Android Toy

Hello World with C/C++ using Eclipse for Android (Full) Native Development

All, Game & Software Engineering, Game Development

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.

Open preferences dialog

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.

Unselect skin packaging

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.

Pointing to NDK

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.

Open project wizard

Within the dialog, select Android-Android Application Project; and then select the Next button.

New project dialog

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

Name app

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


Default config

The next step within the wizard is configuring the launcher icon. Below, is the default settings using the default launcher icon.

Default config launch 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


Blank activity with fragment

The final step of the wizard is to name the Android Activity and the Fragment. Below is the default configured settings.

Name activity

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.

No perspective

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.

Close welcome tab

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.
Issue report

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


Open Android Manifest

To correct the issue, change the highlighted line within the image above to android:icon=”@mipmap/ic_launcher”

, as seen below.

Fix the issue

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..

Add native support menu

A pop-up dialog will appear requesting to name the static object file.

Add native support dialog

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”

to android:them=”@android:style/Them.NoTitleBar.Fullscreen”


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=””


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=””

and android:value=”HelloWorld_NDK”


The final results should be something similar to the image, below.
Android manifest change

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

from the menu.

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


Remove java build path source

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.

Delete source directory

Other files that should be removed from the project are HelloWorld_NDK.cpp

within the jni

folder, activity_main.xml

and fragment_main.xml

within the res/layout folders.

Delete layout C++

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

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.

New source file

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.

// Main.cpp

// Predefined Android API library
#include <android_native_app_glue.h> // Used for defining the interface between
// the OS and this app

#include <android/log.h> // Used for logging information to LogCat

// The main entry point for this Android application. 2016/07/06 DK

void android_main(
android_app *pstDroidAPI) // The pointer’s allocated memory helps manage the
// interface between the Android OS and this app
// The interface is not used for this application.

// The function call prevents the linker (during compilation of
// the source-code) from stripping out the native glue library


// Loop 100 times
for(unsigned int ui = 0; ui < 100; ++ui)
// Print to LogCat “Hello World!”
ANDROID_LOG_INFO, // Type of warning/error message
“Raging Gazebo”, // The print statement’s tag
“Hello World!”); // The message

} // 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; and change its contents to as follows.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LS_CPP=$(subst $(1)/,,$(wildcard $(1)/*.cpp))

LOCAL_LDLIBS := -landroid -llog
LOCAL_STATIC_LIBRARIES := android_native_app_glue


$(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

and log.

There is one more make file that needs to be created. The

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.

New file

Within the File pop-up dialog create the file


New application make 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

and x86


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.

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….

Open perspective

Within the Open Perspective pop-up dialog select the Android perspective.

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.

Select device defs

Within the Device Definitions

tab, select a prebuilt device. (The Nexus S, is a good one for this tutorial.)

Create AVD Nexus S

Within the next pop-up dialog, fill in the dialog with similar settings as detailed below and then select the OK button.

Create AVD Nexus S

Now to run the AVD select the Start… button after selecting the AVD within Android Virtual Device tab.

Start AVD

Another pop-up will appear, select the Launch button.

Launch emulator

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.

Drag and drop APK

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.

Open log cat and select info

Scroll through the list and view the application’s prints! Awesome!

Log cat results

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.

Please, keep your comments family friendly and respectful of each other and the author.