Android Studio With NDK OpenGL ES Sample
Dec 7, 2014
3 minute read

Android Studio 1.0 RC4 was just recently pushed, so I decided now was the time to start refreshing my projects and ideas for Android development.

To get started, I wanted to see how difficult it would be to compile NDK samples with the new Android Studio IDE. I always found the process to be super clunky with Eclipse and had always resorted to doing everything by hand via the command line. It was obviously not ideal and was not a fun way to develop a game.

This article assumes:

  • that you have Android Studio upgraded to 1.0 RC4 (current version as of publication date)
  • that you have the Android NDK installed
  • that you have the Android SDK installed and updated and that Android Studio can find it

The hello-gl2 Sample

If you download the recent Android NDK (version r10d as of this writing), you should be able to find the hello-gl2 sample in the android-ndk-r10d\samples directory. This sample still uses Java for the activities but it implements the OpenGL ES functionality in C++ and exposes the C++ code through a class called GL2JNILib.

In order to test this with Android Studio, I created a new project, copied the code over, changed the AndroidManifest.xml and updated the Gradle Scripts. Both this page and this page were helpful in figuring out the right steps.

Step 1: Create a New Project

When you start Android Studio, select the New Project option from the wizard. I created a project called NDKSample and left all of the settings at the default in the wizard except for selecting no activity instead of Blank Activity.

Step 2: Copy the Code

I put the hello-gl2 code into the new project by doing the following:

  • delete the folder NDKSample\app\src\main\java\com
  • copy hello-gl2\src\com to NDKSample\app\src\main\java
  • copy hello-gl2\jni\gl_code.cpp to NDKSample\app\src\main\jni
  • create file called empty.cpp in NDKSample\app\src\main\jni

The empty.cpp file is literally an empty code file and is only there because there currently is a bug when compiling a NDK project with only one file. Android Studio will automatically make the equivalent of the file, so all that is is needed is the C++ code.

The Java code copied in the com directory contains the Java activity and view classes as well as the class the wraps the JNI code.

Step 3: Modify the AndroidManifest.xml File

You can edit this file by selecting it in the Projects side panel in Android Studio under app > manifests > AndroidMainifest.xml. Replace the content of this file with the following:

<manifest xmlns:android=""
	<activity android:name="GL2JNIActivity"
			<action android:name="android.intent.action.MAIN" />
			<category android:name="android.intent.category.LAUNCHER" />

What this does is change the package name to so that none of the sample code needs to be edited and it changes the activity name so that it knows what activity to launch at startup.

Step 4: Update the Gradle Scripts

The last step is to change the Gradle build scripts. You need to define your path to the NDK by editing and adding the following line to the file:


Then you need to define the NDK module to build by editing the build.gradle (Module: app) script, and add the following script code to the defaultConfig block in the file

ndk {
    moduleName "libgl2jni"
    ldLibs "GLESv2","log"

This controls the file name of the library and should match what is found in the Java file

Once you make these changes you can sync the project settings by selecting the sync option in the tooltip that should be at the top of the editor, or you can manually invoke it using the menu Tools > Android > Sync Project with Gradle Files

Step 5: Build and Run!

Now you should be able to build the project and run it in the emulator or on your device!