programming android application in scala
DESCRIPTION
Presentation at OSDC.tw 2010.TRANSCRIPT
Programming Android Application in Scala
BrianHsu / OSDC.2010
關於我
墳墓 (BrianHsu)
表面上
軟體工程師
實際上
萌系阿宅
用萌化的手機
入侵藍星是也
寫Andro
id
萌程式
是燃的
我最喜歡女
僕了!
我要讓手上機
全都是女僕!
用 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 toJVM .class Byte Code
理論上
Full Java Interoperability
可以直接拿來寫Android Application
What is Scala?
A Scalable Language
會隨 User 變形的程式語言
A Scalable Language
A Scalable Language
Why Scala?
三個願望一次滿足
REPLScriptingCompiling
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 - 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, …
Scala cheat sheet (1): Definitions
Scala method definitions:
def fun(x: Int): Int = { result}
def fun = result
Scala variable definitions:
var x: Int = expressionval x: String = expression
Java method definition:
int fun(int x) { return result}
(no parameterless methods)
Java variable definitions:
int x = expressionfinal String x = expression
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 pat1 => expr1 .... case patn => exprn
}
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 pat1 : return expr1; ... case patn : return exprn ;} // statement only
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; }}
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
當機器人爬上樓梯
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-tools-continuously-integrated-using-cruisecontrol/
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/
How?
XML 地獄
ANT
Perfect
SBT
Simple Build Tool
以 Scala 為Host Language
Rake=>RubySBT =>Scala
不用任何設定
支援連續編譯
Android-Plugin
android-pluginhttp://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-Android● $ git clone http://github.com/brianhsu/sbt-android.git
● Install SBT-Android● $ cd sbt-android● $ sbt publish-local
Create Android/Scala Project
● Create SBT project● $ mkdir AndroidTest● $ cd AndroidTest● $ sbt● Edit project/build/MyProject.scala● Edit project/plugins/Plugins.scala● $ sbt update
Android/Scala Project Struture
Build Scala/Android Project
● $ sbt action● $ sbt install
● $ sbt android-compile● $ sbt proguard● $ sbt zipalign● $ ...
Scala in Android
Trait 是好幫手
SQL 再見!Collection 最高!
善用偽 Duck Typing
結論
可以把 Java 丟了……應該唄?
等等,這是孔明的陷阱!
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
Thanks!