The Perfect Android Emulator

I’ve recently been getting further into Android development and as most people that have touched the official SDK know, the Android emulator is giant pain in the ass to work with. The reason it is slow is a combination of many things (ARM - x86 conversion, slow screen updates), but fortunately thanks to the Androidx86 project, you don’t have to live with it any more.

Android

The Androidx86 project aims to port AOSP to the x86 architecture and supply drivers for many common devices; it does this very well and is getting better with every release. I’m going to show you how to set up the perfect Android VM for development (this assumes you have the rest of the SDK working fine, including access to adb from the command-line).

To start off we’re going to head over to http://code.google.com/p/android-x86/downloads/list and download the iso labelled as “android-x86-4.0-RC1-eeepc.iso” (181MB). While this is downloading you will want to make sure you have a copy of VirtualBox for your platform of choice.

Once this is finished downloading and VirtualBox is installed, fire up the VirtualBox Manager application and select the “New” button from the toolbar. This launches a wizard to guide you through creating a new VM, pretty simple really.

Give the VM a name of your choice (I simply chose “Android”), select “Linux” as the Operating System, and “Linux 2.6” as the version. Next we’ll want to give the VM some memory. Choose whatever is suitable to your setup, but anything greater than 256MB should suffice. Then we’re on to setting up virtual hard disks, we can safely uncheck the “Start-up Disk” option (as we’re using a livecd) and continue to the next screen. Finally (after dismissing the warnings) you should have a brand new empty VM ready to use.

We aren’t quite ready yet though, lets tweak a couple of options before continuing. Right click on your newly created VM and select “Settings”. From here navigate to the “Storage” category, and click on the “Empty” under “IDE Controller”. On the attributes pane to the right, check the “Live CD/DVD” box, then click on the CD icon and select “Choose a virtual CD/DVD disk file…”. Select the eeepc ISO downloaded earlier.

Android Storage Settings

Next jump down to the “Network category”, click the “Advanced” dropdown, and change the Adapter Type to “PCnet-FAST III”. Now click on the “Port Forwarding” button and add a new entry, setting the Host and Guest port options to 5556. Finally you can close the settings dialog and return to the main screen.

Android Network Settings

Android Port Forwarding Rules

Now we’re going to set up a phone resolution (as by default Android-x86 boots in a conventional tablet resolution). To do this, right click on your VM and select “Show in File Manager” (or “Show in Explorer”). Close the VirtualBox Manager and open the .vbox file in your favourite editor and add the following lines under the <Machine> XML tag:

<ExtraData>
  <ExtraDataItem name="CustomVideoMode1" value="320x480x16" />
</ExtraData>

Save and close this file, and now we’re ready to launch our VM. Open up VirtualBox Manager again and hit start. Once presented with the boot menu, hit the TAB key. Now add UVESA_MODE=320x480 vga=864 and hit enter to boot. Your VM should resize and boot up to a welcome screen. Press Right-ctrl + I to disable “Host Mouse Integration”. Now click inside the VM and work your way through the welcome screen. You can skip pretty much everything that it asks, so feel free to do that.

Once at the home screen, open up the apps menu and scroll across to the right until you find “Terminal Emulator”. Open it up and then type the following commands:

su (this will popup a dialog, hit Allow)
netcfg eth0 dhcp
setprop ro.kernel.android.ndns 1
setprop net.dns1 8.8.8.8
adb tcpip 5556 (or whatever port you used earlier)
exit
exit

Your VM is now set up for network and ADB access. At this point I suggest taking a snapshot of the device by going to the “Machine” menu and selecting “Take Snapshot”. Also remember when powering off the VM to select “Restore current snapshot ‘Snapshot Name’”, so that your VM is always fresh.

If you now open up a command-line on the host, you can type in adb connect 127.0.0.1:5556 and you should successfully connect to your VM. You will have to do this each time you start your VM, but I don’t think it’s a big deal. Running your app within Eclipse will now work as expected and launch inside your VM, at much better speeds than before ;)

Finally, to make this setup perfect, right click on your VM inside the Manager and select “Create Shortcut on Desktop”. Now you should be able to start a fresh Android VM in well under 10 seconds (takes about 3 on my PC) and be ready for development!

Comment on this article

LogPixels DPI hack

Here’s a handy tip for those netbook users out there in dire need of more screen estate. You can force a DPI value of lower than 96 by using the Windows registry. To do so open regedit, navigate to HKEY_CURRENT_CONFIG/Software/Fonts/LogPixels and change the decimal value from 96 to something lower such as 80. Then log out and back in and you will see the changes. I’m using a value of 80 on my netbook and it is about the most that I will drop it while still keeping things (half) readable. You will notice that it really hinders the readability of fonts as well as having some strange bitmap scaling artifacts, but if you’re like me and can adjust to it then the extra screen space gained is well worth it :)

You can view a side by side comparison of the my desktop with LogPixels at 80 and at 96 by viewing this imgur album.

http://i.imgur.com/DKnsWm.jpg http://i.imgur.com/JHrJim.jpg

Comment on this article

Docstrings in my Chicken?

Here’s a little bit of amateur code to enable the use of docstrings in Chicken Scheme. I haven’t written any Scheme in a few months and don’t have any formal training in the ninja art of explicit renaming macros so I’d appreciate any feedback from Scheme veterans:

;;; Docstrings for Chicken!

;; The hash-table that will contain the procedures and their associated
;; documentation strings as well as their code for later use.
(define *documentation-hash-table* (make-hash-table))

;; Documented define er-macro
(define-syntax (define* form r c)
  (let* ((args-form (second form))     ; (proc args)
         (proc-name (first args-form)) ;  proc
         (doc (third form))            ; "The docstring"
         (body (drop form 3)))         ; (everything else)
    (hash-table-set! *documentation-hash-table* proc-name (list doc body)) ; insert the doc and code body
    `(,(r 'define) ,args-form ,@body))) ; Define the procedure as normal

;; Example of a documented procedure, will print a nice-ish list of documented procedures
(define* (print-docs)
  "Print a list of documented procedures"
  (hash-table-walk *documentation-hash-table*
    (lambda (key val)
      (printf "~a => ~a~n  ~a~n~n" key (first val) (second val)))))

Another example of why Scheme is such a fun language to work with :)

Comment on this article

Twitter's Bootstrap

To me, one of the most annoying parts of prototyping a new web application is getting a presentable look and feel up quickly. When I’m prototyping stuff I love to have a good looking interface from the start, even if that interface will be changed before going live.

Recently, Twitter released a wonderful CSS/JS framework named Bootstrap for this exact purpose. As stated it is a toolkit “[…] designed to kickstart development of webapps and sites.”. It combines an aesthetically pleasing base styling (based somewhat off of Twitter itself) of HTML elements with an easy to use CSS grid, great browser compatibility, and various optional Javascript modules to extend functionality of things such as Drop-down menus and modal dialogs.

As for the elements it offers, some of my favourites are:

  • All typical HTML elements are styled, including decent typography
  • A top bar to be used for menus and such, with drop-down menus to be activated easily with JS
  • Tabs/Pills
  • Ability to pretty-print code when used in conjunction with google-code-prettify
  • Labels and buttons, with styles for success, warning, notice, etc
  • Alert boxes, with corresponding styles, great for showing form submission status etc

Bootstrap is built on top of the LESS CSS preprocessor for ease of extending and integrating into your apps. Personally I found that the precompiled stylesheet was more than enough to begin with however, and I’m more of a fan of the SASS preprocessor anyway. The JS modules are also very easy to integrate with very little code needed to hook them up to their appropriate classes.

I jumped into Bootstrap shortly after its release, and have been using it ever since for my internal prototypes and eventual applications, including an application in the process of being deployed. Bootstrap played an integral part in getting that project complete under it’s tight deadline.

Highly recommended to all!

Comment on this article

Das Keyboard Ultimate

I took the plunge recently and bought my first mechanical keyboard, the Das Keyboard Ultimate. It was very hard to find someone that would ship one to NZ, but eventually I found AusPCMarket that ships the Das as well as several other “clicky keyboards”. The Das is known for being a bit pricey.. the Ultimate set me back $230NZD (incl shipping and GST) at the current exchange rate and was delivered in just over a week. If you need to justify the price by thinking of it as an investment, then do :)

With it being my first mechanical keyboard I’m unable to give a fair review in comparison to others of its kind. My previous keyboard was a Microsoft Comfort Curve 2000 keyboard (a much cheaper $40NZD), a brilliant keyboard but of course based around rubber dome technology.

Anyway, it took me a while to adjust to the non-curved layout of the Das, but now that I have I feel that I am touch-typing much MUCH more efficiently than before. It feels great to type on, plus it sounds amazing! Personally I revel in the sound of the Das, and while I wouldn’t want to use one around sleeping people, it certainly won’t break through any music playing on your headphones and so far doesn’t seem to penetrate walls ;)

Overall a definite thumbs-up from me. If you’re in NZ and looking to get a Das, I’d highly recommend also going through AusPCMarket as their service was quick and no-bullshit.

Since posting this MightyApe.co.nz has become an official Das Keyboard stockist, so NZers can save a few bucks by grabbing it from there. I also managed to pour coffee through my Das very shortly after getting it, but after flushing it out with water it is still clicking away like nothing ever happened, WIN!.

Comment on this article

Tiny Memcached Wrapper

No posts in a while huh! Been pretty busy and haven’t thought of much to blog about lately. But I’ve been playing with Memcache and wanted to share a tiny class I made to add easy caching to my PHP projects. This PHP class just provides a nice simple interface to use by extending the Memcached class:

<?php
// Call the class whatever you want
class MyCache extends \Memcached {
    function cache($key, $time, $func) {
        if (($val = $this->get($key)) === false) {
            $val = $func();
            $this->set($key, $val, strtotime($time) - time());
        }
        return $val;
    }
}

And now to use it, first you summon it in the same way as the regular class:

<?php
$c = new MyCache;
$c->addServer('127.0.0.1', 11211);

Then where you would like to begin caching (I’m using this very blog as an example, where GeSHi for syntax highlighting is the main bottle-neck for page generation), you call the class like so:

<?php
// Example using my BLACK blog engine (http://github.com/ZaneA/BLACK)

// Here is the original line
$row['body'] = render($row['body']);

// And here is a cached version
// As you can see we have a KEY, a TIMEOUT, and a FUNCTION to call for a cache miss
$row['body'] = $c->cache("blog_post_{$row['id']}", '30 minutes', function () use ($row) {
    return render($row['body']); // Renders all the post filters, including syntax highlighting
});

Awesome, for this small tweak on my local test blog (with 4 posts), this takes the page response time down from 14ms to 2ms. Quite a win I’d say :)

Of course this code requires the memcached PHP extension and a running Memcache server ;)

Comment on this article