Scala on-android

1,213 views
1,103 views

Published on

Scala on Android: My Experience

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,213
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Scala on-android

  1. 1. Scala on Android
  2. 2. Android Development● Write lots of Java code● Write UI and other resources in XML● Generate Java code from resources● Compile Java code into “.class” files● Run proguard● Compile class files to Dalvik Executable (DEX)● Deploy on device
  3. 3. Dalvik VM● Does not support “.class” bytecode format● Can not use libraries which generate bytecode● Devices have limited CPU and Memory● Can not easily share libraries between applications
  4. 4. Eclipse● Has nice UI editors● IDE is a build tool
  5. 5. Alternative Languages for Android
  6. 6. Jython● Does not have enough community support● Slow● Dynamic
  7. 7. JRuby● Ruboto framework● Dynamic● Need to use java code generation
  8. 8. Clojure● Very cool :)● Very slow start-up time● Sometimes need to generate bytecode
  9. 9. PHPWho cares?
  10. 10. Rhino● Nice language● Can run with optimization turned off● Completely interpreted● Dynamic
  11. 11. Groovy ???
  12. 12. Scala● Statically typed● Compiled to Java bytecode● Has access to all classes in Android● Fast ● but sometimes slow :)● Has SBT ● Android ● Maven artifacts
  13. 13. How to build Scala apps for Android● ANT● Maven● SBT + Android Plugin
  14. 14. Little nice things about Scala
  15. 15. Type Inference● Improve readability● Reduce number of imports● Less typing
  16. 16. val stream = getAssets().open("areas.json") val stream: InputStream = getAssets().open("areas.json")
  17. 17. Import statementimport android.view.{ Window, Menu, MenuItem, KeyEvent, View}import android.util.Log.{e => error}
  18. 18. Lazy valslazy val searchBox = findViewById(R.id.searchBox) .asInstanceOf[AutoCompleteTextView]
  19. 19. Mixin Inheritanceclass MainActivity extends MapActivity with Logging with DatabaseHelpers with MapHelpers
  20. 20. Collections● Best collection library I ever used ● Sorry google-collections...● Using arrays is easy● @specialized
  21. 21. Collection examplesval suggestions = areas map { _.name }val index = areas.indexWhere( _.name == searchString)val index = areas.indexWhere(_.contains(coordinate))pointList.minBy(_.lat)
  22. 22. First class functionsclass AreaOverlay( areas: Array[Area], selectListener: Int => Unit)new AreaOverlay( areas, this.onAreaSelected)
  23. 23. Convert listeners into functionsimplicit def fn2ClickListener( f: View => Unit): View.OnClickListener = new View.OnClickListener() { override def onClick(v: View) = f(view) }exitButton.setOnClickListener( this.onExitClick)
  24. 24. Files● Directory structure is not forced to mirror packages● Files can contain any amount of classes
  25. 25. Pattern matching● No NPEs when using Option[T]● Easy to work with data objects
  26. 26. Pattern matchinglastLocation match { case Some(c@Coordinate(lat, lon)) => ... case None => ...}
  27. 27. Implicitsimplicit def coordinate2geopoint( c: Coordinate): GeoPoint = new GeoPoint( c.lat * 1.0e6 toInt, c.lon * 1.0e6 toInt)
  28. 28. XMLfor (s <- xml "Document" "Style")placemarkElem "Polygon" "outerBoundaryIs" "LinearRing" "coordinates"
  29. 29. Bad stuff● Performance sensitive parts ● Learn to use “javap” ● Remove for-comprehensions and lambda functions ● Do not use boxed types “by accident”● Tools ● UI Editor ● Debugger ● Build time (dexing of scala library)
  30. 30. What I missed from Java● Google Guice● Maven● Good Eclipse integration● Feeling important...
  31. 31. Thank you!
  32. 32. Questions?

×