For the last few days I have started to look into cross platform development environment Xamarin to do a little mobile development. After spending few hours trying unsuccessfully to get Jetbrains Rider to work with Linux and Xamarin, I caved and installed my dev environment on a windows virtual machine. So I am running Xamarin on a virtual machine with visual studio. All is good in the world, right?
Well, no. There is one caveat in my particular setup that is frustrating.
The android emulator inside my vm is trash. Slow as hell and unresponsive. Now, I could use a real device. In fact I have a real device connected to my vm right now and I have used it. However having an emulator or something close enough have some advantages like say, larger screen real estate so my neck doesn’t break from constantly looking down at the small phone on my lap when I run tests.
The smarter (or smartasser) reader will now berate me and say “just use windows!” or better yet “buy a frigging mac you dumbass”. Well sorry folks but I run Linux and that’s the way I roll. So if I can setup my environment with minimal exposure to the aforementioned systems, happier I am. So here’s the challenge: Android emulation inside virtual machine is god awful, using real device is another pain in the ass so I need another virtual machine running android and connect to that.
So let’s get started. First things first, is it even possible?
Hell yeah it is. Thanks to awesome folks at android-x86.org. Here we can download iso files that can be used to install android on a vm.
Next, we need virtualization software. I’m running virtualbox. However I will not cover installation of virtualbox itself in this post. I assume you have everything ready yourself.
So let’s create our vm. First start your virtualbox.
Then press new (bluish starry thing upper left corner). Set your preferred name and choose operating system as linux with version from all ranges.
Choose the amount of memory. I prefer at least 2G.
Create a new hard drive as vdi, dynamically allocated and default 8G should be enough space.
Finally you should have your virtual machine initialized. Let’s tweak it a little more (press the orange icon)
Go to system and add another cpu, if you can.
Then max out the display memory and accelerate 3d.
Finally, setup the network settings. Change the adapter to bridged and whichever your primary adapter is at your host computer. What this setting does is tell the virtual machine to attempt to connect to your router and get an ip address from there as if the machine was like any other computer in the network. Using NAT would mean that any connection would go through your host machine and connecting to that from another vm is network trickery out of the scope of my expertise.
Finish your tweaks and start the virtual machine. Since you are running this for the first time you should get a dialog asking for an installation image file. Navigate to whichever iso you downloaded and start.
We should see a menu with different options available. These android images can be run as live cd’s so if you just want to test the cd out, choose Live CD. However we are doing installation so let’s choose, you guessed it, Installation.
Couple of seconds later we are met with another menu. This time we need to create a partition so press down (to choose the option) the option and press enter at the ok.
We don’t want to use GPT.
So now we are at cfdisk, which is one of linuxes partition managers. Choose new and press enter. Press enter a couple of times (no need to change values here, the defaults are fine) and finally once more to make the partition bootable and you should see something like this.
Let’s write those changes to disk. Choose Write, type ‘yes’ and press enter. You should get a message about partition table written to disk.
Now we can quit. And choose the newly created partition as something to install android to.
Choose ext4, when asked, we are sure that we want to format sda1 to ext4, we DO want to install grub AND install system as read only.
Finally let’s reboot.
We are back at the same screen we started at. That’s not good. Go to devices, optical drives and choose “remove disk from virtual drive”. Restart your virtual machine.
Now we should see something else. Like this.
Just press enter here. Wait a moment.
And there you. Android running in a virtual machine!
But we’d hit a snag. Network doesn’t work. Can’t have that now can we. To fix that issue, we need to get down and dirty with some shell scripting *gasp*. Okay, how do we get there from this screen? By pressing CTRL + F1.
Now credit for the next part goes to a guy called cb at innerlife.io comments section. We need to edit a file. How do we do that in android terminal? With a tool from the age when dinosaurs roamed the land and software developers were ranked by the size of their beards. A tool that to this day is still used among geekdom as a measuring contest. It’s called vi. The magic spell to open a proper file goes as follows:
Write that spell and press enter. You should see some magical things.
Alright so by pressing the page down key, hop over all that into the very bottom where there reads “return 0”.
You need to type some things. Hold up! You can’t do that yet. Vi and it’s big brother VIM are a little weird animals when it comes to text editors. They have two modes: command and edit. Command mode is where we are at now. Means we can use the keyboard to tell vi to do something like, copy text, delete entire lines and stuff like that. The edit mode is where we can type words as we usually did. So how do we get into this edit mode? By pressing i.
Now that we can edit, add some lines that prime and config our network to use lan settings.
I’ll try to explain the parts I understand.
The whole set of lines should look similar to this:
ifconfig eth0 192.168.1.111 ndc network create 100 ndc network interface add 100 eth0 ndc network route add 100 eth0 0.0.0.0/0 192.168.1.1 ndc resolver setnetdns 100 localdomain 188.8.131.52 184.108.40.206 ndc network default set 100
What the first line does is tell the android network manager that your ip in this network is now 192.168.1.111. Your’s could be 192.168.0.131, 192.168.0.122, 192.168.8.12 or some such. This depends on the brand of router you have and firmware installed on it.
I have no idea what the second line does. Sounds like it’s creating a network.
Third one most likely connects the aforementioned network into the android network interface.
Fourth one I’m not sure either but I think it tells the interface which ip the interface can get to the outside world.
Fift line tells the interface which dns servers to use. Those unfamiliar with the concept, dns servers are what tell your computer which ip address www.google.com is. Without dns you would need to type the ip address in the address bar instead of www.google.com. Giant like google has several. 220.127.116.11 and 18.104.22.168 are the most well known I think.
Last line sets the network WE created as default so android won’t try using some other networking as default and ruin our internet connection.
Okay so you have written your spells. But how do we get out? Well we need to command vi to write these changes to disk and then exit. We do that in the command mode. How do we get there? By pressing ESC.
In the command mode, we can use letters and symbols to tell vi do all sorts of things. This time let’s command it to write our changes to the file and exit the program. Here’s the spell.
: In here is the prefix that tells vi “I have a command for you”. w Commands vi to “Write” aka save changes. q tells vi to quit. We could write these same instructions separately:
And we should be back at relatively empty black screen. Restart your virtual machine and if all goes well, there is a working network there.
Now how does all this that help us with getting xamarin android application to our virtual machine? With adb connect.
Let’s go the virtual machine with windows. Open command prompt, go to android-sdk folder and platform-tools. There’s adb.exe.
What we can do now is actually connect to the android virtual machine as if it’s just another device in the network.
Do you know that you can do now? You can choose your android virtual machine from the visual studio selection as if it’s another emulator/device and deploy your awesome app there!
And that concludes your tutorial. Hope you had fun. Now go and write some awesome mobile apps.