An Android automake script

Fred Medlin | programming | Saturday, May 8th, 2010

For Android NDK development, it was getting a little irritating bouncing back and forth from Eclipse to Textmate then to a terminal to build an NDK shared library.

There are probably other alternatives, but I was wishing there was something like autotest available for Ruby and Rails projects. Even though it isn’t really enforcing TDD by running any tests, it saves tons of keystrokes while you’re tweaking your NDK project.

I call this little ruby script robomake. It should be run from the ndk directory. It watches the source files and when a change is detected, it runs the make which will build and install the given NDK project.

If you ‘chmod +x’, remove the .rb extension and place it in your executable path, it can run like this.

robomake hello-jni

where hello-jni is the name of an NDK project under the apps folder. The script will run ‘make APP=hello-jni’ each time a dependent file is changed. Run it from the root NDK folder where you would normally do your make.

#! /usr/bin/ruby
# A script to watch files and build Android NDK projects

interrupted = false
trap("INT") { interrupted = true }

def print_separator
  puts "============================================="
end

extensions = ["mk", "cpp", "h"]

if ARGV.size < 1
  puts "Usage: robomake.rb <app>"
  exit 1
end

app = ARGV.shift
app_folder = "apps/#{app}"
application_mk = "#{app_folder}/Application.mk"
command = "make APP=#{app}"

unless File.exists?(application_mk)
  puts "#{application_mk} does not exist"
  exit 1
end

jni_path = ""
File.open(application_mk) do |file|
  while (line = file.gets)
    split = line.split(':=')
    if "#{split[0].rstrip}" == "APP_PROJECT_PATH"
      jni_path = split[1].strip << "/jni/**"
      break
    end
  end
  file.close
end

files = {}
extensions.each { |ext|
  Dir[jni_path + "/*.#{ext}"].each { |file|
    files[file] = File.mtime(file)
  }
}

system(command)
print_separator
loop do
  sleep 1
  if interrupted
       exit
  end

  changed_file, last_changed = files.find { |file, last_changed|
    File.mtime(file) > last_changed
  }

  if changed_file
    files[changed_file] = File.mtime(changed_file)
    puts "=> #{changed_file} changed, running #{command}"
    system(command)
    print_separator
  end
end

view raw robomake.rb This Gist brought to you by GitHub.

Powered by WordPress | Thanks to Roy Tanck for the theme