SlideShare a Scribd company logo
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
Programming Android Application in Scala.
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?
Programming Android Application in 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            - 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:     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
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
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;
                                        }
                                      }
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
當機器人爬上樓梯
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=>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-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 dependency


        missing dependcy
        –   Google Maps API 踩到地雷了
    ●   Cyclic dependcy
        –   Google Data API 中計了
        –   以於 Scala 2.8 main trunk 中修正
●
    解決方式
    ●   用 Java 寫個 Wrapper 唄
Thanks!

More Related Content

PDF
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
ODP
Introduction To Scala
PDF
JavaScript objects and functions
PDF
Scala jargon cheatsheet
PDF
Scala - core features
PDF
Scala cheatsheet
PDF
Few simple-type-tricks in scala
PPT
Scala uma poderosa linguagem para a jvm
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Introduction To Scala
JavaScript objects and functions
Scala jargon cheatsheet
Scala - core features
Scala cheatsheet
Few simple-type-tricks in scala
Scala uma poderosa linguagem para a jvm

What's hot (19)

PPTX
All about scala
PDF
An Introduction to Scala for Java Developers
PPTX
Scala on Android
PDF
scala
PDF
Solid and Sustainable Development in Scala
PDF
Scaladroids: Developing Android Apps with Scala
PDF
JavaScript Foundations Day1
PDF
Starting with Scala : Frontier Developer's Meetup December 2010
PDF
Railroading into Scala
PDF
Workshop Scala
PDF
Introduction to Scala for JCConf Taiwan
PPTX
PDF
Scala vs java 8
PDF
Scala categorytheory
PDF
PPT
Scala - brief intro
ODP
PDF
Lecture 5
PDF
Stepping Up : A Brief Intro to Scala
All about scala
An Introduction to Scala for Java Developers
Scala on Android
scala
Solid and Sustainable Development in Scala
Scaladroids: Developing Android Apps with Scala
JavaScript Foundations Day1
Starting with Scala : Frontier Developer's Meetup December 2010
Railroading into Scala
Workshop Scala
Introduction to Scala for JCConf Taiwan
Scala vs java 8
Scala categorytheory
Scala - brief intro
Lecture 5
Stepping Up : A Brief Intro to Scala
Ad

Viewers also liked (20)

PDF
Android Programming
KEY
Android Development: The Basics
PPT
PDF
Introduction to Scala
PDF
Designing Ruby APIs
ODP
Open document presentation
PPTX
Android Coding Standered
PPT
Android
PDF
Android Apps for Absolute Beginners
PDF
App Smashing for Beginners
KEY
Scala For Java Programmers
PPTX
Architect Concept - .NET Interview Senior / Mid Level
PDF
Scala vs Java 8 in a Java 8 World
PDF
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
PPTX
Beginners guide to creating mobile apps
PPTX
Android App Development Tips for Beginners
PPS
Data Mining - Classification Of Breast Cancer Dataset using Decision Tree Ind...
PDF
Android Development: Build Android App from Scratch
PPSX
Decision tree Using c4.5 Algorithm
Android Programming
Android Development: The Basics
Introduction to Scala
Designing Ruby APIs
Open document presentation
Android Coding Standered
Android
Android Apps for Absolute Beginners
App Smashing for Beginners
Scala For Java Programmers
Architect Concept - .NET Interview Senior / Mid Level
Scala vs Java 8 in a Java 8 World
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Beginners guide to creating mobile apps
Android App Development Tips for Beginners
Data Mining - Classification Of Breast Cancer Dataset using Decision Tree Ind...
Android Development: Build Android App from Scratch
Decision tree Using c4.5 Algorithm
Ad

Similar to Programming Android Application in Scala. (20)

PDF
Getting Started With Scala
PDF
Getting Started With Scala
PPT
Scala Talk at FOSDEM 2009
PDF
A Sceptical Guide to Functional Programming
PDF
FP in Java - Project Lambda and beyond
PPT
Introduction to Javascript
PPT
Scala in a nutshell by venkat
PPTX
Intro to scala
PDF
The Scala Programming Language
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PDF
Scala is java8.next()
PDF
Scala Bootcamp 1
PDF
Python to scala
ODP
Introducing scala
PDF
Kotlin for Android Developers - 3
PDF
Scala overview
PPTX
Qcon2011 functions rockpresentation_scala
PPTX
Scala for curious
PDF
BCS SPA 2010 - An Introduction to Scala for Java Developers
Getting Started With Scala
Getting Started With Scala
Scala Talk at FOSDEM 2009
A Sceptical Guide to Functional Programming
FP in Java - Project Lambda and beyond
Introduction to Javascript
Scala in a nutshell by venkat
Intro to scala
The Scala Programming Language
Develop your next app with kotlin @ AndroidMakersFr 2017
Scala is java8.next()
Scala Bootcamp 1
Python to scala
Introducing scala
Kotlin for Android Developers - 3
Scala overview
Qcon2011 functions rockpresentation_scala
Scala for curious
BCS SPA 2010 - An Introduction to Scala for Java Developers

More from Brian Hsu (6)

PDF
Unicode ncr
PDF
自由軟體讓你五分鐘上新聞
PDF
自由軟體讓你五分鐘上新聞
PDF
Cat Fs Draft
PDF
個人簡介
PDF
Ontology-based Content Management System (ICIM 2008)
Unicode ncr
自由軟體讓你五分鐘上新聞
自由軟體讓你五分鐘上新聞
Cat Fs Draft
個人簡介
Ontology-based Content Management System (ICIM 2008)

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
Advanced Soft Computing BINUS July 2025.pdf
Chapter 2 Digital Image Fundamentals.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
madgavkar20181017ppt McKinsey Presentation.pdf
Understanding_Digital_Forensics_Presentation.pptx
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development
NewMind AI Monthly Chronicles - July 2025
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Advanced methodologies resolving dimensionality complications for autism neur...

Programming Android Application in Scala.