Your SlideShare is downloading. ×
0
Programming Android
 Application in Scala
  BrianHsu / OSDC.2010
關於我
墳墓 (BrianHsu)
表面上

軟體工程師
實際上

萌系阿宅
入用
侵萌
藍化
星的
是手
也機
僕我
     了最    寫
     !喜    




           Android
      歡
      女    
           
           
全我
都要
是讓
          是萌
女手
僕上...
用 Java 寫程式卻燃不起來
高手大師兄
聽說高手都要精通 Multiple Language……
ASE+Ruby/Python/Perl
寫作
Dynamic Scripting
   Language
唸作
快、狠、準
等等
還要先裝 Runtime 啊?
等等
動態型別?
def boom
    puts "Hello World"
end

puts " 一個小小的 typo"
booom




執行期爆炸
結論
我是懦夫
不敢用 ASE
What is Scala?
樓
梯
A Postfunctional
Programming Language
on Java Virtual Machine
Compile to
JVM .class Byte Code
理論上


   Full Java
Interoperability
可以直接拿來寫
Android Application
What is Scala?
A Scalable Language
會隨 User 變形的程式語言
A Scalable Language
A Scalable Language
Why Scala?
三個願望一次滿足
REPL
Scripting
Compiling
REPL
Scripting
Compiling
三個願望一次滿足
Procedure

Object Oriented

  Functional
Procedure Programming
Object Oriented Programming
Functional Programming
三個願望一次滿足
Concise
Static Typing
Duck Typing
Concise (Ruby)
Concise (Scala)
Static Typing (Ruby)
Static Typing (Scala)
真、 Duck Typing (Ruby)
偽‧ Duck Typing (Scala)
結論
有 Scala 、無懦夫
僕我    用
     了最
           




           Scala
     !喜
      歡
      女
           
           
           寫
全我
都要
      ...
Scala vs Java
Scala compared to Java


     Scala adds                        Scala removes
     + a pure object system            - sta...
Scala cheat sheet (1): Definitions

Scala method definitions:     Java method definition:

def fun(x: Int): Int = {      i...
Scala cheat sheet (2): Expressions

 Scala method calls:          Java method call:
 obj.meth(arg)                obj.meth...
Scala cheat sheet (3): Objects and Classes


 Scala Class and Object               Java Class with statics
               ...
Scala cheat sheet (4): Traits

                                       Java Interface
 Scala Trait

 trait T {             ...
當機器人爬上樓梯
Android Application
     開發流程
Java / ANT
$ android create project -p …
$ vim .....
$ ant install
http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package-
apk-from-the-command-line-using-the-sdk...
http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package-
apk-from-the-command-line-using-the-sdk...
How?
XML 地獄


ANT
Perfect


SBT
Simple Build Tool
以 Scala 為
Host Language
Rake=>Ruby
SBT =>Scala
不用任何設定
支援連續編譯
Android-Plugin
android-plugin
http://github.com/jberkel/android-plugin




project/build/MyProject.scala
android-plugin
我不會用

功能完整
阿宅的通病

自己寫一個
sbt-android




project/build/MyProject.scala
sbt-android




project/plugins/Plugins.scala
Live Demo
Install SBT/SBT-Android
●
    Install SBT
    ●
        http://code.google.com/p/simple-build-tool/
●
    Download SBT-And...
Create Android/Scala Project
●
    Create SBT project
    ●   $ mkdir AndroidTest
    ●   $ cd AndroidTest
    ●
        $...
Android/Scala Project Struture
Build Scala/Android Project
●
    $ sbt action
●
    $ sbt install
    ●
        $   sbt android-compile
    ●
        $  ...
Scala in Android
Trait 是好幫手
SQL 再見!
Collection 最高!
善用偽 Duck Typing
結論

可以把 Java 丟了……應該唄?
等等,這是孔明的陷阱!
Scala 的限制與臭蟲
●
    限制
    ●   Cannot acess static protected Java field.
        –   對不起, Google Maps API 用到了。
●
    臭蟲
   ...
Thanks!
Programming Android Application in Scala.
Programming Android Application in Scala.
Upcoming SlideShare
Loading in...5
×

Programming Android Application in Scala.

9,846

Published on

Presentation at OSDC.tw 2010.

Published in: Technology
1 Comment
25 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,846
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
312
Comments
1
Likes
25
Embeds 0
No embeds

No notes for slide

Transcript of "Programming Android Application in Scala."

  1. 1. Programming Android Application in Scala BrianHsu / OSDC.2010
  2. 2. 關於我
  3. 3. 墳墓 (BrianHsu)
  4. 4. 表面上 軟體工程師
  5. 5. 實際上 萌系阿宅
  6. 6. 入用 侵萌 藍化 星的 是手 也機
  7. 7. 僕我 了最  寫 !喜   Android 歡 女       全我 都要 是讓 是萌 女手 僕上 燃程 !機 的式
  8. 8. 用 Java 寫程式卻燃不起來
  9. 9. 高手大師兄
  10. 10. 聽說高手都要精通 Multiple Language……
  11. 11. ASE+Ruby/Python/Perl
  12. 12. 寫作 Dynamic Scripting Language
  13. 13. 唸作 快、狠、準
  14. 14. 等等
  15. 15. 還要先裝 Runtime 啊?
  16. 16. 等等
  17. 17. 動態型別?
  18. 18. def boom puts "Hello World" end puts " 一個小小的 typo" booom 執行期爆炸
  19. 19. 結論
  20. 20. 我是懦夫 不敢用 ASE
  21. 21. What is Scala?
  22. 22. 樓 梯
  23. 23. A Postfunctional Programming Language on Java Virtual Machine
  24. 24. Compile to JVM .class Byte Code
  25. 25. 理論上 Full Java Interoperability
  26. 26. 可以直接拿來寫 Android Application
  27. 27. What is Scala?
  28. 28. A Scalable Language
  29. 29. 會隨 User 變形的程式語言
  30. 30. A Scalable Language
  31. 31. A Scalable Language
  32. 32. Why Scala?
  33. 33. 三個願望一次滿足
  34. 34. REPL Scripting Compiling
  35. 35. REPL
  36. 36. Scripting
  37. 37. Compiling
  38. 38. 三個願望一次滿足
  39. 39. Procedure Object Oriented Functional
  40. 40. Procedure Programming
  41. 41. Object Oriented Programming
  42. 42. Functional Programming
  43. 43. 三個願望一次滿足
  44. 44. Concise Static Typing Duck Typing
  45. 45. Concise (Ruby)
  46. 46. Concise (Scala)
  47. 47. Static Typing (Ruby)
  48. 48. Static Typing (Scala)
  49. 49. 真、 Duck Typing (Ruby)
  50. 50. 偽‧ Duck Typing (Scala)
  51. 51. 結論
  52. 52. 有 Scala 、無懦夫
  53. 53. 僕我  用 了最   Scala !喜 歡 女      寫 全我 都要 是萌 是讓 女手 燃程 僕上 !機 的式
  54. 54. Scala vs Java
  55. 55. 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, …
  56. 56. Scala cheat sheet (1): Definitions Scala method definitions: Java method definition: def fun(x: Int): Int = { int fun(int x) { result return result } } def fun = result (no parameterless methods) Scala variable definitions: Java variable definitions: var x: Int = expression int x = expression val x: String = expression final String x = expression
  57. 57. Scala cheat sheet (2): Expressions Scala method calls: Java method call: obj.meth(arg) obj.meth(arg) obj meth arg (no operator overloading) Scala choice expressions: Java choice expressions, stmts: if (cond) expr1 else expr2 cond ? expr1 : expr2 if (cond) return expr1; else return expr2; expr match { case pat1 => expr1 switch (expr) { .... case pat1 : return expr1; case patn => exprn ... } case patn : return exprn ; } // statement only
  58. 58. Scala cheat sheet (3): Objects and Classes Scala Class and Object Java Class with statics class Sample { class Sample(x: Int, val p: Int) { private final int x; def instMeth(y: Int) = x + y public final int p; } Sample(int x, int p) { this.x = x; object Sample { this.p = p; def staticMeth(x: Int, y: Int) = } x*y int instMeth(int y) { } return x + y; } static int staticMeth(int x, int y) { return x * y; } }
  59. 59. Scala cheat sheet (4): Traits Java Interface Scala Trait trait T { interface T { int abstractMth(String x) def abstractMth(x: String): Int } def concreteMth(x: String) = x + field (no concrete methods) var field = “!” (no fields) } Java extension + implementation: Scala mixin composition: class C extends Super implements T class C extends Super with T
  60. 60. 當機器人爬上樓梯
  61. 61. Android Application 開發流程
  62. 62. Java / ANT
  63. 63. $ android create project -p … $ vim ..... $ ant install
  64. 64. 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/
  65. 65. 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/
  66. 66. How?
  67. 67. XML 地獄 ANT
  68. 68. Perfect SBT
  69. 69. Simple Build Tool
  70. 70. 以 Scala 為 Host Language
  71. 71. Rake=>Ruby SBT =>Scala
  72. 72. 不用任何設定 支援連續編譯
  73. 73. Android-Plugin
  74. 74. android-plugin http://github.com/jberkel/android-plugin project/build/MyProject.scala
  75. 75. android-plugin
  76. 76. 我不會用 功能完整
  77. 77. 阿宅的通病 自己寫一個
  78. 78. sbt-android project/build/MyProject.scala
  79. 79. sbt-android project/plugins/Plugins.scala
  80. 80. Live Demo
  81. 81. 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
  82. 82. Create Android/Scala Project ● Create SBT project ● $ mkdir AndroidTest ● $ cd AndroidTest ● $ sbt ● Edit project/build/MyProject.scala ● Edit project/plugins/Plugins.scala ● $ sbt update
  83. 83. Android/Scala Project Struture
  84. 84. Build Scala/Android Project ● $ sbt action ● $ sbt install ● $ sbt android-compile ● $ sbt proguard ● $ sbt zipalign ● $ ...
  85. 85. Scala in Android
  86. 86. Trait 是好幫手
  87. 87. SQL 再見! Collection 最高!
  88. 88. 善用偽 Duck Typing
  89. 89. 結論 可以把 Java 丟了……應該唄?
  90. 90. 等等,這是孔明的陷阱!
  91. 91. Scala 的限制與臭蟲 ● 限制 ● Cannot acess static protected Java field. – 對不起, Google Maps API 用到了。 ● 臭蟲 ● Static Java Inner class 造成 classfile borken / Missing dependency missing dependcy – Google Maps API 踩到地雷了 ● Cyclic dependcy – Google Data API 中計了 – 以於 Scala 2.8 main trunk 中修正 ● 解決方式 ● 用 Java 寫個 Wrapper 唄
  92. 92. Thanks!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×