2011-10-18

Getting started with IntelliJ to write Android applications on Linux

This blog post gives instructions to get started with Android application development with IntelliJ on Linux.
  1. Install the Java runtime environment (JRE) and the Java compiler (in the JDK). Command to to it on Ubuntu Lucid: sudo apt-get install openjdk-6-jdk . Please note that the JRE without the JDK is not enough, IntelliJ needs the JDK.
  2. You will need about 1.5 GB of free disk space.
  3. Download IntelliJ (either the Ultimate edition or the Community edition; please note that you have to pay for the Ultimate edition after the evaluation period expires) from from. http://www.jetbrains.com/idea/download/ . I've downloaded the file from http://download.jetbrains.com/idea/ideaIU-10.5.2.tar.gz , it was about 160 MB.
  4. Download the Android SDK tools from http://developer.android.com/sdk/ . I've downloaded the file from http://dl.google.com/android/android-sdk_r13-linux_x86.tgz . It was about 160 MB.
  5. If you ever want to connect a real phone via USB, then follow the instructions http://ptspts.blogspot.com/2011/10/how-to-fix-adb-no-permissions-error-on.html do create and install the android.rules udev rule. Please also restart the udev service.
  6. Extract the downloaded archive android-sdk_r13-linux_x86.tgz .
  7. Run the android-sdk-linux_x86/tools/android tool.
  8. On the Available packages tab, find and install the following packages:
    • Android SDK Tools
    • Android SDK Platform-tools
    • Documentation for Android SDK (the latest one)
    • SDK Platform Android 2.2, API 8 (or whichever Android version you are developing for).
    • Samples for SDK API 8 (or whichever Android version you are developing for).
    • Google APIs by Google Inc., Android API 8 (or whichever Android version you are developing for).
    • Android Compatibility package
  9. In the Virtual devices tab, create a virtual device. Use these settings (make sure to enabling snapshots and disabling audio playback and recording):
  10. Start the virtual device, play with it (it's an emulated Android phone), watch how it eats your CPU capacity, and stop it by closing the emulator window.
  11. Please note that the emulator is very slow. It will happily eat 100% even if the virtual device is idle. When you disable sound playback and sound recording, it still eats about 800 MHz (tested on Intel(R) Core(TM)2 Duo CPU P9500 @ 2.53GHz).
  12. A possible bug in the Android emulator: The Launch from snapshot functionality didn't work for me in the emulator, even though the snapshot was present.
  13. In the file ~/Downloads/idea-IU-107.587/bin/idea.sh (the actual filename may depend on where you have downloaded and extracted IntelliJ to), change the line containing OPEN_JDK=$? to OPEN_JDK=1 . This will disable the startup warning.
  14. Start IntelliJ. Enter license data (or choose evaluation), accept the license agreement, just click OK in the Select VCS Integration dialog (or select a superset of the version control systems you are planning to use), unselect all Web/JavaEE plugins (they are not needed for Android development), unselect all the HTML/JavaScript plugins, in the Other plugins dialog select at least these: Android, GenerateToString, Inspection Gadgets, Intention Power Pack, JUnit, Remote Hosts Access, SpellChecker, Structural Search, Task Management, Type Migration; make sure Android is checked; finish the installation.
  15. File / New project. Create project from scratch. Next. Name: afirst. Select type: Android Module. Next. Create source... src. Next. Project JDK / Configure. /usr/lib/jvm/java-6-openjdk . OK. Next. Android SDK: ... . In the top left corner of the window, click +, and select Android SDK. Specify /home/USERNAME/Downloads/android-sdk-linux_x86 (where you have downloaded and extracted the Android SDK to). Select internal JDK: 1.6. OK. Select build target: Android 2.2 (or the Android version of your choice). OK. OK. Now you are back in the Create project wizard, with the Android SDK: selection containing Android 2.2 Platform. Finish.
  16. Wait a few minutes until the project tree afirst appears. Open afirst. Open src. Open com.example. Double click on MyActivity. The MyActivity.java source file appears.
  17. Make sure the virtual device barvirt is running in the Android emulator. Play it safe and restart the emulator.
  18. Stop adb: sudo ~/Downloads/android-sdk-linux_x86/platform-tools/adb kill-server
  19. Restart adb: sudo ~/Downloads/android-sdk-linux_x86/platform-tools/adb devices
  20. In IntelliJ Run / Edit configurations, set up devices and virtual devices like this:
  21. In IntelliJ: Run / Run. Wait a minute. The app should start on the emulator.
  22. Connect your phone with USB debugging enabled (in Settings / Applications / Development). In IntelliJ: Run / Run. Wait a minute. The app should start on the phone.

Typical IntelliJ message for starting the app on the phone:

Waiting for device.
Target device: 1234567890ABCDEF
Uploading file
 local path: /home/pts/IdeaProjects/afirst/out/production/afirst/afirst.apk
 remote path: /data/local/tmp/com.example
Installing com.example
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example"
pkg: /data/local/tmp/com.example
Success


Launching application: com.example/com.example.MyActivity.
DEVICE SHELL COMMAND: am start -n "com.example/com.example.MyActivity"
Starting: Intent { cmp=com.example/.MyActivity }

Typical IntelliJ message for starting the app on the emulator:

Waiting for device.
Target device: emulator-5554 (barvirt)
Uploading file
 local path: /home/pts/IdeaProjects/afirst/out/production/afirst/afirst.apk
 remote path: /data/local/tmp/com.example
Installing com.example
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example"
pkg: /data/local/tmp/com.example
Success


Launching application: com.example/com.example.MyActivity.
DEVICE SHELL COMMAND: am start -n "com.example/com.example.MyActivity"
Starting: Intent { cmp=com.example/.MyActivity }

No comments: