[BY TIMOTHY BOGDALA]

Android Studio with NDK OpenGL ES Sample

Published: December 07, 2014

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:

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:

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 Android.mk 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="http://schemas.android.com/apk/res/android"
    package="com.android.gl2jni">
    <application
	android:allowBackup="true"
	android:label="@string/app_name"
	android:icon="@drawable/ic_launcher"
	android:theme="@style/AppTheme">
	<activity android:name="GL2JNIActivity"
		  android:launchMode="singleTask"
		  android:configChanges="orientation|keyboardHidden">
		  <intent-filter>
			<action android:name="android.intent.action.MAIN" />
			<category android:name="android.intent.category.LAUNCHER" />
		  </intent-filter>
        </activity>
    </application>
</manifest>

What this does is change the package name to com.android.gl2jni 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 local.properties and adding the following line to the file:

ndk.dir=C\:\\Projects\\android-ndk-r10d

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 com.android.gl2jni.GL2JNILib.

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!

<- OLDER ENTRY .|. NEWER ENTRY ->