Scala on Android

Mateusz Kubuszok > Scalac

Agenda

  • raw Scala and Android
  • different flavors of UI development
  • functional programming on Android

Scala and Android

Gotchas

Gradle - less invasive approach

Gradle

  • IntelliJ support for WYSIWYG
  • all normal tutorials and docs could be easily applied
  • no Scala formatters, coverage etc

gradle-android-scala-plugin

build.gradle
buildscript {
  dependencies {
      classpath "com.android.tools.build:gradle:1.3.1"
      classpath "jp.leafytree.gradle:gradle-android-scala-plugin:1.4"
  }
}
apply plugin: "com.android.application"
apply plugin: "jp.leafytree.android-scala"
dependencies {
  compile "org.scala-lang:scala-library:2.11.7"
}

gradle-android-scala-plugin

  • existed since 2014
  • supports Scala 2.10.5 and 2.11.7
  • no support for Scala compiler plugins
  • requires manual config of multidex and/or proguard
  • last update 5 months ago

SBT - complete Scala experience

sbt-android

project/plugins.sbt
addSbtPlugin("org.scala-android" % "sbt-android" % "1.6.15")
build.sbt
androidBuild
platformTarget := "android-21"

sbt-android

  • existed since 2012
  • allows to use any Scala version (though 2.11.5 min would be best)
  • requires manual config of multidex and/or proguard
  • last update few days ago

sbt-android-gradle

  • imports all settings from Gradle project into SBT
  • we could maintaining project in Gradle and then reflect changes to SBT
  • we can migrate Gradle to SBT and then remove Gradle build
  • changes made manually would not be maintained so we would have to reapply

Different flavors of UI development

Ol' good XML/Java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:padding="20dip">
  <TextView android:layout_width="match_parent" android:text="ID"
      android:id="@+id/userid" android:layout_height="wrap_content" />
  <EditText android:layout_width="match_parent"
      android:layout_height="wrap_content" android:id="@+id/userId" />
  <Button android:layout_width="match_parent"
      android:layout_height="wrap_content" android:id="@+id/signin"
      android:text="Sign in"/>
</LinearLayout>
example from Scaloid docs

Ol' good XML/Java

EditText userId = (EditText)findByViewId(R.id.userid);
Button signin = (Button)findByViewId(R.id.signin);
signin.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    signin(userId.getText());
  }
});
example from Scaloid docs

Ol' good XML/Java

  • all tools support them
  • documentation is using them, so no need to translating concepts into sth else
  • verbose

Scaloid

new SVerticalLayout {
    STextView("ID")
    val userId = SEditText()
    SButton("Sign in", signin(userId.text))
  }.padding(20 dip)
example from Scaloid docs

Scaloid

  • less verbosity
  • uses Scala features to create DSL
  • has online converted from XML to Scaloid (for supported features)
  • wraps everything in new classes/traits - it is hard to mix Scaloid with Java code
  • some methods run on an UI thread
  • no support for some newer SDK features (e.g. android.support.v4.widget.DrawerLayout)
  • last update 2 months ago

Macroid

var userId = slot[EditText]

l[VerticalLinearLayout](
  w[TextView] <~ text("ID"),
  w[EditText] <~ wire(userId),
  w[Button] <~ text("Sign in") <~ On.click(signin(userId.get.getText))
) ~> padding(all = 20 dp)
example from Macroid docs

Macroid

  • maintained by 47deg
  • as little verbosity as with Scaloid
  • works with all Android components
  • really nice support for async - UI actions as IO monad (unfortunately they don't work with for-comprehension)
  • thread safety (as opposed to vanilla Android SDK)
  • last update 3 months ago

Macroid

val action: Ui[Button] =
  (startButton <~ disable) ~
  (stopButton <~ enable)

import macroid.FullDsl._
runUi(action)

Protify

demo from Protify's page

Functional programming on Android

Android

  • stateful API
  • allows and uses nulls
  • imposes flow of data and way of handling actions
  • many libraries use globals

Slick

Scalaz and Cats

  • :)

Mutability and statefulness

android lifecycle

from Android documentation

Mutability and statefulness

  • purely functional programming virtually impossible
  • nothing prevent us from creating an abstraction layer

Summary

  • Scala is an option
  • UI can be developed in several ways
  • our favorite libraries works
  • Android SDK sucks

Useful sources

Questions?

Thank you!