Tips on Android Development Using Emacs

5138860024_ab3d23b72d_android-computer

Android using Emacs

For the intrepid Emacs user, here are some tips for doing Android development without Eclipse. I am working on a follow-up post with some general Android SDK tips.

Emacs prerequisites

  1. android-mode
    This mode gives you the following interactive commands:

    • M-x android-start-emulator Start the Android SDK emulator from emacs
    • M-x android-start-ddms Start the Android debugger
    • M-x android-logcat Like syslog for Android
    • M-x android-ant Run any ant task in the current project directory
  2. android.el
    This is a file included in the Android SDK. It duplicates some of the functionality of android-mode, but adds M-x android-jdb, which starts the JDB debugger once you have DDMS running.Load it from {SDK dir}/tools/lib
  3. JDE
    Optional, but it’s great for java code. Required to use beanshell shortcuts like the import statement command below (Generate and insert import statements)
  4. Typical emacs setup cruft
    Here’s what I have in my init file for Android:

    (add-to-list 'load-path "~/emacs/android-mode")
    	    (require 'android-mode)
    	    (setq android-mode-sdk-dir "~/work/android/android")
    	    (add-hook 'gud-mode-hook
                (lambda ()
                (add-to-list 'gud-jdb-classpath "/home/gregj/work/android-sdk-linux_86/platforms/android-7/android.jar")
                ))

Launch the emulator

Although you can start the emulator from within emacs using android-mode: M-x android-start-emulator

I prefer to launch from the shell, since this allows me to exit emacs without having to restart the emulator, e.g.:

emulator -avd starter-21 -partition-size 128 >/dev/null &

Create a project

Instead of using the New Project wizard in Eclipse, use these steps to create an Android project using the shell:

cd {project directory}
	mkdir HelloAndroid
	cd HelloAndroid
	android create project --name HelloAndroid --target android-7 --path . --package home.hoochiepep.HelloAndroid --activity HelloAndroid

Change =–target=, =–package=, and =–activity= parameters as appropriate.

To get a list of valid targets for your SDK, use

android list

Specify the application name

To change the name of the application, edit {project root}/res/values/strings.xml:

<string name="app_name">Hello, Android</string>

Specify a minimum SDK level

The Eclipse New Project wizard includes a Min SDK Version field. To set this without the wizard, add a line to AndroidManifest.xml as a child of manifest:

<uses-sdk android:minSdkVersion="2" />

Make an eclipse project ant-ready (do also when moved to a new machine)

Projects you pull from online will have been created using Eclipse, and will not have a build.xml file for ant.

Another common problem is that the file local.properties will be missing. Running an update as shown here will fix both problems:

android update project --path . --target android-7 --subprojects

Generate and insert import statements

You will see references to a handy Eclipse command (invoked by Control-Shift-O) which adds import statements for referenced classes.

The emacs equivalent is found in jde-mode: M-x jde-import-find-and-import (C-c C-v C-z). Make sure the classpath is set correctly prior to starting jde-mode

Use the debugger within Emacs

  • Install the app on the emulator using M-x android-ant-install (the ‘install’ target uses the debug apk)
  • Start ddms. There are no cmdline args, so you might as well use M-x android-start-ddms
  • In the emulator, go to Dev Tools -> Development Settings and select the app as the debug app
  • In the emulator, start the app
  • Look in the ddms window for the app’s debug port (usu. 8700 if the Dev Settings step was done)
  • Start jdb by invoking M-x android-jdb from android.elYou can also start jdb directly using the following command line params as a guide
    jdb -sourcepath/home/gregj/work/android/projects/NotepadCodeLab/Notepadv3/src -attach localhost:8700

    Set breakpoints in JDB, like so

    stop in com.android.demo.notepad3.NoteEdit.onResume

    If the project directory is set correctly in the jdb command, you will be able to set breakpoints by line using jde-mode’s M-x gud-break (C-x space).

Posted in Android, Development, emacs Tagged with: , , , , ,

10 Tips for Powerful Emacs on Windows

9521997739_538e219fcd_windows-freedom

I avoided using Microsoft Windows for almost 15 years, but with my
new job at a Microsoft-enthralled development shop, those idyllic
days have come to an abrupt end. Because in the past I could always
use my trusty Linux and OS X machines, I never did push past the
hurdles of using Emacs on Windows. My utter reliance on Org-mode,
TRAMP, and Ediff (to name a few) made it essential to get Emacs
working on Windows if at all possible.

Now I am using Emacs on Windows XP and Windows 7 on a daily basis,
and am quite happy with the results. It was not easy to get to the
point of full functionality, so I wanted to share the magic that
makes it all work.
Read more ›

Posted in emacs Tagged with: , , , , ,

Snippets with Emacs Lisp Power

The YASnippet package for Emacs has some pretty awesome power for the developer, especially when you utilize the power of Emacs Lisp.

YASnippet was inspired by TextMate, which was inspired by Emacs, in a highly-out-of-equilibrium whirlwind of self-referential creativity.

The screencast above uses a set of snippets I originally took from this WordPress TextMate bundle, created by Shawn Parker and Gordon Brander

The first snippet in the screencast illustrates YASnippet’s mirrors with transformation, in a WordPress plugin template. The snippet calls Emacs Lisp functions as the plugin name is filled in to create the plugin URI, the “namespace” (used here as a prefix for function and variable names), and the primary class name for the plugin.

The second snippet writes a function skeleton, then calls Emacs Lisp at the end to move the generated function outside the current scope into a correct position in the file. This snippet uses YASnippet’s fields with transformations syntax, but to do a sneaky thing: not transform the field, but move a region of generated text!

Following is the code for the snippets shown in the screencast, with no commentary. So pipe up in the comments if you’re curious about how something works!

# -*- mode: snippet -*-
# name: WP Plugin
# key: plugin
# --
/*
Plugin Name: ${1:Plugin Name}
Plugin URI: http://${2:dynapse.com/plugins/}${1:$(gjg/sanitize text)}/
Description: ${3:Description}
Version: ${4:1.0}
Author: ${5:Gregory Grubbs}
Author URI: http://${6:gregorygrubbs.com/}
Namespace: ${1:$(gjg/acronyminize text)}_
*/
 
class ${1:$(gjg/whitespace-to-underscore text)} {
	/**
	 * constructor for $1
	 *
	 * The constructor is responsible for registering all hooks used
	 * by this class as as WordPress plugin
	 */
	function __construct() {
		 $0
	} // constructor
 
}
$${1:$(gjg/acronyminize text)} = new ${1:$(gjg/whitespace-to-underscore text)}();

Next, the add_action snippet, which moves a generated function at the end:

# -*- mode: snippet -*-
# name: add_action
# key: add_action
# --
add_action('${1:init}', array($this, 'my_${2:$1}'));
${3:$$(gjg/move-next-sexp-past-current-scope)}
function my_$1 () {
}

And finally, (some of) the Emacs Lisp functions that the snippets call:

(defun gjg/acronyminize (text &optional do-capitalize)
  "Make an acronym from the text 
do-capitalize: t means run text through capitalize function, nil will respect CamelCase
"
  (save-excursion
    (setq case-fold-search nil)
    (downcase
     (replace-regexp-in-string
      "[^A-Z]" ""
      (if do-capitalize (capitalize text) text) nil t))))
(defun gjg/move-next-sexp-past-current-scope ()
  "kill sexp following point, move past current scope/sexp/function"
  (beginning-of-line)
  (let ((beg (point)))
    (re-search-forward "^[ \t]*function[ \t]+[^}]+?}" (point-max) nil)
    (mark-defun)
	(kill-region (point) (mark)))
  (forward-line)
  (yank)
  (indent-region (mark) (point)))
Posted in Development, emacs, wordpress Tagged with: , , , , , , , , , , ,

FAST file access with Emacs and ido-mode

One of the things that makes daily Emacs use so enjoyable is the availability of brilliant add-ons designed to make you work faster.

Emacs with ido-mode fuzzy matching (or flex matching) makes it incredibly quick to navigate the file system using only the keyboard. But it does far more than that, allowing the emacs pilot to quickly find help, commands, variables and much more. This video shows the finer points of using ido-mode with flex matching.

It’s all in the video – enjoy.

Here are my current settings for ido-mode

;; do not confirm a new file or buffer
(setq confirm-nonexistent-file-or-buffer nil)
(require 'ido)
(ido-mode 1)
(ido-everywhere 1)
(setq ido-enable-flex-matching t)
(setq ido-create-new-buffer 'always)
(setq ido-enable-tramp-completion nil)
(setq ido-enable-last-directory-history nil)
(setq ido-confirm-unique-completion nil) ;; wait for RET, even for unique?
(setq ido-show-dot-for-dired t) ;; put . as the first item
(setq ido-use-filename-at-point t) ;; prefer file names near point
Posted in Development, emacs Tagged with: , , , ,