Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Brian Hsu

April 25, 2010
Tweet

More Decks by Brian Hsu

Other Decks in Programming

Transcript

  1. 寫 Android 萌 程 式          

    是 燃 的 我 最 喜 歡 女 僕 了 ! 我 要 讓 手 上 機 全 都 是 女 僕 !
  2. 用 Scala 寫 萌 程 式        

      是 燃 的 我 最 喜 歡 女 僕 了 ! 我 要 讓 手 上 機 全 都 是 女 僕 !
  3. 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, …
  4. 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
  5. 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
  6. 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; } }
  7. 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
  8. 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
  9. Create Android/Scala Project • Create SBT project • $ mkdir

    AndroidTest • $ cd AndroidTest • $ sbt • Edit project/build/MyProject.scala • Edit project/plugins/Plugins.scala • $ sbt update
  10. Build Scala/Android Project • $ sbt action • $ sbt

    install • $ sbt android-compile • $ sbt proguard • $ sbt zipalign • $ ...
  11. 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