Category Archives: Tutorials

Quitting your game without killing the Android process

I’m back with a small article on how to improve Android Cocos2D-X based games. To quit an Android game when pressing Back, the engine sample tells you to call

  Director::getInstance()->end();

Now this the job in most cases, but has one major drawback: it doesn’t follow Android guidelines for finishing an Activity. Instead, it kills the whole Linux process hosting the Android game. This has implications if your game includes Android services (eg. for push notifications), because it kills the service as well. Therefore, your push notifications will not work properly.

So I had to come up with a fix. I implemented this C++ function which calls a native Java method:
Continue reading Quitting your game without killing the Android process

Building san-angeles and hello-gl2 samples from Android NDK

Recently I started playing with the Android NDK. Looking at the included samples, I stumbled upon a native compilation problem (using the latest Android NDK Revision 8b):
compilation failed for hello-gl2 and san-angeles:

Compile++ thumb : gl2jni <= gl_code.cpp
D:/Work/Apps/android-ndk/samples/hello-gl2//jni/gl_code.cpp:22:23: fatal error:
GLES2/gl2.h: No such file or directory
compilation terminated.
make: *** [D:/Work/Apps/android-ndk/samples/hello-gl2//obj/local/armeabi/objs/gl
2jni/gl_code.o] Error 1

Searching around, I found others with the same problem, but no clear solution. However, the problem is quite simple: Google forgot to include the Application.mk file for these samples.

So to fix it, you need to create Application.mk, or copy an existing one from another sample. You will need to set the APP_PLATFORM inside it to the one used by the Java project. Check the AndroidManifest.xml file for each sample (ie. )

Here’s my Application.mk for hello-gl2:

# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-5

For san-angeles it’s this:

# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-4 

Please note that for OpenGL 1.x, the minimum platform is android-4; for OpenGL 2.x, it’s android-5.

UPDATE: I was testing the OpenGL samples, but it seems others fail as well:
native-audio (set android platform to android-9 – at least)
native-media (set android platform to android-14 – currently maximum included platform in NDK)

UPDATE 2: It seems there’s also an open issue at Google for this:

I updated it with the solution:

Issue 36133: ndk-build.cmd does not work

I hope this helps.

Code snippet: Get all User installed apps on Android

In App List I needed to get the list of all user installed apps. So I made this little utility function:

	public List<PackageInfo> getInstalledPackages()
	{
		List<PackageInfo> allPackages = getPackageManager().getInstalledPackages(0);
		List<PackageInfo> installedPackages = new ArrayList<PackageInfo>();
		for(PackageInfo p : allPackages) //parse all packages to remove the ones with "System" flag
		{
			if(!((p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM)) //package is not marked as "System"
			{
				installedPackages.add(p);
			}
		}
		return installedPackages;
	}

I hope this helps.

Android tutorial – display embedded HTML file in WebView

Working on Power Schedule, I wanted to make a short help file, with some formatting. It seems there were several ways of doing this, but I chose to include an embedded HTML file into my application. It seemed easier to just edit an HTML file in an editor.
So let’s get started…