Thursday, 7 November 2013

Getting started with IntelliJ, Mac OS X and Android 4.3 (18)

This should act as a quick getting-started guide to creating and editing Android apps on a Mac using IntelliJ.

Note: This article assumes that you have the latest version of Java installed through the normal Mac OS X installation method. This was written as Java version 1.6.0_51 was the most up-to-date version of Java released by Apple.

It also describes some solutions to frequently seen problems.

It will also demonstrate how to install HAX so we get a faster emulator.

Getting ready...

Before we start, download:

Install IntelliJ by copying the application over to your Applications/ folder

To install the Android SDK first cd to the directory you copied the sdk to, it should be called something like android-sdk-macosx. From there, cd tools and finally ./android. This will bring up the installation window, as shown below.

Select the files needed to complete the Android 4.3 (API 18) installation, and then let the SDK download them.

Once completed, it's time to get HAX installed so that we can run the resulting application at more than a snail's pace. To do this, run the appropriate installer from the site linked above, depending on which version of OS X you're using.

Creating the project...

Time to head into IntelliJ and get the environment set up so that we can develop.

Create New Project

Select Android | Application Module from the left, and give the project a name. Select the location to store the files, then click New... next to the Project SDK field. Locate the folder you downloaded the Android SDK to (the one which was originally named something like android-sdk-macosx) and select Choose. Finally, a pop-up should appear asking for you to select the appropriate Java JDK and Android SDK you wish to use. Select Java SDK 1.6 and Build target Android 4.3. You should end up with a New Project window that looks something like the following.

Press Next and allow for folders, etc. to be generated. Allow IntelliJ to generate the "Hello, World!" activity and selected Emulator as the Target Device. Do not worry that nothing is selected in the Prefer Android Virtual Device dropdown at this point.

You should now have a fully armed and operational battle station IDE, from which you can develop your application. So, let's set up the emulator so that we can see our new Android application in action. From the Run dropdown, select Edit Configurations... and create a new Android Application. Check that the correct Activity is selected to Launch (if following through using the default application this should simply be called MyActivity), and ensure that the Target Device is set to Emulator. Next, click the elipses (...) next to Prefer Android Virtual Device, and this will bring up a window entitled Android Virtual Device Manager. From here click New... and generate your Android device.

Give your device a name, then select the physical device that it will represent. You'll want to select as the Target the Android 4.3 - API Level 18, and as the CPU/ABI make sure you select Intel Atom(x86). Finally, it's best to give the machine 200MiB of SD Card space (see the list of common issues below). To make sure that the device starts up from scratch each time it's booted, ensure that the Snapshot checkbox is not ticked.

The finished window should look something like the following:

Finally, check that the virtual device you just generated is selected in the Prefer Android Virtual Device dropdown.

At this point you should be good to start up your emulator. Once the boot splash screen has left, and the lock screen is facing you, simply unlock the phone, and your new application should be staring you in the face.

Common Issues


Alter the uses-sdk element of your AndroidManifest.xml file. Ensure that its target and minimum are something appropriate, for example: <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18"/>


Alter the AndroidManifest.xml file so that the manifest tag contains the attribute android:installLocation="preferExternal". You will also need to ensure that the emulated device within the Edit Endroid Virtual Device application has an SD Card with appropriate size installed, as shown below.

The emulator crashes immediately on boot with message "emulator64-x86 quit unexpectedly"

This occurs when the location to launch the emulation window isn't a value that the application understands. Generally when its been pulled across to a monitor that isn't the primary display. To resolve the issue, locate the file: ~/.android/avd/.avd/emulator-user.ini and alter the values window.x and window.y to both be 0.