Debugging Android Cocos2dx-3.0(rc2) with Visual Studio and Tegra Nsight 1.5

I have been writing NDK since 2011, and I love MSVC as a debugger… I was excited when Nvidia just released Nsight 1.5 which has good support for Android on MSVC. I’m just NOW learning Cocos2d-x, so I decided to see if I could get them all working together this evening. And I had great success!

My goal was to create a standard cocos2d-x 3.0 project which supports Win32, and use that same MSVC sln to include an Android project that could be debugged through Visual Studio also.

I figured I’d write about it, since maybe others want to do the same thing, and the community could probably improve this process.

Ok, you need to realize that this depends on the Android tools that Nvidia has been releasing… They created the TADP, which includes ALL tools you’d need to set up an Android debug environment, Eclipse or Visual Studio, SDKs, NDK, Ant, etc… or if you already have a setup and VS2012, you just need to install Nsight 1.5 for NDK.

TADP here: https://developer.nvidia.com/content/all-new-tegra-android-development-pack-20r8-now-available-under-nvidia-gameworks
Nsight here: http://docs.nvidia.com/gameworks/index.html#developertools/mobile/nsight_tegra_v1_5.htm%3FTocPath%3DDeveloper%20Tools|Mobile%20Developer%20Tools|Nsight%20Tegra%2C%20Visual%20Studio%20Edition|Nsight%20Tegra%20Visual%20Studio%20Edition%201.5|_____0

Now, assuming that installs and you can debug their samples in Visual Studio 2012, let’s debug some cocos2d! The thing that took me the longest was reconciling PATHs because I had a bunch of different installations of python and android laying around, but the actual process is fairly straightforward.

Create a new project:

cocos new MyGame -p com.mygame -l cpp -d d:\test_app

inside d:\test_app\MyCppGame\proj.win32 is a sln file: MyCppGame.sln, Open that in VS2012.

Compile and build the win32 version to make sure that it runs and debugs. Then stop debugging.

Now, click FILE->New->Project…

That opens a dialog with a bunch of options. On the left (assuming Nsight is installed), you should see Installed->Templates… select “Android”. You’ll see about 7 options of Android type projects… Select “Import a Makefile-based Android Project”

Now, choose a name for your project, MyAndroidProject… then select the directory “D:\test_app” and instead of “create new solution”, choose “Add to solution”. Visual Studio should find the path to the solution. Click Ok.

Visual Studio will then ask what files you want to import into the project. I’ve found the defaults to work ok, but you need to “Browse” to “d:\test_app\MyCppGame\proj.android” (or whatever directory your AndroidManifest.xml lives).

Visual Studio will then ask you to select a project number. Be careful not to select an SDK version that your device doesn’t support (4.4 is Android 19, 4.3 is Android 18, etc). Click Finish.

Now… the way Cocos2d-x normally operates: you call:
cocos run -s “d:\test_app\MyCppGame” -p android
it eventually calls android-build.py, which does some platform-specific stuff and eventually sets your NDK_MODULE_PATH. You need to capture the results of this evaluation to pass the same arguments to Visual Studio. so if you run the above command line, cocos2d-x will output its ndk-build command line which look like (for me):

-C d:\test_app\MyCppGame\proj.android -j4 NDK_MODULE_PATH=d:\test_app\MyCppGame\cocos2d;d:\test_app\MyCppGame\cocos2d/cocos;d:\test_app\MyCppGame/cocos2d/external NDK_DEBUG=1

with a few “proj.android/…/” in there that I edited out for clarity.

Copy that output to clipboard and, in Visual Studio, right click on your Android project properties. In Configuration Properties->NDK Build, there is a menu “Build” command that is set to “NDK_DEBUG=1”… highlight that and paste in the command line from above (excluding “ndk-build”). If you have a multicore machine, make sure there is -j4, as on my machine it used -j1, which makes builds really slow.

Now, if everything worked correctly, you can right-click your new Android project in the Solution Explorer, select Debug->Start New Instance… The project will build, deploy and debug (you’ll need to set a breakpoint in your AppDelegate or somewhere). The initial “Deploying Application” step will take a couple minutes the first time, but afterwards happen in seconds.

Anyway, I typed this up in a frenzy once I got it to work… I’m sorry for any errors or omissions! If you have any questions or problems, I’m happy to help. Android debugging can be a … difficult … undertaking.

3 Likes

@Ajas
Thanks for your sharing.
So it supports debugging c++ codes for Android in MSVC?
It is so great.

Yes, it supports debugging C++ for Android on MSVC, even some “advanced” stuff like using data breakpoints (watchpoints). There are a couple more things I found that are important:

in Application.mk, you need to add -gdwarf-3 to the APP_CPPFLAGS, and you need to specify your “NDK_MODULE_PATH” arguments to the ‘clean’ and ‘rebuild’ ndk-build settings to get those to work right too.

Also there is an updated Tegra Tools that has been released (with the NSight 1.5.1 Visual Studio support): https://developer.nvidia.com/content/tegra-android-development-pack-20r9-released-latest-android-sdk-and-ndk-r9d-support

Here’s a screenie of the Hello World in action :wink:

Great.
Thanks.

Hi @Ajas,
Thanks for sharing this nice work, I have a problem i’m using Tegra Nsight 3.0r1 ,In Configuration Properties i have’t NDK Build!!!

Also can u share your MyGame Project?

Thanks.

When looking for Configuration Properties, there are several ones. Make sure you are right-clicking the PROJECT you just created… (NOT the Workspace).

For instance, mine looks like this:

I still need to download Nsight 3.0r1,

Thanks @Ajas ,this my property page.


Any idea??

So instead of “NDK-Build” it’s “NMake”… did you try following my instructions filling out “Build” arguments and making NDK_MODULE_PATH? You don’t even say what the error is…

I try it, I got the following error :
Error 13 error MSB3073: The command “-C d:\android\samples\test_app\MyGame\proj.android -j1 NDK_MODULE_PATH=d:” exited with code 9009.

@Ajas this my command :
-C d:\android\samples\test_app\MyGame\proj.android -j1 NDK_MODULE_PATH=d:\android\samples\test_app\MyGame\cocos2d;d:\android\samples\test_app\MyGame\cocos2d\cocos;d:\android\samples\test_app\MyGame\cocos2d\external NDK_DEBUG=1

ok, it looks like it’s not automatically prefixing these build arguments with “ndk-build” then… So you should just put it there yourself-- put "ndk-build " before the -C …

Thank @Ajas now i can build the new project but i get the following error when i click Debug->Start new instance where Tegra is Android project Name :

@Ajas , Error 14 error MSB6006: “ndk-build.cmd” exited with code 2. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Platforms\Tegra-Android\Nvidia.Cpp.Tegra-Android.Make.targets .

can you help me ?

@bangthaisu – is there any more info besides that?

@hossainiir – it’s looking for your android apk in the proj.win32 directory. You should figure out where that apk got built to and fix your paths.

I also found a weird issue in the newer 3.2 Application.mk.

The lines:

APP_DEBUG := $(strip $(NDK_DEBUG))
ifeq ($(APP_DEBUG),1)

cause the gdbserver not to be included in the apk, causing an error in the “Starting GDB server” part. Change those 2 lines simply to:

ifeq ($(NDK_DEBUG),1)

and you can debug again!

@Ajas , I did as your guide, i debug and then " Error 14 error MSB6006 "
. i’m using cocos2d-x 2.2.1 , tegra 3.0r1, vs 2012.
Thank you for your quick reply .

I gave this a try with cocos2d-x 3.2, tegra 3.0r2, vs 2013 with bad results. To anyone who’s thinking about installing TADP, it’s 6 gb large or so!!

I choose Import and ndk-build Android Project:

Was that the correct choice?

I get the error when building:

1> [armeabi] Compile++ thumb: cocos2dx_static <= CCPhysicsBodyInfo_chipmunk.cpp
1> error: error opening ‘…/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.o.d’: Error opening output file ‘…/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.o.d’: Can’t find the search path

1> 1 error generated.
1> make.exe: * […/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsBodyInfo_chipmunk.o] Error 1
1> make.exe: * Waiting for unfinished jobs…
1> [armeabi] Compile++ thumb: cocos2dx_static <= CCPhysicsContactInfo_chipmunk.cpp
1> error: error opening ‘…/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsContactInfo_chipmunk.o.d’: Error opening output file ‘…/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsContactInfo_chipmunk.o.d’: Can’t find search path

1> 1 error generated.
1> make.exe: * […/…/…/…/…/cocosIDEprojects/vsprojects/VSTest/VSTest/VSTest/Tegra-Android/Debug//local/armeabi/objs-debug/cocos2dx_static/physics/chipmunk/CCPhysicsContactInfo_chipmunk.o] Error 1
1> make.exe: Leaving directory `D:/Programming/cocos2d-x-3.2rc0/projects/VSGameCenterTest/VS_GameCenterTest/proj.android’
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Platforms\Tegra-Android\Nvidia.Cpp.Tegra-Android.Make.targets(56,5): error MSB6006: “ndk-build.cmd” exited with code 2.

I open the really short folder path:

“D:\Programming\cocosIDEprojects\vsprojects\VSTest\VSTest\VSTest\Tegra-Android\Debug\local\armeabi\objs-debug\cocos2dx_static\physics\chipmunk”
and there is indeed not a single .o file inside that directory (it has files outside the dir tho).

Any kind of help is appreciated.

Thanks for posting this, I was going insane finding a stable (and free) solution for debugging Java/C++ code in Cocos2d-x 3.5 apps for Android on Windows.

Good news:
I had success in getting this to work with Tegra 2.1, Visual Studio 2013 & Android 4.2! Debugging Java & C++ code in the Cocos2dx proj.android solution worked perfectly in Visual Studio.

Bad news:
On Android 4.3+, I get an error when the NSight debugger tries to launch the application on the device: Failed to pull device files: Index was outside the bounds of the array.

I’ve posted about this on the NVidia forums so I’m hoping there’s some easy workaround or patch for it. Any ideas anyone ? Thanks in advance.

Update:
Discovered later that the Tegra Android Development Pack has a nag screen when debugging devices that don’t have NVidia chips. See here: https://developer.nvidia.com/nvidia-nsight-tegra-licensing

To get around the error I mentioned on Samsung 4.3+ devices, the Android device needs to be rooted - I think. But anyway, I ended up picking up a cheap Nexus 7 for debugging C++ on Android and got it all humming along with Android 5.1 - no interrupted debugging now. Good-bye to Eclipse.

I have used the Eclipse plugin from NVIDIA called Debug Manager for Android NDK and it worked great for breakpoint debugging on Android.

Has anyone else tried using the NVIDIA Debug Manager and the NVIDIA Tegra Nsight for Visual Studio?
I am curious if someone has used both, which one they liked better and the reasons why one might be better than the other.

Primary Requirements

  1. Breakpoint C++ debugging on Android
  2. Does not break the cocos console build and game publishing processes.
  3. Minimal hacking required to get it to work reliably.

This post talks about using the NVIDIA Nsight for Visual Studio Android debugging.
vs.
In the post in the following link I added some info about getting and setting up the NVIDIA Tegra Eclipse C++ NDK IDE with breakpoint debugging: How do you debug in Cocos2d-x?

With regards to your comment about needing Visual Studio Pro in the other post, you should be able to use Visual Studio Community Edition which is free and has the same functionality as VS Pro, however there are licensing restrictions. https://www.visualstudio.com/en-gb/products/visual-studio-community-vs

Visual Studio 2015 has built-in android/NDK debugging but I haven’t played with the preview version as yet.
http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/12/introducing-visual-studio-s-emulator-for-android.aspx