Building-for-Android.xhtml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Building for Android</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Android-view-construction.xhtml" title="Android view construction"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Building for Android" epub:type="subchapter" id="Building-for-Android">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Building for Android</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Google’s phone/tablet operating system
  23. <a class="ulink" href="https://developers.google.com/android/" target="_top">Android</a>
  24. is based on a custom virtual machine on top of a Linux kernel.
  25. Even though Android isn’t strictly (or legally) speaking Java,
  26. you can build Android applications using Kawa.
  27. </p>
  28. <p>Below is "Hello world" written in Kawa Scheme. A slightly
  29. more interesting example is in <a class="link" href="Android-view-construction.xhtml" title="Android view construction">next section</a>.
  30. </p>
  31. <pre class="screen">(require 'android-defs)
  32. (activity hello
  33. (on-create-view
  34. (android.widget.TextView (this)
  35. text: "Hello, Android from Kawa Scheme!")))
  36. </pre>
  37. <p>The following instructions have been tested on GNU/Linux,
  38. specifically Fedora 17.
  39. <a class="ulink" href="http://asieno.com/blog/index.php/post/2012/08/16/Setting-up-the-environment-Android-Kawa" target="_top">This link</a> may be helpful if you’re building on Windows.
  40. </p>
  41. <section class="sect2" title="Downloading and setting up the Android SDK" epub:type="division" id="idm139667869672672">
  42. <div class="titlepage">
  43. <div>
  44. <div>
  45. <h3 class="title">Downloading and setting up the Android SDK</h3>
  46. </div>
  47. </div>
  48. </div>
  49. <p>First <a class="ulink" href="http://code.google.com/android/download.html" target="_top">download the Android SDK</a>. Unzip in a suitable location, which we’ll refer to as <code class="literal">ANDROID_HOME</code>.
  50. </p>
  51. <pre class="screen">export ANDROID_HOME=/path/to/android-sdk-linux
  52. PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
  53. </pre>
  54. <p>Next you have to get the appropriate platform SDK:
  55. </p>
  56. <pre class="screen">$ android update sdk
  57. </pre>
  58. <p>You need to select an Android “platform”.
  59. Platform (API) 16 corresponds to Android 4.1.2 (Jelly Bean).
  60. Select that or whatever you prefer, and click <code class="literal">Install</code>.
  61. (You can install multiple platforms, but each project
  62. is built for a specific platform.)
  63. </p>
  64. <pre class="screen">ANDROID_PLATFORM=android-16
  65. </pre>
  66. </section>
  67. <section class="sect2" title="Building Kawa for Android" epub:type="division" id="idm139667869666944">
  68. <div class="titlepage">
  69. <div>
  70. <div>
  71. <h3 class="title">Building Kawa for Android</h3>
  72. </div>
  73. </div>
  74. </div>
  75. <p>Set <code class="literal">JAVA_HOME</code> to where your JDK tree is.
  76. You should use JDK 6; JDK 7 does not work at time of writing.
  77. </p>
  78. <pre class="screen">$ export JAVA_HOME=/opt/jdk1.6
  79. </pre>
  80. <p>First <a class="link" href="Getting-Kawa.xhtml" title="Getting Kawa">get the Kawa source code</a>.
  81. </p>
  82. <p>If using Ant (as is recommended on Windows):
  83. </p>
  84. <pre class="screen">$ ant -Denable-android=true
  85. </pre>
  86. <p>Alternatively, you can use <code class="literal">configure</code> and <code class="literal">make</code>:
  87. </p>
  88. <pre class="screen">$ KAWA_DIR=path_to_Kawa_sources
  89. $ cd $KAWA_DIR
  90. $ ./configure --with-android=$ANDROID_HOME/platforms/$ANDROID_PLATFORM/android.jar --disable-xquery --disable-jemacs
  91. $ make
  92. </pre>
  93. </section>
  94. <section class="sect2" title="Creating the application" epub:type="division" id="idm139667869660736">
  95. <div class="titlepage">
  96. <div>
  97. <div>
  98. <h3 class="title">Creating the application</h3>
  99. </div>
  100. </div>
  101. </div>
  102. <p>Next, we need to create a project or “activity”.
  103. This tutorial assumes you want to create the project
  104. in the target directory <code class="literal">KawaHello</code>,
  105. with the main activity being a class named <code class="literal">hello</code> in a
  106. package <code class="literal">kawa.android</code>:
  107. </p>
  108. <pre class="screen">PROJECT_DIR=KawaHello
  109. PROJECT_CLASS=hello
  110. PROJECT_PACKAGE=kawa.android
  111. PROJECT_PACKAGE_PATH=kawa/android
  112. </pre>
  113. <p>To create the project use the following command:
  114. </p>
  115. <pre class="screen">$ android create project --target $ANDROID_PLATFORM --name $PROJECT_DIR --activity $PROJECT_CLASS --path ./$PROJECT_DIR --package $PROJECT_PACKAGE
  116. </pre>
  117. <p>Replace the skeleton <code class="literal">hello.java</code> by the Scheme code at the
  118. top of this note, placing in a file named <code class="literal">hello.scm</code>:
  119. </p>
  120. <pre class="screen">$ cd $PROJECT_DIR
  121. $ HELLO_APP_DIR=`pwd`
  122. $ cd $HELLO_APP_DIR/src/$PROJECT_PACKAGE_PATH
  123. $ rm $PROJECT_CLASS.java
  124. $ <span class="emphasis"><em>create</em></span> $PROJECT_CLASS.scm
  125. </pre>
  126. <p>We need to copy/link the Kawa jar file so the Android SDK can find it:
  127. </p>
  128. <pre class="screen">$ cd $HELLO_APP_DIR
  129. $ ln -s $KAWA_DIR/kawa-2.92_invoke.jar libs/kawa.jar
  130. </pre>
  131. <p>Optionally, you can use kawart-2.92_invoke.jar, which is slightly smaller,
  132. but does not support eval, and does not get built by the Ant build:
  133. </p>
  134. <pre class="screen">$ ln -s $KAWA_DIR/kawart-2.92_invoke.jar libs/kawa.jar
  135. </pre>
  136. <p>Copy or link <code class="literal">custom_rules.xml</code> from the Kawa sources:
  137. </p>
  138. <pre class="screen">ln -s $KAWA_DIR/gnu/kawa/android/custom_rules.xml .
  139. </pre>
  140. <p>Finally to build the application just do:
  141. </p>
  142. <pre class="screen">$ ant debug
  143. </pre>
  144. </section>
  145. <section class="sect2" title="Running the application on the Android emulator" epub:type="division" id="idm139667869650560">
  146. <div class="titlepage">
  147. <div>
  148. <div>
  149. <h3 class="title">Running the application on the Android emulator</h3>
  150. </div>
  151. </div>
  152. </div>
  153. <p>First you need to create an <a class="ulink" href="http://developer.android.com/tools/devices" target="_top">Android Virtual Device (avd)</a>. Start:
  154. </p>
  155. <pre class="screen">android
  156. </pre>
  157. <p>Then from menu <code class="literal">Tools</code> select <code class="literal">Manage AVDs...</code>.
  158. In the new window click <code class="literal">New....</code>
  159. Pick a <code class="literal">Name</code> (we use <code class="literal">avd16</code> in the following),
  160. a <code class="literal">Target</code> (to match <code class="literal">$ANDROID_PLATFORM</code>),
  161. and optionally change the other properties, before clicking <code class="literal">Create AVD</code>.
  162. </p>
  163. <p>Now you can start up the Android emulator:
  164. </p>
  165. <pre class="screen">$ emulator -avd avd16 &amp;
  166. </pre>
  167. <p>Wait until Android has finished booting (you will see the Android home screen),
  168. click the menu and home buttons. Now install our new application:
  169. </p>
  170. <pre class="screen">adb install bin/KawaHello-debug.apk
  171. </pre>
  172. </section>
  173. <section class="sect2" title="Running the application on your device" epub:type="division" id="idm139667869642320">
  174. <div class="titlepage">
  175. <div>
  176. <div>
  177. <h3 class="title">Running the application on your device</h3>
  178. </div>
  179. </div>
  180. </div>
  181. <p>If the emulator is running, kill it:
  182. </p>
  183. <pre class="screen">$ kill %emulator
  184. </pre>
  185. <p>On your phone or other Android devude, enable USB debugging.
  186. (This is settable from the <code class="literal">Settings</code> application,
  187. under <code class="literal">Applications / Development</code>.)
  188. </p>
  189. <p>Connect the phone to your computer with the USB cable.
  190. Verify that the phone is accessible to <code class="literal">adb</code>:
  191. </p>
  192. <pre class="screen">$ adb devices
  193. List of devices attached
  194. 0A3A560F0C015024 device
  195. </pre>
  196. <p>If you don’t see a device listed, it may be permission problem. You can figure out which device corresponds to the phone by doing:
  197. </p>
  198. <pre class="screen">$ ls -l /dev/bus/usb/*
  199. /dev/bus/usb/001:
  200. total 0
  201. ...
  202. crw-rw-rw- 1 root wheel 189, 5 2010-10-18 16:52 006
  203. ...
  204. </pre>
  205. <p>The timestamp corresponds to when you connected the phone.
  206. Make the USB connection readable:
  207. </p>
  208. <pre class="screen">$ sudo chmod a+w /dev/bus/usb/001/006
  209. </pre>
  210. <p>Obviously if you spend time developing for an Androd phone you’ll want to automate this process;
  211. <a class="ulink" href="https://sites.google.com/site/siteofhx/Home/android/drivers/udc" target="_top">this link</a>
  212. or <a class="ulink" href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/nTfhhPktGfM" target="_top">this link</a> may be helpful.
  213. </p>
  214. <p>Anyway, once <code class="literal">adb</code> can talk to the phone, you install in the same way as before:
  215. </p>
  216. <pre class="screen">adb install bin/KawaHello-debug.apk
  217. </pre>
  218. </section>
  219. <section class="sect2" title="Some debugging notes" epub:type="division" id="idm139667869632608">
  220. <div class="titlepage">
  221. <div>
  222. <div>
  223. <h3 class="title">Some debugging notes</h3>
  224. </div>
  225. </div>
  226. </div>
  227. <p>You will find a copy of the SDK documentation in <code class="literal">$ANDROID_HOME/docs/index.html</code>.
  228. </p>
  229. <p>If the emulator complains that your application has stopped unexpectedly, do:
  230. </p>
  231. <pre class="screen">$ adb logcat
  232. </pre>
  233. <p>This shows log messages, stack traces, output from the <code class="literal">Log.i</code> logging method, and other useful information.
  234. (You can alternatively start <code class="literal">ddms</code> (Dalvik Debug Monitor Service), click on the <code class="literal">kawa.android line</code> in the top-left sub-window to select it, then from the <code class="literal">Device</code> menu select <code class="literal">Run logcat....</code>).
  235. </p>
  236. <p>To uninstall your application, do:
  237. </p>
  238. <pre class="screen">$ adb uninstall kawa.android
  239. </pre>
  240. </section>
  241. <section class="sect2" title="Other resources" epub:type="division" id="idm139667869626352">
  242. <div class="titlepage">
  243. <div>
  244. <div>
  245. <h3 class="title">Other resources</h3>
  246. </div>
  247. </div>
  248. </div>
  249. <p>(A more interesting <a class="ulink" href="http://androidscheme.blogspot.com/2010/10/text-to-speech-app.html" target="_top">text-to-speech</a> example app is on Santosh Rajan’s <a class="ulink" href="http://androidscheme.blogspot.com/" target="_top">Android-Scheme blog</a>.)
  250. </p>
  251. <p><a class="ulink" href="https://github.com/ecraven/SchemeAndroidOGL" target="_top">https://github.com/ecraven/SchemeAndroidOGL</a>
  252. </p>
  253. </section>
  254. </section>
  255. <footer>
  256. <div class="navfooter">
  257. <ul>
  258. <li>
  259. <b class="toc">
  260. <a href="Building-for-Android.xhtml#idm139667869672672">Downloading and setting up the Android SDK</a>
  261. </b>
  262. </li>
  263. <li>
  264. <b class="toc">
  265. <a href="Building-for-Android.xhtml#idm139667869666944">Building Kawa for Android</a>
  266. </b>
  267. </li>
  268. <li>
  269. <b class="toc">
  270. <a href="Building-for-Android.xhtml#idm139667869660736">Creating the application</a>
  271. </b>
  272. </li>
  273. <li>
  274. <b class="toc">
  275. <a href="Building-for-Android.xhtml#idm139667869650560">Running the application on the Android emulator</a>
  276. </b>
  277. </li>
  278. <li>
  279. <b class="toc">
  280. <a href="Building-for-Android.xhtml#idm139667869642320">Running the application on your device</a>
  281. </b>
  282. </li>
  283. <li>
  284. <b class="toc">
  285. <a href="Building-for-Android.xhtml#idm139667869632608">Some debugging notes</a>
  286. </b>
  287. </li>
  288. <li>
  289. <b class="toc">
  290. <a href="Building-for-Android.xhtml#idm139667869626352">Other resources</a>
  291. </b>
  292. </li>
  293. </ul>
  294. <p>
  295. Up: <a accesskey="u" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
  296. <p>
  297. Previous: <a accesskey="p" href="Building-JavaFX-applications.xhtml">Building JavaFX applications</a></p>
  298. <p>
  299. Next: <a accesskey="n" href="Android-view-construction.xhtml">Android view construction</a></p>
  300. </div>
  301. </footer>
  302. </body>
  303. </html>