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

Scala On Android

Javi Pacheco
November 27, 2015

Scala On Android

An overview of the current state of affairs when developing Android apps with Scala, and examine why contrary to popular belief it is ready for prime time. We will discover some of the tools and more useful libraries, and explore how Scala on Android helps circumvent most of the limitations of Java and the Android SDK

Javi Pacheco

November 27, 2015
Tweet

More Decks by Javi Pacheco

Other Decks in Programming

Transcript

  1. WHO ARE WE? Fede Fernández Scala Software Engineer at 47

    Degrees @fede_fdz Javi Pacheco Scala Software Engineer at 47 Degrees @javielinux
  2. Proguard and Multidex Problem: 65k Methods and Scala library size

    Solution 1: Proguard to remove unused methods
  3. Proguard and Multidex Problem: 65k Methods and Scala library size

    Solution 1: Proguard to remove unused methods Solution 2: Multidex to deal with method limit
  4. Typed Resources class MainActivity extends AppCompatActivity with TypedFindView { lazy

    val text: TextView = findView(TR.textView) override def onCreate(bundle: Bundle) = { super.onCreate(bundle) setContentView(R.layout.my_activity) text.setText("Hi!") } }
  5. SBT Console > test …. …. [info] 69 examples, 0

    failure, 0 error [info] Passed: Total 159, Failed 0, Errors 0, Passed 159 [success] Total time: 30 s, completed 20-nov-2015 9:59:48
  6. SBT Console > android:run …. [info] Generating dex, incremental=false, multidex=true

    [info] dex method count: 79628 [info] Packaged: scala-sample-debug-unaligned.apk (7,08MB) [info] Debug package does not need signing: scala-sample-debug-unaligned.apk [info] zipaligned: scala-sample-debug.apk [info] Installing to Nexus 4 (047d6953d08fa35b)... [info] [scala-sample-debug.apk] Install finished: 7,09MB in 26,69s. 271,86KB/s [info] Running on Nexus 4 (047d6953d08fa35b)... [info] Starting: Intent { cmp=com.fortysevendeg.scala/com.fortysevendeg.scala.launcher.Activity } [success] Total time: 97 s, completed 20-nov-2015 9:52:48
  7. SBT Console > devices [info] Serial Model Battery % Android

    Version [info] ---------------------- ---------------- ------------- -------------- [info] * oquoe2avnooz1my5 Nexus 4 76% 5.1.1 API 22 [info] 192.168.57.101:5555 Google Nexus S - 4.1.1 95% 4.1.1 API 16 > device oquoe2avnooz1my5 [info] default device: oquoe2avnooz1my5
  8. android.Plugin.androidBuild // Specifying the Android target Sdk version platformTarget in

    Android := "android-22" // Application Name name := """scala-android-sample""" // Application Version version := "1.0.0" // Scala version scalaVersion := "2.11.7" SBT Configuration
  9. // Override the run task with the android:run run <<=

    run in Android proguardScala in Android := true useProguard in Android := true proguardOptions in Android ++= Seq( "-ignorewarnings", "-keep class scala.Dynamic") SBT Configuration
  10. libraryDependencies ++= Seq( aar("com.android.support" % "appcompat-v7" % "23.0.1"), aar("com.android.support" %

    "recyclerview-v7" % "23.0.1"), "com.google.android" % "android" % "4.1.1.4" % "test", "org.specs2" %% "specs2-core" % "3.6.5" % "test", "org.specs2" %% "specs2-mock" % "3.6.5" % "test") // Repositories for dependencies resolvers ++= Seq(Resolver.mavenLocal, DefaultMavenRepository, Resolver.typesafeRepo("releases"), Resolver.defaultLocal, "Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases" ) SBT Configuration
  11. Google says: If you have been using Eclipse with ADT,

    be aware that Android Studio is now the official IDE for Android
  12. Scaloid Features Easy to use & Compatible with your legacy

    code UI Layout without XML Asynchronous task processing More … [ https://github.com/pocorall/scaloid ]
  13. Scaloid //plain vanilla scala val button = new Button(context) button.setText("Greet")

    button.setOnClickListener(new OnClickListener() { def onClick(v: View) = { Toast.makeText(context, "Hello!", Toast.LENGTH_SHORT).show() } }) layout.addView(button)
  14. Macroid Features DSL focused to GUI Intensive use of Macros

    Brings the Functional Programming on Ui More … [ http://macroid.github.io ]
  15. Macroid - Layouts getUi { l[LinearLayout]( w[Button], w[TextView] <~ text(“Hi!”)

    ) } <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hi!"/> </LinearLayout>
  16. Android Contexts Can be derived from Application, Activities and Services

    Give access to Resources, Device Status… Singletons and life cycle attached Needed for a broad range of operations!
  17. class MyActivity extends AppCompatActivity with Contexts[Activity] { override def onCreate(bundle:

    Bundle) = { ToastOps.showMessage("Implicits are awesome!") } } object ToastOps { def showMessage(msg: String)(implicit c: ContextWrapper) = Toast.makeText(c.bestAvailable, msg, Toast.LENG_SHORT).show() } Macroid - Using implicits
  18. Macroid - Tweaks Add styles and behaviours to your widgets

    textView <~ text("Hi") textView <~ text("Hi") <~ show def textAndShow(str: String): Tweak[TextView] = text(str) + show textView <~ textAndShow("Hi")
  19. Macroid - Custom Tweaks Create your own tweaks def myTweak(str:

    String) = Tweak[TextView] { tv => tv.setText(str) tv.setTypeface(tv.getTypeface(), Typeface.BOLD) } textView <~ myTweak("My Bold Text")
  20. Macroid - Snails textView <~~ fadeIn textView <~~ fadeIn <~~

    fadeOut editText <~ text("foo") <~~ fadeIn <~ enable Add animations to your views
  21. Macroid - Transformers val imagesOnly = Transformer { case i:

    ImageView ⇒ i <~ show case x: View ⇒ x <~ hide } rootLayout <~ imagesOnly Scala pattern matching to alter layouts in a flexible way
  22. Compile and Run Slower (Scala Compiler + Proguard) Better Build

    Tool Easier to extend Tip: Take your time to setup your SBT