Comparison among Java, Groovy, Scala, Kotlin in Android Development.

Overview

中文版 日本語

AndroidDemoIn4Languages

A simple Android application written in Java, Groovy, Scala and Kotlin in order to find out what is the better language for Android development.

ChangeLog

  • 2017-03-30 Update versions: Android Sdk -> v25, Gradle Plugin -> v2.2.0, Kotlin -> v1.1.1, Groovy -> 2.4.10
  • 2016-07-30 Update Scala, SBT, sbt-android versions
  • 2016-05-19 Update Kotlin from v1.0.0 to v1.0.2

How to Compare?

Only import the minimal dependencies.

Dependence Report

  • In Java
    • Import com.android.support:appcompat-v7:25.2.0
  • In Groovy
    • Import com.android.support:appcompat-v7:25.2.0
    • Import org.codehaus.groovy:groovy:2.4.10:grooid
    • Import org.codehaus.groovy:groovy-json:2.4.10
  • In Scala
    • Import com.android.support:appcompat-v7:25.2.0
    • Import org.scala-lang:scala-library:2.11.8
  • In Kotlin
    • Import com.android.support:appcompat-v7:25.2.0
    • Import org.jetbrains.kotlin:kotlin-stdlib:1.1.1

Analysis Report

Line Counter

Language Files Blank Lines Comment Lines Code Lines
Java 3 20 9 157
Groovy 3 23 9 140
Scala 3 32 9 127
Kotlin 3 23 9 136

Size Counter

Language Disable Proguard (bytes) Enable Proguard (bytes)
Java 1,438,673 893,137
Groovy 3,395,936 1,982,125
Scala 3,277,007 1,349,352
Kotlin 1,833,258 903,566

Method Counter

Language Disable Proguard Enable Proguard
Java 17,416 7,608
Groovy 47,982 24,379
Scala 67,608 20,109
Kotlin 23,587 7,656

Build Speed

Test on MacBook Pro (Retina, 15-inch, Mid 2014 & APPLE SSD SM0256F Media)

Gradle task: ./gradlew :app:clean :app:assembleDebug SBT task: sbt app/clean app/android:package

Language Gradle Plugin Time (secs) SBT Time (secs)
Java 2.2.0 ≈ 8 0.13.12 ≈ 10
Groovy 2.2.0 ≈ 20 0.13.12 -
Scala 1.3.1 ≈ 28 0.13.12 ≈ 17
Kotlin 2.2.0 ≈ 9 0.13.12 ≈ 20

Coding Comparison

Find View

Java

TextView title = (TextView)view.findViewById(android.R.id.text1);

Groovy

def title = view.findViewById(android.R.id.text1) as TextView

Scala

With Typed Resources enabled (include in Android SDK Plugin for SBT)

val title = view.findView(TR.text1)

Note: as a fallback, one can use the more traditional cast-based approach:

val title = view.findViewById(android.R.id.text1).asInstanceOf[TextView]

Kotlin

With Kotlin Android Extensions enabled (no extra dependencies needed):

val title = view.text1

Note: as a fallback, one can use the more traditional cast-based approach:

val title = view.findViewById(android.R.id.text1) as TextView

OnClickListener

Java

button.setOnClickListener(new View.OnClickListener() {
    @Override
    void onClick(final View v) {
      //  do something
    }
})

Groovy

button.onClickListener = {
    //  do something
}

Scala

button.onClick((v: View) =>
    //  do something
)

Kotlin

button.setOnClickListener {
    //  do something
}

Callback

Java

public interface FindCallback {
    void onFinish(List<String> results, Exception e);
}
private void findCountries(FindCallback doneCallback) {
    try {
        //  a long time mission
        doneCallback.onFinish(results, null);
    } catch (Exception e) {
        doneCallback.onFinish(null, e);
    }
}
findCountries(new FindCallback(){
    void onFinish(List<String> results, Exception e){
      //  handle result
    }
});

Groovy

def findCountries(Closure doneCallback) {
    try {
        //  a long time mission
        doneCallback(results, null)
    } catch (e) {
        doneCallback(null, e)
    }
}
findCountries{ List<String> results, Exception e ->
  //  handle result
});

Scala

def findCountries(doneCallback: (ArrayBuffer[String], Exception) => Unit):Unit = {
    try {
        //  a long time mission
        doneCallback(results, null)
    } catch {
        case e: Exception => doneCallback(null, e)
    }
}
findCountries((names: ArrayBuffer[String], e: Exception) =>
  //  handle result
)

Kotlin

fun findCountries(doneCallback: (List<String>?, Exception?) -> Unit) {
    try {
        //  a long time mission
        doneCallback(results, null)
    } catch (e: Exception) {
        doneCallback(null, e)
    }
}
findCountries{ list, e ->
  //  handle result
}

Extension

Scala

object AndroidContext {

  implicit class RichContext(ctx: Context) {
    def toast(msg: String) = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show()

    def progress(): ProgressDialog = {
      val progressDialog = new ProgressDialog(ctx)
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER)
      progressDialog.setCancelable(false)
      progressDialog
    }
  }

}

import AndroidContext._
this.toast(msg)
this.progress().show()

Kotlin

fun Activity.toast(message: CharSequence?, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this, message, duration).show()
}

fun Activity.progress() = ProgressDialog(this).apply {
    setProgressStyle(ProgressDialog.STYLE_SPINNER)
    setCancelable(false)
}

Switch

Java

String result;
switch (obj) {
    case 1:
        result = "Result is 1";
        break;
    case 10:
        result = "Result is 10";
        break;
    default:
        result = "Default result";
        break;
}

Groovy

def result
switch (obj) {
    case 0:
        result = "Object equals"
        break
    case 11..20:
        result = "Range contains"
        break
    case [1, 2, 3]:
        result = "List contains"
        break
    case Float:
        result = "Class instance"
        break
    case { it % 3 == 0 }:
        result = "Closure"
        break
    case ~'[0-9]{3}':
        result = "Pattern match"
        break
    default:
        result = "Default"
        break
}

Scala

val result = foo match {
  case 0 => "Object equals"
  case i if i == 10 || i == 11 => "Expression"
  case i: Int => s"Class instance holds $i"
  case List(1, 2, _*) => "List contains"
  case Number(n) => s"Case class holds $n"
  case t: {def length: Int} => "Duck type"
  case _ => "Default"
}

Kotlin

var result = when (foo) {
    0 -> "Object equals"
    3, 10 -> "Or"
    in 11..20 -> "Range contains"
    is Date -> "Class instance"
    !in 4..30 -> "Range not contain"
    else -> "Default"
}

3rd Party Libraries

Scala

Kotlin

Who use these languages?

TODO

Community Support

  • Java Native Support
  • Groovy Official Support
  • Scala No Support
  • Kotlin Official Support

Conclusion

The futher comparison to be continue...

You might also like...
To-Do-List - Create a To Do List-style App from scratch and drive the entire development process using Kotlin
To-Do-List - Create a To Do List-style App from scratch and drive the entire development process using Kotlin

To-Do-List! Crie um App no estilo "To Do List" do zero e conduza todo o processo

A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles

This repository serves as template and demo for building android applications for scale. It is suited for large teams where individuals can work independently on feature wise and layer wise reducing the dependency on each other.

Aggregated Android news, articles, podcasts and conferences about Android Development
Aggregated Android news, articles, podcasts and conferences about Android Development

DroidFeed Curated news feed for Android Developers! Stay up to date with the latest Android Development news. Built for the Android developer communit

A project that showcases best practices for MAD (Modern Android Development).

MAD Dictionary Is this dictionary mad? Well yes, but actually no. MAD = Modern Android Development, this app is built using the very new and very awes

Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.
Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.

Kotlin-Everywhere: Kotlin Programming Language Cross-Platform Development This is still a WIP but the idea is to create a tiny KOTLIN project that cou

A single screen app learn in google basic Android Development course.

Project: Lemonade App - Starter Code Starter code for the first independent project for Android Basics in Kotlin Introduction This is the starter code

This is a GitHub template repository intended to kickstart development on an Android application.

Android App Template This is a GitHub template repository intended to kickstart development on an Android application. This project comes set with a h

 Futurama Quotes demonstrates modern Android development implements MVVM architecture
Futurama Quotes demonstrates modern Android development implements MVVM architecture

This application allows you to see futurama quotes. You can search quotes according to the character that said it as well. Futurama Quotes demonstrates modern Android development implements MVVM architecture.

 🗡️ Deddit demonstrates modern Android development with Hilt, Coroutines, Flow, Jetpack, and Material Design based on MVVM architecture
🗡️ Deddit demonstrates modern Android development with Hilt, Coroutines, Flow, Jetpack, and Material Design based on MVVM architecture

Deddit demonstrates modern Android development with Hilt, Coroutines, Flow, Jetpack (ViewModel,Paging3), and Material Design based on MVVM

Comments
  • Kotlin extensions

    Kotlin extensions

    Why is the kotlin code without these extensions https://kotlinlang.org/docs/tutorials/android-plugin.html ? it would reduce your code a bit and make it more readable imo

    opened by lub0s 3
  • Create SBT builds for all Java, Scala and Kotlin projects

    Create SBT builds for all Java, Scala and Kotlin projects

    Created an SBT build for all 3 languages, I could have used sbt-android-gradle but I wanted to demonstrate the full sbt experience: shorter build files, faster builds. Fixes #1

    java 8 seconds kotlin 15 seconds scala 18 seconds

    Full output included below

    java

    [pfnguyen@galactica AndroidDemoIn4Languages] $ cd JavaAndroid/
    [pfnguyen@galactica JavaAndroid] $ sbt app/clean app/android:package
    [info] Loading global plugins from C:\Users\pfnguyen\.sbt\0.13\plugins
    [info] Loading project definition from C:\Users\pfnguyen\src\AndroidDemoIn4Languages\JavaAndroid\project
    Resolving default:javaandroid-build:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Set current project to javaandroid (in build file:/C:/Users/pfnguyen/src/AndroidDemoIn4Languages/JavaAndroid/)
    [success] Total time: 0 s, completed May 5, 2016 10:55:32 AM
    Resolving app:app:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Collecting resources
    [info] Performing full resource merge
    [info] Processing resources
    [info] Rebuilding all classes because R.java has changed
    [info] Compiling 6 Java sources to C:\Users\pfnguyen\src\AndroidDemoIn4Languages\JavaAndroid\app\target\android\intermediates\classes...
    [info] Packaging C:\Users\pfnguyen\src\AndroidDemoIn4Languages\JavaAndroid\app\target\android\intermediates\classes.jar ...
    [info] Packaging resources: resources-debug.ap_
    [info] Done packaging.
    [info] Generating dex, incremental=true, multidex=false
    [warn] Android SDK updates available, run 'android:update-sdk' to update:
    [warn]     Intel x86 Atom System Image
    [warn]     Google APIs Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Google APIs Intel x86 Atom System Image
    [info] dex method count: 15676
    [info] Packaged: app-debug-unaligned.apk (1.10MB)
    [info] Debug package does not need signing: app-debug-unaligned.apk
    [info] zipaligned: app-debug.apk
    [success] Total time: 8 s, completed May 5, 2016 10:55:41 AM
    

    kotlin

    [pfnguyen@galactica JavaAndroid] $ cd ../KotlinAndroid/
    [pfnguyen@galactica KotlinAndroid] $ sbt app/clean app/android:package
    [info] Loading global plugins from C:\Users\pfnguyen\.sbt\0.13\plugins
    [info] Loading project definition from C:\Users\pfnguyen\src\AndroidDemoIn4Languages\KotlinAndroid\project
    Resolving default:kotlinandroid-build:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Set current project to kotlinandroid (in build file:/C:/Users/pfnguyen/src/AndroidDemoIn4Languages/KotlinAndroid/)
    [success] Total time: 0 s, completed May 5, 2016 10:56:01 AM
    Resolving app:app:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Collecting resources
    [info] Performing full resource merge
    [info] Processing resources
    [warn] Android SDK updates available, run 'android:update-sdk' to update:
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Google APIs Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Google APIs Intel x86 Atom System Image
    [info] Rebuilding all classes because R.java has changed
    [info] Compiling 3 Kotlin sources
    [warn] Variable 'holder' initializer is redundant
    [info] Compiling 3 Java sources to C:\Users\pfnguyen\src\AndroidDemoIn4Languages\KotlinAndroid\app\target\android\intermediates\classes...
    [info] Packaging C:\Users\pfnguyen\src\AndroidDemoIn4Languages\KotlinAndroid\app\target\android\intermediates\classes.jar ...
    [info] Packaging resources: resources-debug.ap_
    [info] Done packaging.
    [info] Generating dex, incremental=true, multidex=false
    [info] dex method count: 23185
    [info] Packaged: app-debug-unaligned.apk (1.50MB)
    [info] Debug package does not need signing: app-debug-unaligned.apk
    [info] zipaligned: app-debug.apk
    [success] Total time: 15 s, completed May 5, 2016 10:56:16 AM
    

    scala

    [pfnguyen@galactica KotlinAndroid] $ cd ../ScalaAndroid/
    [pfnguyen@galactica ScalaAndroid] $ sbt app/clean app/android:package
    [info] Loading global plugins from C:\Users\pfnguyen\.sbt\0.13\plugins
    [info] Loading project definition from C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\project
    Resolving default:scalaandroid-build:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Set current project to scalaandroid (in build file:/C:/Users/pfnguyen/src/AndroidDemoIn4Languages/ScalaAndroid/)
    [success] Total time: 0 s, completed May 5, 2016 11:00:17 AM
    Resolving app:app_2.10:0.1-SNAPSHOT
    Resolution done
    Fetching artifacts
    Fetching artifacts: done
    [info] Collecting resources
    [info] Performing full resource merge
    [info] Processing resources
    [info] Rebuilding all classes because R.java has changed
    [info] Regenerating TR.scala because R.java has changed
    [warn] action_bar_root was reassigned: android.support.v7.widget.FitWindowsLinearLayout => android.support.v7.widget.FitWindowsFrameLayout
    [warn] status_bar_latest_event_content was reassigned: android.widget.RelativeLayout => android.widget.RelativeLayout => android.widget.LinearLayout
    [info] Compiling 4 Scala sources and 4 Java sources to C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\app\target\android\intermediates\classes...
    [error] [NewApi] target\android\intermediates\classes\com\bookislife\android\scalademo\TypedResource$$anon$2.class:Call requires API level 11 (current min is 9): android.animation.AnimatorInflater#loadAnimator
    [error] lint found 1 error, 0 warnings
    [info] Packaging C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\app\target\android\intermediates\classes.jar ...
    [info] Packaging resources: resources-debug.ap_
    [info] Done packaging.
    [info] Finding dependency references for: com.android.support:support-annotations:23.1.1:default
    [info] Finding dependency references for: com.android.support:support-v4:23.1.1 [info] Finding dependency references for: com.android.support:appcompat-v7:23.1.1
    [info] Finding dependency references for: com.android.support:support-v4:23.1.1 ProGuard, version 5.0
    ProGuard is released under the GNU General Public License. You therefore
    must ensure that programs that link to it (android, ...)
    carry the GNU General Public License as well. Alternatively, you can
    apply for an exception with the author of ProGuard.
    Reading input...
    Reading program jar [C:\Users\pfnguyen\android-sdk-windows\extras\android\m2repository\com\android\support\support-annotations\23.1.1\support-annotations-23.1.1.jar] (filtered)
    Reading program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-support-v4-23.1.1\libs\internal_impl-23.1.1.jar] (filtered)
    Reading program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-appcompat-v7-23.1.1\com.android.support-appcompat-v7-23.1.1.jar] (filtered)
    Reading program jar [C:\Users\pfnguyen\.coursier\cache\v1\https\repo1.maven.org\maven2\org\scala-lang\scala-library\2.10.6\scala-library-2.10.6.jar] (filtered) Reading program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-support-v4-23.1.1\com.android.support-support-v4-23.1.1.jar] (filtered)
    Reading program jar [C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\app\target\android\intermediates\classes.jar] (filtered)
    Reading library jar [C:\Users\pfnguyen\android-sdk-windows\platforms\android-23\android.jar]
    Reading library jar [C:\Users\pfnguyen\android-sdk-windows\platforms\android-23\optional\org.apache.http.legacy.jar]
    Initializing...
    Ignoring unused library classes...
      Original number of library classes: 3880
      Final number of library classes:    988
    Shrinking...
    [warn] java.net.UnknownHostException: dl.google.com
    [warn] Android SDK updates available, run 'android:update-sdk' to update:
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Google APIs Intel x86 Atom System Image
    [warn]     Intel x86 Atom System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Google APIs Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom_64 System Image
    [warn]     Intel x86 Atom System Image
    Removing unused program classes and class elements...
      Original number of program classes: 6439
      Final number of program classes:    1323
    Writing output...
    Preparing output jar [C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\app\target\android\intermediates\proguard\classes.proguard.jar]
      Copying resources from program jar [C:\Users\pfnguyen\android-sdk-windows\extras\android\m2repository\com\android\support\support-annotations\23.1.1\support-annotations-23.1.1.jar] (filtered)
      Copying resources from program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-support-v4-23.1.1\libs\internal_impl-23.1.1.jar] (filtered)
      Copying resources from program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-appcompat-v7-23.1.1\com.android.support-appcompat-v7-23.1.1.jar] (filtered)
      Copying resources from program jar [C:\Users\pfnguyen\.coursier\cache\v1\https\repo1.maven.org\maven2\org\scala-lang\scala-library\2.10.6\scala-library-2.10.6.jar] (filtered)
      Copying resources from program jar [C:\Users\pfnguyen\.android\sbt\exploded-aars\com.android.support-support-v4-23.1.1\com.android.support-support-v4-23.1.1.jar] (filtered)
      Copying resources from program jar [C:\Users\pfnguyen\src\AndroidDemoIn4Languages\ScalaAndroid\app\target\android\intermediates\classes.jar] (filtered)
    [info] Creating proguard cache: proguard-cache-f75b3a6d5279987c664b74dc929a9f07ade81fe1.jar
    [info] Generating dex, incremental=false, multidex=false
    [info] dex method count: 11536
    [info] Packaged: app-debug-unaligned.apk (946.57KB)
    [info] Debug package does not need signing: app-debug-unaligned.apk
    [info] zipaligned: app-debug.apk
    [success] Total time: 18 s, completed May 5, 2016 11:00:35 AM
    
    opened by pfn 1
Owner
Sidney Xu
Sidney Xu
StarkNet SDK for JVM languages (java, kotlin, scala)

☕ starknet jvm ☕ StarkNet SDK for JVM languages: Java Kotlin Scala Clojure Groovy Table of contents Documentation Example usages Making synchronous re

Software Mansion 29 Dec 15, 2022
Scala 3 Standard Library with bracket syntax.

Scala 3 Library with braces Scala adds a terrible new feature, optional braces, which allow use indentation instead of braces. The new syntax is widel

Glavo 10 Dec 30, 2021
A multiversion gradle scala plugin

WIP Work in Progress: Learning how to write a gradle plugin. A Scala multiversion plugin Inspired by: https://github.com/ADTRAN/gradle-scala-multivers

Ross Lawley 1 Dec 8, 2021
An introductory dynamics to Test Driven Development (TDD)An introductory dynamics to Test Driven Development (TDD)

tdd-demo Nesse hands-on teremos uma dinâmica introdutória a Test Driven Development (TDD), ou desenvolvimento orientado por testes. instruções 1 - Clo

Plataforma Impact 1 Jan 15, 2022
A generic library for quick development in Spring Boot using Java Generics.

slinky Now in Kotlin! A generic library for quick development in Spring Boot using Java Generics. Why "slinky"? A slinky is a precompressed helical sp

Paulo Elienay II 33 Dec 14, 2022
Shreyas Patil 2.2k Jan 4, 2023
📒 NotyKT is a complete 💎Kotlin-stack (Backend + Android) 📱 application built to demonstrate the use of Modern development tools with best practices implementation🦸.

NotyKT ??️ NotyKT is the complete Kotlin-stack note taking ??️ application ?? built to demonstrate a use of Kotlin programming language in server-side

Shreyas Patil 1.4k Dec 26, 2022
Examples for using Kotlin at a basic level for Android application development.

Kotlin Android Jetpack Basics Ejemplos para usar Kotlin a nivel básico para el desarrollo de aplicaciones Android. Kotlin Android Jetpack Basics Acerc

José Luis González Sánchez 2 Jun 28, 2022
KataContacts written in Kotlin. The main goal is to practice Clean Architecture Development

KataContacts written in Kotlin We are here to practice Clean Architecture Development. Clean Architecture is a way of structuring code. We are going t

Karumi 48 Oct 3, 2022
Spikot is a Kotlin library to make Spigot development easier

Spikot is a Kotlin library to make Spigot development easier Using Spikot Installation To use spikot add the following to your build.gradle.kts file.

Cody 1 Oct 30, 2021