vs-android: Developing for Android in Visual Studio

4 February, 2011 at 9:49am | Android, C++

Visual Studio - Android

DISCLAIMER: I tend to waffle on a bit. “Skip to the end” if you just want to find out about vs-android, and where to download it.
 

About six weeks ago I moved into that club which everyone else seemed to be part of, but not me. I joined the ranks of smartphone owners! No more 90′s flip-phone; I picked up a recent-ish Android handset. I’d never really been too into the whole concept of smartphone, but was starting to feel a little left behind. I must say that after those few weeks I’m sold, these things are pretty cool.

So, what’s a programmer to do? I think I may have actually downloaded the Android SDK before I even received the phone. Yeah, just a little bit eager! It’s actually the second handheld device I’ve tried to code on. The first being the Tapwave Zodiac, also with an arm processor and touchscreen. I never really got anything worthwhile going on it, my excuse being a job hunt and subsequent move to the United States. But nonetheless, it was a fun little handheld to code for. I was certainly looking forward to coding on a much more popular device.

Total Eclipse of the IDE

Now if you’ve ever tried to write anything for the Android, you’ll know the preferred development environment is Eclipse. If you’re writing your stuff completely in Java, you have everything you’d ever need in Eclipse. It seems to do all the nice syntax highlighting, and intellisense-like functionality that Visual Studio offers. Of course the shortcut keys are completely wrong, and must have been setup by a madman ;), but it does seem a pretty good IDE to develop in. Better yet, it’s free!

But is it for me? Nah. It’s not for me.

I just couldn’t get on with it. Primarily I want to write my code in C/C++ too. So I started to convince myself that I needn’t bother with Eclipse. I can get all the highlighting and quick-reference stuff in Visual Studio, and I could run batch files to build my code. After all, with C++ under Eclipse you still have to install Cygwin and effectively build from the command-line. (or setup batch/makefiles as you would with Visual Studio).


eclipse

 

I’ve used Visual Studio for a very long time now, since it’s older guise as simply: ‘Visual C++’. Was version 4.0 I think that I started with, when I moved from DOS to Windows programming. Now it’s my primary coding IDE; I’ve used it at both games companies I’ve worked at in the past decade. It’s an odd one to admit, but it’s too hard to let the thing go.

Makefile Project

So I set off and started a ‘Makefile Project’ in Visual Studio. I could update the system include paths under Visual Studio 2010 (something I think was trickier with earlier versions, or at least not set on a per-project basis, I forget). This meant I’d get properly working intellisense of the Android NDK headers, the ‘External Dependencies’ list in my solution was all completely correct too. All in all, a pretty nice setup.

I put together a batch file, which invoked a cygwin session running ‘ndk-build’. I then setup some further ones which would build the entire package using ‘ant’, and then install and run the application on the Android device connected to my PC. Not much to argue with here. For someone who was longing for Visual Studio back, after a few days with Eclipse. It was lovely.

If you want to try the makefile method, take a look at this website:

His method is a little cleaner than the one I used, my batch/script files were hard-coded messes of experimentation. Check out the debugging article on his page too, he’s got a way of using WinGDB to debug Android NDK code in Visual Studio. It’s awesome.

What about when it gets big?

Unfortunately Android’s NDK build scripts under Cygwin on Windows, isn’t exactly the performer. When your file count gets into triple figures the initial dependency checks take an age. I also wasn’t too enamored with not knowing what was passed to my compiler. You get a limited set of proprietary settings you can change in your makefile, but it did feel like a black box.


ndk-build

 

Coming from a console background, I like to know what my debug and release builds actually are. I also like to setup further build variants too… Usually one specific to profiling the game, essentially release with some limited modules enabled to do the profiling. The other major one would be a variant of my debug build, with asserts on but the optimization cranked up to max.

So it did feel a little confining. My realization about the build slowdowns with lots of files came when I attempted to integrate third-party code… Specifically what I’m wanting to use is the Irrlicht engine. It’s a well-structured open source rendering engine. Very versatile, and runs on a variety of platforms. Android included, of course.

Runing ndk-build, the thing would sit there for well over a minute before even starting to compile files. I’ve a recent i7-based laptop too, so it’s no slouch. I had an itching to try and delve into a more integrated method of building with Visual Studio, and that tipped the balance for me.

Enter MSBuild

With Visual Studio 2010 Microsoft made a radical change to the way their C++ environment worked. They moved over the entire thing to use MSBuild. Having worked with a certain non-Microsoft gaming console, you could see that they had to do a little hoop-jumping in previous versions of Visual Studio. Integrating custom compilers with the older versions really appeared to be quite a task. With VS2010, it had been hinted on some preview posts from Microsoft that this would be far easier.

Unfortunately though when I started to look into this, I found next to no information on the web about it. Well, really I found absolutely nothing in the end. I found a couple of questions on stackoverflow.com about the same subject, but with no answers. I ended up searching through the Visual Studio directories to find the existing scripts for Microsoft’s compiler. From there I duplicated the directory layout and began tinkering with setting up a new platform.

I’d never used MSBuild before this. It was completely new to me, never even seen a MSBuild file before. I hear some game companies use it, but ours certainly doesn’t. I can definitely recommend it though, it’s very powerful and flexible. It does have quite a learning curve, and Google searching for documentation can lead you to very bad explanations of how it works. If you’re at all interested in it, I recommend this book:

As well as being a very good guide to MSBuild, it actually had a small chapter about exactly what I was trying to do! Something I couldn’t find at all with Google. Unfortunately I picked the book up after I’d done the majority of my scripting, but it at least showed me I was barking up the right tree.

vs-android


vs-android

 

So, vs-android. Once I’d gotten all the features I wanted, and all the bugs I could find ironed out, I decided to release it as open-source. The regular download package just consists of a collection of MSBuild scripts, which need to be copied within a certain sub-directory of your Visual Studio installation.

Alongside the scripts is a single DLL file. When you’re working with MSBuild, you can code up custom tasks in a .NET language, such as C#. The header dependency scanning code lives here, which invokes gcc to find all the headers that the c/cpp files would be dependent on. As well as some other command-line switch manipulation code. The full source to the C# DLL is up on my Google code page.


vs-android

 

You can download vs-android here:

There’s full documentation on those pages too. Along with a step-by-step example of getting Android’s ‘san-angeles’ sample app compiling and linking. The Google Code page also contains more technical info about the implementation, if you’re interested. There’s a link on the main page to ‘tech notes’, which expands on some of the points I’ve touched on here.

One thing I’d like to mention is that you don’t need Cygwin at all any more, if you use vs-android. That in itself is a godsend! It’s been a few years since I last used it. It was compulsory for our build process a few years ago, when I was working in the United Kingdom. For whatever reason the install took far longer than I remember it taking, and it has a horrible long pause when the thing starts up. I’m definitely glad I don’t need to deal with it again for Android dev.

Finally, I’ll leave you with a comparison of the build times, versus using ‘ndk-build’ (sans long Cygwin pause). They’re pretty good, but I think with some more work I can get the thing running even faster.

Building 381 .cpp/.c files. The Irrlicht engine Android port.

ndk-build vs-android
Full Rebuild 9m14s 5m32
Incremental Build (1 cpp changed) 1m24s 35s
No-op build (build with nothing changed) 1m2s 0s
Single cpp build, aka Ctrl-F7 N/A 2s

 

What’s next?

I’d hope to continue updating these scripts in future. I’ve got a nice list of things I’d like to tackle already. For now though, I’m going to take a little break from the build scripts. I’m very happy that I can change gears back to writing game code again. Getting a ‘release’ together, with all that it entails, documentation and everything is pretty draining.

I think the last time I put something up on the internet like this was in 1997, when I worked on an editor for Command and Conquer: Red Alert. If you do click onto the garish Geocities website, keep in mind that I was a teenager at the time. :)

Comments

Pingback from Tweets that mention vs-android: Developing for Android in Visual Studio « gavpugh.com — Topsy.com
Time: February 4, 2011, 11:34 am

[...] This post was mentioned on Twitter by Gareth Bradley, Gavin Pugh. Gavin Pugh said: New post on my blog: vs-android: Developing for Android in Visual Studio http://bit.ly/fnyWyH [...]

Comment from @lx
Time: February 5, 2011, 6:17 am

wow, that’s a pretty cool work, I’ll give it a try, thanks!

Comment from Paul K
Time: February 7, 2011, 12:00 pm

Interesting. I’m not an Eclipse fan and have been looking for a more familiar alternative for Android. I’ll have to check this out! Thanks!

Comment from Peter
Time: February 11, 2011, 7:08 am

It tried and it works on my PC with the sample. :)
It looks very promising. I have to port a pretty complex image processing module to Android from our imaging SDK for Windows/WinCE. I am thinking to use your stuff…
Do you have any experience with Standard C++ Library support in the NDK?

Comment from Gavin
Time: February 13, 2011, 2:16 pm

Hi Peter,

There’s an issue that was recently posted here, with a workaround to use stlport:
https://code.google.com/p/vs-android/issues/detail?id=2

Making the change suggested to the build file should allow your STL code to compile. As mentioned in the issue post, I’ll add some support to the property sheets in a future update.

Thanks!

Comment from Sam Hobbs
Time: February 14, 2011, 10:09 pm

Thank you for vs-android. I have downloaded it but I have not yet installed it. There are so many options for Android development. I am very tempted to use vs-android so I can use C++ in Visual Studio.

First, I want to suggest that you fix the link to this web site from code.google.com; when I click on your “Homepage” in that web site, the link does not work.

Do you know if it is possible to convert your install.cmd to a Windows Installer (msi) type of setup? I think you can do that; create a cab file maybe and a msi file to install the files.

Comment from Gavin
Time: February 15, 2011, 8:40 pm

Hi Sam,

Thanks for the tip on the website link!, it was the one tucked away in my Google user profile it seems. Fixed it now.

Yeah, I’d like to do an installer in future. Particularly in order to make the assignment of paths to the Android SDK and NDK easier, so the user needn’t bother with editing env-vars or files. Bit of a ways off right now though, not a priority just yet.

Cheers, thanks for the feedback.

Comment from Sam Hobbs
Time: February 16, 2011, 6:17 pm

Feel free to contact me directly; I might fiddle with the setup thing. I would contact you offline from this but I don’t see an email address or whatever. Also, for what it is worth, I am a SFV guy too.

Comment from John
Time: March 10, 2011, 6:43 am

I assume because your using MSBuild, that this can be used with VB? Or is this simply a C-solution? Im in the task of learning and starting a migration project from .Net 3.5 to Android. Mostly in VB for .Net but was wondering if this vs-android solution you provide allows for VB coding as well.

I havent touched the C language since they came out with MFC, which turned me off real quick to it. Long time lover of the C language, since i learned ForTran 77 and Ada 95. I know with .Net 4, the margin between VB and C is now miniscule and almost a flavor based choice, which is why i ask if this android solution can be used with VB.

Comment from Gavin
Time: March 10, 2011, 9:33 am

Hi John,

Yes, vs-android is at it’s heart just a collection of MSBuild scripts. However, these call into the NDK toolchain which Google provide. So you’re stuck with C/C++ for the language you’re coding in unfortunately.

There’s MonoDroid for C# which might be a better compromise for you: http://monodroid.net/ … Can’t vouch for it though, I’ve never used it.

Cheers.

Comment from John
Time: March 10, 2011, 1:25 pm

where is the instruction on how to attach VS2010 into this platform….or do i simply create a empty project, and create a default build target?

Comment from Gavin
Time: March 10, 2011, 3:36 pm

I’d suggest starting here with the san-angeles demo:
http://code.google.com/p/vs-android/wiki/UsageExample

It takes you through creating a new solution/project, and setting it up to compile some Android code.

Comment from Pete
Time: April 5, 2011, 7:49 am

Hey Gavin,

vs-android has worked great for me so far. I was hoping to do some modifications for my local environment, but when I got the code from the google code page there was a missing file GetGCCHeaders.cs. Is it possible you just missed adding that to the source control?

Thx,
Pete

Comment from Gavin
Time: April 6, 2011, 1:27 am

Thanks for the headsup. I’ve uploaded that missing file now. Glad you’re finding vs-android useful!

Cheers.

Comment from Jamie Licitra
Time: April 14, 2011, 1:43 pm

Have you gotten this to work with the Honeycomb SDK? I assume it should – but before diving into it I figured I’d ask.

Thanks,
–Jamie

Comment from Gavin
Time: April 14, 2011, 10:11 pm

Hi Jamie,

I haven’t tested it with Honeycomb. Would be interested in hearing how it goes, I’m a little snowed lately to give it a try. I’d figure that as long as they didn’t radically change the directory structures, it’d be fine. It survived the upgrade from ndk5 to 5b without requiring any changes.

Cheers.

Comment from Asaf
Time: May 22, 2011, 3:07 am

Hi Gavin,
First, thanks for this very useful utility. If only I had known about this before installing Cygwin…

I have 2 questions:
- Did you ever get to adding the STL options property?

- The linker which comes with the NDK is sensitive to the order of libraries in the command line.
We have several projects – most generate a static library and one is a shared library which contains them all. For the linking to work we had to remove the project references and add the libraries manually to the command line.

Is there a way to create the command line so it takes into account the build order or another project dependency (in reverse order, of course)?

Thanks,
Asaf

Comment from Gashara
Time: May 24, 2011, 4:26 am

it’s so good!!!! you are my hero… (sorry about my english skill not good.. I’m Korean)

I have a BIG problem…….. How can I make the /obj/local/ “binary” .so ??? (to debug)
your program is So good, but it can’t Debug..
if use ndk-build, it can make debug information but, you know.. IT’S NOT COOL.
I really need debug…… so,

I want, make Debuggable project. through WinGDB or ndk-gdb..

plz help me ;_;

Comment from gashara
Time: May 24, 2011, 6:58 am

Hi!! I have some question for debugging.
I know about using WInGDB, so I want make NDK project for WinGDB Debug.
but, can’t make that without use ndk-build.. have you some answer?? with vs-android?
plz.. ;o;

Comment from chris
Time: May 25, 2011, 1:18 am

Absolutely cool work!
Do you have an idea when the next vs-android version will come out? (already awaiting the new STL options :-)))

cheers,
Chris

Comment from Manu
Time: May 29, 2011, 4:03 pm

How hard would it be to adapt this work to Google’s NaCL (Native Client) toolchain?
That could really benefit from the same treatment, and it would seem there may be a lot of cross over/shared work… Possible?

Comment from Gavin
Time: June 1, 2011, 5:17 pm

Some catchup… I’ve been out of town on my honeymoon. Back to reality now! :)

@Asaf – I didn’t complete the STL work, unfortunately. It was what I was in the middle of the last time I worked on it though, many weeks ago. It’d be in the next version for sure.

As goes the linker command-line, the dependency libs IIRC are expanded as one MSBuild variable. I don’t think dependency information is available during the Link ‘target’ script. Possibly the dependent libraries are in some logical order, but that order possibly needs to be reversed? Doing that would be pretty easy.

@Gashura – He contacted me through other means. I’ve not successfully been able to use WinGDB myself, but I think my problem was using an un-rooted retail phone. This link describes a workaround:
http://ian-ni-lewis.blogspot.com/2011/05/ndk-debugging-without-root-access.html

@Chris – It’s the next ‘hobby-coding’ thing on my list, to do an updated vs-android. E3 looms though next week, so I doubt I’ll be onto it until after then.

@Manu – I’ve got no experience of the NaCL stuff at all, but if it uses a command-line compiler and linker, I can’t see why not. If it’s GNU-GCC style too, it should be pretty trivial to setup another ‘PlatformToolset’ for it.

Thanks for the feedback everyone!

Comment from Brian
Time: July 16, 2011, 4:15 pm

I installed vs-android, downloaded and installed android-ndk-r6-windows.zip as discribed in your guide. All went well. I tried compiling the sample program sanangeles downloaded from your google page. It appears to me r6 of the ndk does not work with the sample program. I get a build error stating it cannot find C:\android-ndk-r6/toolchains/arm-eabi-4.4.0/prebuilt/windows/bin\arm-eabi-gcc.exe. I checked the directory.It is now …/arm-linux-androideabi-4.4.3. The files in the dir specified in the build now bwgin with arm-linux-androideabi.

Comment from Gavin
Time: July 16, 2011, 10:10 pm

Hi Brian,

Yeah, vs-android was developed initially with NDK r5b. r5c also happens to work, but r6 removed the 4.4.0 toolchain.

Just switching the directories like you mention is a good enough workaround for now.

A new release of vs-android is imminent (19th-20th most likely), and works out-of-the-box with the r6 SDK.

Cheers.

Comment from Kak0
Time: July 18, 2011, 2:19 am

Hi Gavin,

It seems like you did a fine work with vs-android, it is almost exactly what I was looking for. Except that, for some reasons, I’m stuck with VS 2005, so do you think there is any chance vs-android will work under VS 2005 soon ? Or should I try to go with the article you talk about in your post and tweak it a little to make everything work fine ?

Cheers, and keep up the good work !
Kak0.

Comment from Kak0
Time: July 18, 2011, 7:12 am

Edit : woops, it’s not even VS 2005 but VS 2003

Comment from Gavin
Time: July 18, 2011, 6:14 pm

Hi Kak0,

Sorry you’re out of luck with versions older than 2010. Microsoft overhauled the entire C/C++ compilation side of things with the switch to 2010, they’re now using their MSBuild system for it. It’s far more flexible now than previous versions.

I think it’s possible with previous versions of Visual Studio, but does require a lot more effort. It’d be a completely different method, likely writing an ‘add-in’ to provide the UI options, and then somehow catching or overriding the calls to the compilation tools.

Cheers.

Comment from Brian
Time: July 21, 2011, 8:36 pm

Hi Gavin,

Glad to see you released the new version compatible with android ndk r6, but I think I might be having another version issue. I installed ant v 2.20 with dependencies (apache ivy) from the link you provided on your install page. I created an enviroment variable pointing to the dir (c:\apache-ivy-2.2.0). I ran the build on the san-angeles sample you provided in the new samples download package, but get an error stating it “cannot find ant, my variable ANT_HOME might be wrong, it’s currently c:\apache-ivy-2.2.0″. Not sure. Do you see anything I’m missing? I’m certain the dir I unzipped the ant package to is c:\apache-ivy-2.2.0. I copied the dir name and pasted it into te environment variable to avoid typos.

Best Regards,

Brian

Comment from Brian
Time: July 21, 2011, 9:26 pm

OPPS!

Sorry for the last comment Gavin. As I’m sure you release (and are probably either laughing at or cursing at depending on your mood) I had a brain freeze and downloaded the wrong package from apache. I corrected that, everything is working fine. Thank you for releasing this fine piece of work!

Best Regards,

Brian

Comment from Gavin
Time: July 21, 2011, 10:57 pm

No worries, Brian. Glad it’s working for you!

Comment from Konstantin
Time: July 22, 2011, 12:47 am

Great project! Gavin, thank you!

Trying to build pure native app (based on NativeActivity and android_native_app_glue), but can’t find how to include and build android_native_app_glue lib.

UPDATE: now I’ve manually included android_native_app_glue.c to the project and it works fine. But it will be good to have an ability to add libs from ndk/sources/ like $(call import-module,android/native_app_glue) in ndk-build.

Do you plan to add this?

p.s.: for me vs-android works +3x faster compared with cygwin build =)

Comment from Gavin
Time: July 22, 2011, 9:50 am

Hi Konstantin, glad vs-android’s working well for you. Happy to hear the speedup is good.

I think I held off with looking into NativeActivity because it needs a minimum of a 2.2 device (IIRC), and there’s still a ton of 2.1′s out there. Though given your description it seems pretty straightforward, so I’ll add it to the list for sure.

Cheers.

Comment from Brian
Time: July 22, 2011, 6:50 pm

Hi Gavin,

Is there a way to add the ant environment variable ANT_OPTS=-Xms512m -Xmx512m or otherwise set the JVM heap size within vs-android for a build?

Brian

Comment from Gavin
Time: July 24, 2011, 9:44 pm

This sounds like a solution to this guys’ issue potentially:
http://code.google.com/p/vs-android/issues/detail?id=15
? I haven’t come across these problems myself. Possibly because the Java-side of my projects tends to be tiny.

It should drag in all system environment variables to the run of ant. You’d need to restart your Visual Studio session though.

I can push in temporary environment variables through to the Ant task. I’ll try and provide something in the property sheets with the next version for this.

Comment from zappa
Time: September 29, 2011, 3:11 am

Hi Gavin, everything is working perfectly. oh yes, one thing to change is to differentiate the building process to the start debugging process. For now, when I build a project, the app is deployed automatically.
thanks again for this amazing job and I’m impatient to get the 1.0! I will check your blog to see if I can make a donation to support this great project!

Comment from Pedro Vicente
Time: January 7, 2012, 1:03 am

ok, got it to work the san-angeles sample in the latest emulator version (level 15)
next
how to debug under Visual Studio ?

Comment from Gavin
Time: January 7, 2012, 2:06 am

Hi Pedro,

Debugging is “you’re on your own” right now, unfortunately. I’ve heard reports of WinGDB working, and reports of straight ‘gdb’ too. The problem is that no-one has written up a HOWTO guide of how to set this up. I’ve certainly tried encouraging various people I’ve exchanged emails with, but nothing has come of it yet.

Personally, I’ve not had time to work on vs-android for quite a while, so it’s not something I’ve been able to do myself. The next batch of time I get to work on it, hopefully I’ll be able to write up something. Could potentially be many weeks or months from now though.

Also, I had to remove all those other comments you added with the issues you were experiencing. It seems like you managed to get through them all, but the vs-android site is definitely a better place for them. Specifically the ‘issues’ section is the best place to post problems you’re having.

Cheers.

Pingback from Microbender Blog » vs-android: Developing for Android in Visual Studio
Time: October 21, 2012, 5:29 pm

[...] Developing for Android in Visual Studio vs-android: Developing for Android in Visual Studio A Videogame Programming Blog This was posted on [...]

Comment from tony
Time: August 19, 2013, 4:06 pm

Thanks so much. This is awesome.

Write a comment



 (not shown when published)



 (comments will appear after moderation)

*