Simon Morris, Guest Blogger from Strawdog Studios
Welcome to the world of Android NDK. Please leave your debuggers at the door.
Veteran programmers of a certain age will happily recount old-school war stories where they’ve had marathon sleepless debugging sessions where bugs were discovered in DVD-only release builds at the 11th hour before shipping and that they managed to debug the problem and save the day using just printf and changing screen border colours when certain systems started up.
So it was our experiences with Android development over the last year that reminded me exactly how much we take debuggers for granted these days. I think it’s fair to say that Android development requires a certain tenacity, but here at Strawdog HQ we have certainly found that getting a debugger to work with native code on Android requires something approaching supernatural powers! This is why until recently, debugging on Android had sadly remained an unsolved challenge for us, and that meant we were still in the trenches, developing on Android with only a printf to keep us going, and despite all the hard-core glory of printf’ing our way to a stable build, we were constantly and painfully reminded of how massively time consuming it is to develop without a debugger. And time means money, right?
Well, you can imagine how excited we were toward the end of last year when we heard that NVIDIA had a new solution for Android dev, that not only promised us a debugger, but also a development environment that integrated into Visual Studio. Sounded like purest win.
At the time we were busy working on our forthcoming Android title “Space Ark THD”, which is a new mobile optimized version of our Xbox360 Live Arcade title (coming real soon to Google Play!!), and we were getting pretty fed up with the speed of printf style dev, so we thought we’d check it out.
Nsight Tegra – First Impressions
Now we’d already used the Tegra Android Development Pack, which massively simplifies the many tools, drivers and SDKs you need to get started with Android development into one single install (highly recommended, check it out here: https://developer.nvidia.com/tegra-android-development-pack), so we installed the latest version since that included Nsight Tegra as well.
Out of the box, you get a toolbar that is integrated into Visual Studio (2010 for us) that has ADB device bridge and logcat output windows. There is also a solution containing a selection of sample projects and libraries, which we noticed are targeting a new integrated “Android” platform. (Nice!)
An initial survey of the sample project properties showed that the Visual Studio integration was impressively deep - the familiar configuration property pages are all present, but with Android-specific settings to control the full compile/link/sign/deploy APK build process. Hitting build in the solution just worked for me and everything compiled and linked first time (Something I always find reassuring), and I had a bunch of APKs to play with.
Next step was to try out the debugger, so I hooked up my Nexus 7, hit debug and sure enough, a new dialog appeared, which downloaded the APK to the device and started a debug session. I dropped some breakpoints and this also “just worked”, complete with call stacks, memory views and single step in/out controls. I confess I almost wept with joy at this point.
Building our own Native Android App
In terms of our background, as a studio we have shipped many titles over the years that have been built using our own internal C++ cross platform game engine SDK, which allows our games to run on most mobile and console platforms (Windows/iOS/PS3/Xbox360 etc.)
Early last year we began our journey with Android integration, but for various reasons we had been unsatisfied with the solutions we had tried, and had been actively looking out for a good way to build native Android apps without middleware SDK’s or complicated/unfamiliar build setups.
I can say that spending some time with the Nsight Tegra sample code was an epiphany for us. When new to any platform, it’s hard to overstate how useful it is to single step and debug break through all the sample code to get a solid feel for how it all works and fits together. NVIDIA provide some incredibly useful and robust framework apps in their samples – both Java and Native – as well as documentation for some best practices when using the NDK. There are examples for covering most of the core systems you’ll need like handling app lifecycle events, touch, joypad and accelerometer input events, plus multi-threading, display management and OpenGL ES and OpenSL ES sample code.
So armed, with the knowledge that based on Nsight Tegra, we could now build and debug native apps on Android within Visual Studio we took the decision to add a complete Native Android implementation of our API’s to our tech.
Porting our game tech to Android
The first step was to get our codebase compiling and linking. We use custom jam scripts as a cross-platform means to compile and link the various libraries that comprise our SDK. Getting this working with Nsight Tegra was pretty easy – we just wired our scripts upto the same GCC toolchain used by Nsight Tegra and copied the build settings from Visual Studio.
Next step, was getting the build up and running . Once our tech was all compiling and linking against the NDK, our approach was to slowly introduce the engine systems one by one so we could see each core system was working within the framework we knew was stable. The first few days of the port were all about fixing asserts, adding missing Android implementations, and getting the file system working. Again, it’s hard to overstate how productive one can be with a debugger here.
I should also mention that we based our new app entirely around the “native globe” sample application framework provided by NVIDIA and I’m certain we saved a whole bunch of time here. Our app is a fully 3D OpenGL ES 2.0 driven game – and re-using an existing framework saved us all the hassle and intricacies with native_app_glue, lifecycle management and JNI that I’d read about being necessary to getting to get a native OpenGL based app off the ground. We’ve also run the app through AppThwack (https://appthwack.com/ - a really awesome online device compatibility testing site) and got an excellent report, so apps built this way will run on more than just Tegra devices.
Goodbye printf’s, hello debugger!
So I know that it should go without saying why debuggers are useful, but I’d like to share a couple of real world examples of issues we had where not having
something like Nsight Tegra would have made life really (and I mean really) miserable:
- Getting systems working – Our audio library was adapted for OpenSL ES which kicked out some unexpected runtime issues like missing/Null callback functions, and API compatibility issues. With Nsight Tegra we were quickly able to identify these, fix them and iterate the solution.
- We found our build worked great on Nexus 7 hardware but when we tested on another device it crashed in some seemingly benign constructor code. It turned out the problem was unsupported VFPU instructions on that device, and we found that real quick by single stepping through the disassembly in Visual Studio. Our initial assumptions about the cause were completely wrong and actually fairly paranoid such as memory overwrites, member initialization order etc. so I cannot begin to imagine how long that would have taken to fix with the old-skool printf style!
- We’ve since isolated and fixed numerous compatibility and run-time issues in what feels like record time, since we can simply run debug builds on the target devices (no need to use slow emulators anymore!) and pick up any problems that way.
Space Ark THD now preparing for launch
Since then, we’ve gone from strength to strength beefing up our Android tech, and easily adding new Java classes for stuff like Facebook and we now have a fast, smooth and stable Android game that anyone of the guys in the studio can use Visual Studio to build, run and debug on any Android device that they have plugged into their USB, and all this took us about 6 engineer weeks to achieve, and we are back on track for a spring launch of “Space Ark” on the Google Play Store.
To sum up, if you’re used to Visual Studio, and thinking about bringing an existing or a new OpenGL/C++ project to Android, NVIDIA Nsight Tegra and the Tegra Android Development Pack could be just the very tool you are looking for.
About The Author
Simon Morris is the Technical Director for Strawdog Studios. With over 20 years of experience and 18 titles under his belt, he knows a thing or two about painful debugging processes.
Strawdog are a leading independent developer focused on building the best social and mobile apps for publishers and brands.
About Space Ark THD
Bounce your way through 36 challenging missions to terraform and breathe life into 5 new worlds!
Discover how to collect all the valuable crystals and get the best combos for the highest scores!
Beat your friends and unlock powerful upgrades to complete your mission!
Coming Spring 2013 to Android Tegrazone! www.spaceark.com