2011-01-25

Use Emacs/JDEE in Android Development

Eclipse/ADT is a great tool for Android development. But for people using Emacs, it is difficult to get used to it. Fortunately, JDEE is easy to be customized to work on Android development. Here are my settings on my MacBookPro,

;; Load JDEE
(require 'jde)

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(jde-global-classpath (quote ("~/android/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar" "~/android/out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar" "~/android/out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes.jar" "~/android/out/out/target/common/obj/JAVA_LIBRARIES/android.policy_intermediates/classes.jar")))
 '(jde-jdk-registry (quote (("1.5.0" . "/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0"))))
 '(jde-sourcepath (quote ("~/android/source/frameworks/base/services" "~/android/source/frameworks/base/core/java" "~/android/source/dalvik/libcore/luni/src/main/java")))

Here, I have android source code in ~/android/source and build in ~/android/out. If you are using SDK, you should add android.jar in SDK to jde-global-classpath. Also, you can put these settings into a per-project file prj.el in your project top directory for JDEE to load.

For example, my prj.el in one of my project looks like,
(jde-project-file-version "1.0")

;; Reset class path and source path
(jde-set-variables
 '(jde-project-name "openalarm-android")
 '(jde-gen-buffer-boilerplate
   '("/**"
     "*  OpenAlarm - an extensible alarm for Android"
     "*  Copyright (C) 2010 Liu Yen-Liang (Josh)"
     "*"
     "*"
     "*  This program is free software: you can redistribute it and/or modify"
     "*  it under the terms of the GNU General Public License as published by"
     "*  the Free Software Foundation, either version 3 of the License, or"
     "*  (at your option) any later version."
     "*"
     "*  This program is distributed in the hope that it will be useful,"
     "*  but WITHOUT ANY WARRANTY; without even the implied warranty of"
     "*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
     "*  GNU General Public License for more details."
     "*"
     "*  You should have received a copy of the GNU General Public License"
     "*  along with this program. If not, see ."
     "*/"))
 '(jde-log-max 5000)
 '(jde-enable-abbrev-mode t)
 ;; Class path for browsing files and generate code templates
 '(jde-global-classpath
   (quote ("$ANDROID_SDK_ROOT/platforms/android-7/android.jar"
           "$HOME/projects/openalarm-android/bin")))
 '(jde-sourcepath
   (quote ("$HOME/projects/openalarm-android/src"
           "$HOME/android/eclair_21/frameworks/base/core/java")
          ))
 '(jde-compile-option-directory "$HOME/projects/openalarm-android/bin") 
 '(jde-complete-function (quote jde-complete-menu)) 
 '(jde-run-working-directory "$HOME/projects/openalarm-android/bin")
 '(jde-help-docsets
   (quote (("Android SDK Doc" "$ANDROID_SDK_ROOT/docs/index.html" nil))))
 '(jde-build-function (quote (jde-ant-build)))
 '(jde-ant-args "-emacs")
 '(jde-ant-complete-target t)
 '(jde-ant-enable-find t) 
 '(jde-ant-read-args nil)
 '(jde-ant-read-buildfile nil)
 '(jde-ant-read-target nil)
 '(jde-ant-use-global-classpath nil)
 '(jde-ant-working-directory "$HOME/projects/openalarm-openalarm/bin/") 
 '(jde-built-class-path (quote ("$HOME/projects/openalarm-android/bin")))
 )
These settings enable us using method completion, source browsing, etc. But, I am still experimenting if we can build android target in JDEE.

For MacOS users: the environment variables must be defined in ~/.MacOSX/environemtn.plist