Programming Android Application in Scala @ OSDC.tw 2010

Programming Android Application in Scala @ OSDC.tw 2010

Introduction how to programming Android application using Scala @ OSDC.tw 2010.

2377fa6c1b0f089b7e3229dfe2f6ae7f?s=128

Brian Hsu

April 25, 2010
Tweet

Transcript

  1. Programming Android Application in Scala BrianHsu / OSDC.2010

  2. 關於我

  3. 墳墓 (BrianHsu)

  4. 表面上 軟體工程師

  5. 實際上 萌系阿宅

  6. 用 萌 化 的 手 機 入 侵 藍 星

    是 也
  7. 寫 Android 萌 程 式          

    是 燃 的 我 最 喜 歡 女 僕 了 ! 我 要 讓 手 上 機 全 都 是 女 僕 !
  8. 用 Java 寫程式卻燃不起來

  9. 高手大師兄

  10. 聽說高手都要精通 Multiple Language……

  11. ASE+Ruby/Python/Perl

  12. 寫作 Dynamic Scripting Language

  13. 唸作 快、狠、準

  14. 等等

  15. 還要先裝 Runtime 啊?

  16. 等等

  17. 動態型別?

  18. 執行期爆炸 def boom puts "Hello World" end puts " 一個小小的

    typo" booom
  19. 結論

  20. 我是懦夫 不敢用 ASE

  21. None
  22. What is Scala?

  23. 樓 梯

  24. A Postfunctional Programming Language on Java Virtual Machine

  25. Compile to JVM .class Byte Code

  26. 理論上 Full Java Interoperability

  27. 可以直接拿來寫 Android Application

  28. What is Scala?

  29. A Scalable Language

  30. 會隨 User 變形的程式語言

  31. A Scalable Language

  32. A Scalable Language

  33. Why Scala?

  34. None
  35. 三個願望一次滿足

  36. REPL Scripting Compiling

  37. REPL

  38. Scripting

  39. Compiling

  40. 三個願望一次滿足

  41. Procedure Object Oriented Functional

  42. Procedure Programming

  43. Object Oriented Programming

  44. Functional Programming

  45. 三個願望一次滿足

  46. Concise Static Typing Duck Typing

  47. Concise (Ruby)

  48. Concise (Scala)

  49. Static Typing (Ruby)

  50. Static Typing (Scala)

  51. 真、 Duck Typing (Ruby)

  52. 偽‧ Duck Typing (Scala)

  53. 結論

  54. 有 Scala 、無懦夫

  55. 用 Scala 寫 萌 程 式        

      是 燃 的 我 最 喜 歡 女 僕 了 ! 我 要 讓 手 上 機 全 都 是 女 僕 !
  56. Scala vs Java

  57. Scala compared to Java Scala adds Scala removes + a

    pure object system - static members + operator overloading - primitive types + closures - break, continue + mixin composition with traits - special treatment of interfaces + existential types - wildcards + abstract types - raw types + pattern matching - enums Modeled in libraries: assert, enums, properties, events, actors, using, queries, …
  58. Scala cheat sheet (1): Definitions Scala method definitions: def fun(x:

    Int): Int = { result } def fun = result Scala variable definitions: var x: Int = expression val x: String = expression Java method definition: int fun(int x) { return result } (no parameterless methods) Java variable definitions: int x = expression final String x = expression
  59. Scala cheat sheet (2): Expressions Scala method calls: obj.meth(arg) obj

    meth arg Scala choice expressions: if (cond) expr1 else expr2 expr match { case pat 1 => expr 1 .... case pat n => expr n } Java method call: obj.meth(arg) (no operator overloading) Java choice expressions, stmts: cond ? expr1 : expr2 if (cond) return expr1; else return expr2; switch (expr) { case pat 1 : return expr 1 ; ... case pat n : return expr n ; } // statement only
  60. Scala cheat sheet (3): Objects and Classes Scala Class and

    Object class Sample(x: Int, val p: Int) { def instMeth(y: Int) = x + y } object Sample { def staticMeth(x: Int, y: Int) = x * y } Java Class with statics class Sample { private final int x; public final int p; Sample(int x, int p) { this.x = x; this.p = p; } int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) { return x * y; } }
  61. Scala cheat sheet (4): Traits Scala Trait trait T {

    def abstractMth(x: String): Int def concreteMth(x: String) = x + field var field = “!” } Scala mixin composition: class C extends Super with T Java Interface interface T { int abstractMth(String x) } (no concrete methods) (no fields) Java extension + implementation: class C extends Super implements T
  62. 當機器人爬上樓梯

  63. Android Application 開發流程

  64. Java / ANT

  65. $ android create project -p … $ vim ..... $

    ant install
  66. http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package- apk-from-the-command-line-using-the-sdk-tools-continuously-integrated-using- cruisecontrol/

  67. http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package- apk-from-the-command-line-using-the-sdk-tools-continuously-integrated-using- cruisecontrol/

  68. How?

  69. XML 地獄 ANT

  70. Perfect SBT

  71. Simple Build Tool

  72. 以 Scala 為 Host Language

  73. Rake=>Ruby SBT =>Scala

  74. 不用任何設定 支援連續編譯

  75. Android-Plugin

  76. android-plugin http://github.com/jberkel/android-plugin project/build/MyProject.scala

  77. android-plugin

  78. 我不會用 功能完整

  79. 阿宅的通病 自己寫一個

  80. sbt-android project/build/MyProject.scala

  81. sbt-android project/plugins/Plugins.scala

  82. Live Demo

  83. Install SBT/SBT-Android • Install SBT • http://code.google.com/p/simple-build-tool/ • Download SBT-Android

    • $ git clone http://github.com/brianhsu/sbt-android.git • Install SBT-Android • $ cd sbt-android • $ sbt publish-local
  84. Create Android/Scala Project • Create SBT project • $ mkdir

    AndroidTest • $ cd AndroidTest • $ sbt • Edit project/build/MyProject.scala • Edit project/plugins/Plugins.scala • $ sbt update
  85. Android/Scala Project Struture

  86. Build Scala/Android Project • $ sbt action • $ sbt

    install • $ sbt android-compile • $ sbt proguard • $ sbt zipalign • $ ...
  87. Scala in Android

  88. Trait 是好幫手

  89. SQL 再見! Collection 最高!

  90. 善用偽 Duck Typing

  91. 結論 可以把 Java 丟了……應該唄?

  92. 等等,這是孔明的陷阱!

  93. Scala 的限制與臭蟲 • 限制 • Cannot acess static protected Java

    field. – 對不起, Google Maps API 用到了。 • 臭蟲 • Static Java Inner class 造成 classfile borken / missing dependcy – Google Maps API 踩到地雷了 • Cyclic dependcy – Google Data API 中計了 – 以於 Scala 2.8 main trunk 中修正 • 解決方式 • 用 Java 寫個 Wrapper 唄 Missing dependency
  94. Thanks!