Android emacs (android-host.el) 기능 추가

AOSP에서 제공되는 Emacs용 Andorid 개발환경을 설정한 이후(이 post 참조) 잘 모르는 LISP을 더듬어 가며 추가한 몇 가지 기능을 소개합니다.

adb reboot (M-x android-adb-reboot)

Module등을 변경한 후에 device를 reset하는 명령어가 없어서 추가했다. 이 기능의 장점을 굳이 꼽자면 shell을 따로 뛰우지 않고 리붓을 할수 있다는거…

(defun android-adb-reboot ()
  "Execute 'adb reboot'."
  (interactive)
  (android-adb-command "reboo

Module push (M-x android-adb-push-module)

Compile한 module을 target에 push 할 때 사용한다. Command를 입력하면 push 할 source와 target을 물어보고 adb push command를 실행한다.

(defun android-adb-push-module (source target)
  "Push specified module into target path."
  (interactive "fSource: \nsTarget: ")

  (android-adb-root)
  (android-adb-remount)
  (android-adb-command
    (concat "push " source " " target) 'p))

Module push Hotkey  (C-x a p)

위의 android-adb-push-module 함수를 쓰다보니 path를 입력하는게 무척 귀찮다. android-compile 명령어를 수행해서 만들어지는 출력 버퍼에서 위의 단축키로 target의 해당 directory로 module을 push한다.

Install: out/target/product/TARGET_DEVICE/system/xxx/yyy.zz

출력 버퍼로 이동해서 출력된 결과물 위에 cursor를 놓고 ‘C-x a p’를 입력하면 그 경로를 읽어서 target으로 push 한다. Source의 path에서 target 위치를 읽어 오게 되어 있어서 특별한 입력이 필요 하지 않다.

(define-key map (kbd "p") 'android-adb-push-module-at-point)
...

(defun android-adb-push-module-at-point ()
  "Push module path at cursor point an push to the target path."
  (interactive)

  (let*
    (
      (sourcepath (concat (android-find-build-tree-root) (thing-at-point 'filename)))
      (targetpath (substring sourcepath (string-match "\/system" sourcepath) nil)))
    (android-adb-push-module sourcepath targetpath)))

전체 수정내역을 포함하는 파일: icon_text_file android-host.el

Cygwin에서 Android systrace 실행 문제

2014년 8월 현재, Cygwin으로 systrace를 사용하려고 하면 zlib 관련한 error가 나면서 동작하지 않는다.

$ python systrace.py -o output.html gfx input view wm am video hal dalvik
capturing trace... done
downloading trace... done

Traceback (most recent call last):
  File "systrace.py", line 286, in <module>
    main()
  File "systrace.py", line 231, in main
    decoded_chunk = dec.decompress(chunk)
zlib.error: Error -3 while decompressing: incorrect header check

AOSP project의 이 링크에 보면 문제를 해결하기 위한 patch가 올려져 있는데 comment를 보니 한동안 받아들여 지지 않다가 현재 source와 달라져서 적용할 수 없게 된 모양이다. 같은 동작을 하도록 약간만 수정해서 이렇게 고쳐 보니 좀 지저분하긴 해도 zlib error 없이 Cygwin에서 systrace를 사용할 수 있었다.

diff --git a/systrace.py b/systrace.py
index 387f413..0d3be25 100644
--- a/systrace.py
+++ b/systrace.py
@@ -205,8 +205,12 @@ def main():
       data = ''.join(data)

       # Collapse CRLFs that are added by adb shell.
-      if data.startswith('\r\n'):
-        data = data.replace('\r\n', '\n')
+      if sys.platform == 'cygwin':
+        if data.startswith('\r\r\n'):
+          data = data.replace('\r\r\n', '\n')
+      else:
+        if data.startswith('\r\n'):
+          data = data.replace('\r\n', '\n')

       # Skip the initial newline.
       data = data[1:]


Emacs에서 Android 개발 환경 사용

AOSP에는 Emacs에서 Android 개발환경을 사용할 수 있도록 몇몇 el file들을 제공하는데, platform  module build를 주로 하게 되는 나로써는 사용에 부족함이 없는 것 같다. Module build와 기본적인 ADB 조작을 지원하는 이 script들의 기능이 부족하다고 느낀다면 Android 개발환경을 보다 적극적으로 지원하는 android-mode와 같은 project를 고려해 보는것도 좋을 것이다.

buildspec.mk 설정

사실 이 기능은 그동안 사용하지 않았었는데, AOSP에서 지원하는 el script들을 사용하려면 설정해 주어야 한다. 간단한 과정을 거쳐 설정해 놓고 보니 shell에서도 사전설정 없이 ‘make target’ command만으로 build를 할 수 있어서 편하다. 대신 ‘cd $OUT’이나 ‘croot’ 같은 이동 command들을 사용할 수 없다는 것은 불편한 점이다.

<ANDROID_ROOT>/build/buildspec.mk.default file을 복사해서 환경에 맞게 수정한 다음 <ANDROID_ROOT> 위치에 놓아두면 build 명령을 내릴때 자동으로 참조된다. 기본적으로 TARGET_PRODUCT, TARGET_BUILD_VARIANT 정도만 수정하면되는데 특이한 build환경을 사용하고 있다면 나머지 항목들은 읽어보고 자신에 맞게 수정하자.

# Choose a product to build for. Look in the products directory for ones
# that work.
ifndef TARGET_PRODUCT
  TARGET_PRODUCT:=product_name
endif

# Choose a variant to build. If you don't pick one, the default is eng.
# User is what we ship. Userdebug is that, with a few flags turned on
# for debugging. Eng has lots of extra tools for development.
ifndef TARGET_BUILD_VARIANT
  TARGET_BUILD_VARIANT:=eng
endif

el file들 복사 및 설정

<ANDROID_ROOT>/development/ide/emacs/ 안에 있는 file들을 Emacs 환경 directory로 복사한 다음 해당 file이 load되도록 init file을 수정한다.

mkdir ~/.emacs.d/android.el
cp <ANDROID_ROOT>/development/ide/emacs/*.el ~/.emacs.d/android.el/ android-common.el android-compile.el android-host.el

다음을 init file에 붙여 넣으면 된다.

; Load Android tools
(add-to-list 'load-path "~/.emacs.d/android.el/")
(require 'android-host)
(require 'android-compile)


사용

편집 중인 file의 module을 build하려면 ‘M-x android-compile’을 입력 하면 된다. 그 외의 ADB에 관련한 명령어 들은 android-host.el script에 설명되어 있다.

;; C-x a a android-adb-root
;; C-x a r android-adb-remount
;; C-x a s android-adb-sync
;; C-x a b android-adb-shell-reboot-bootloader
;; C-x a f android-fastboot-flashall