Using a Linux Virtual Machine for Android NDK C++ development

Alias Cummins
6 min readNov 30, 2020

--

A lot of people want to use C++ with their Android projects, but get stuck at the first hurdles, getting the tool chain up and running.

One reason for this is that Windows, and recently to a lesser extent, Mac OS X, can be tricky to set up and are not always the best environments for C++ development, especially if you’re more comfortable working with Linux.

If you want to get started with Android NDK development, you can quite happily use Linux, you don’t even need to have a full Linux install, you can just run it inside VMWare.

(N.B. Unfortunately, Oracle VirtualBox is not really up to the task for this, as it doesn’t have the same extensive support for hardware assisted virtualization, which you’re going to need — if you’re serious about developing for NDK, it’s worth investing in a VMWare licence. You get what you pay for!)

Why would I want to do this?

First off, it’s actually very easy to run Android Studio on Linux, and if you have a dedicated VM to run your Android NDK Projects, it’s easier to keep everything separate from your main desktop work.

Also, you will be able to take advantage of the various low level command line tools which can increase your productivity, and in general, Linux is just a more pleasant environment for C++ development. Windows and Mac tend to have a fair bit of hoop jumping, for example the Windows file system path length limitations, and weird case sensitivity awkwardness — or Mac’s OS X’s regular security updates / general march towards total domination of the Apple ecosystem. These issues can often lead to many wasted hours fighting against the OS, and lead to various workarounds like installing Unix emulation subsystems on Windows (which never really equate to using a real Linux system).

So, rather than spending hours fighting Windows, trying to compensate with tools like CygWin, Homebrew, Windows Subsystem for Linux, and so on, most often, it’s just easier to use Linux, especially for C++ development.

OK, that makes sense. Let’s do it!

So first off, let’s download and install VMWare. VMWare is a virtualization platform which lets you run various different operating systems inside another machine.

On Windows, you’ll want either VMWare Player or VMWare Pro. VMWare Player is free for noncommercial or educational use.

If you’re on Mac, you’ll need VMWare Fusion.

Once you’ve installed VMWare, you’ll want to pick a linux distro.

What’s a linux distro?
Ah, you must be new here. Linux is an open source operating system, and pretty much anyone with the inclination and time can make their own customized Linux OS. These are referred to as distros, (short for distribution), or sometimes flavor. Like jellybeans, Linux is available in many flavors/distros, but this article is going to focus on one specific distro.

Which Linux distro should I get?

So… this is where you will get bombarded with a lot of different opinions. Linux hackers are notorious for loving obscure versions of Linux, and there are literally thousands of Linux distributions to choose from.

Let me save you some time. If you want to develop Android apps, which use the NDK, in a virtual machine on either Mac or Windows, your best bet is a distro called Kubuntu.

Why? There are lots of reasons. For one, Kubuntu combines the KDE window manager, which is integrated out of the box with the various graphics libraries that Android Studio needs. You can install Android studio on Ubuntu, but it requires a bunch of extra configuration steps, which are kind of painful. If you’re curious, you can see these steps detailed here: https://developer.android.com/studio/install#64bit-libs

However, Kubuntu does not require these extra steps, and it also comes with a great package manager called Snap. Snap is a package manager for applications, and is increasingly how applications are being distributed on Linux.

So, just trust me. Use Kubuntu. It’s nice. It’s free. It’s low-effort to get running, and it’s a powerful, fully featured Linux, with a top class UI, superior in many ways to OS X or Windows.

Enable Hardware Assisted Virtualization

OK, this part depends on your hardware. Modern CPUs have the capability to do hardware assisted virtualization, which allows emulated processes to run at almost the same speed as native ones. However, it’s often switched off by default. VMWare provides instructions on how to do this, it usually involves changing one setting in your BIOS. If you’re on a corporate network, you may need to justify this to your systems administrator. If you’re a developer, you should be able to write an impressive, technical sounding justification for why you need this. Use the words ‘business critical’ — Or just send them a link to this blog if you’re lazy. Me and your sysadmin go way back ;)

Checking your VM’s hardware configuration

Ok, so fire up VMWare, select File > New Virtual Machine. This will give you a dialog to configure your VM, usually you can leave the default settings pretty much as they are, with a couple of tweaks:

N.B: Recommended Hardware-SSD Considered critical!

In this day and age, if you are doing professional work, your time is the most precious commodity you have. An SSD can triple or even quadruple the speed of things like compile time, and the general speed of your workflow. So ask IT for an SSD, and again use the term ‘business critical’. You can use an external SSD, ideally USB 2 or 3, as this will make the VM run smoothly and you won’t have the janky downtime you get with mechanical hard drives.

Ok, so you’ve got your nice big 2TB external SSD, you’ve installed VMWare, and you’re ready to setup your new awesome VM workstation!

Settings:

CPU: If you intend to move your VM around, it’s best to give it just one processor. If you’re on a beefy workstation and you’ve got a bunch of cores to spare, set it to 4–8 cores. You can always tweak this later on.

RAM: VMware will recommend an amount of RAM based on your system configuration, and it’s actually possible for a VM to have too much RAM, on an incorrect amount which can prevent it from booting up. However, Android Studio (especially the Java compiler) is a bit of a memory beast, so you might want to go up to about halfway between the maximum amount and the recommended amount. Again, you can always come back and tweak this to your specific requirements.

DVD Drives: Mostly you won’t need to worry about DVD drives, except to load the OS onto, via an ISO image. So it’s best to have only one DVD drive on your VM, just to avoid confusion between them.

Hard Drives: It can be a bit of a mission to resize an existing hard drive partition once it’s installed. I’d recommend at least 200GB just so you will have a bit of space to play with.

GPU: Again, this is hardware dependent, but VMWare will usually complain here if you have any major problems, and give you steps to fix them. Select the options for hardware acceleration.

Installing the Linux distro

Once your VM is configured, select your VM’s DVD drive, and navigate to the .iso file you downloaded. If all is well, you should get a nice blue glowing logo and Kubuntu will run through the installation process. This is pretty straightforward and should only take about 20 minutes or so.

(Making a VM for my local rabbi’s assistant)

Follow the instructions, and you’re good to go!

Alias Cummins is a software engineering consultant based in Switzerland, and the founder of UFID Studios Switzerland

--

--