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...
Healthify - An app to track your daily water intake and sleep and boost your work efficiency. Healthify is built using Kotlin and follows all modern android Development practices and hence is a good learning resource for beginners
Healthify - An app to track your daily water intake and sleep and boost your work efficiency. Healthify is built using Kotlin and follows all modern android Development practices and hence is a good learning resource for beginners

Healthify Healthify is an app to track your daily water intake and sleep and boost your work efficiency. Video Introduction 📹 This is a small introdu

A pet project created to practise Android Development skills in Kotlin after finishing multiple courses online.

A pet project created to practise Android Development skills in Kotlin after finishing multiple courses online. The app displays a list of hundreds of characters provided by The Rick and Morty API https://rickandmortyapi.com/. In other screens user can access detailed information about a particular character, such as status, location and episodes. Libraries used in a project: - Kotlin - Jetpack libraries (Navigation, Room, Hilt, Palette) - other: Glide, Retrofit

App for lesson 8 of the Android App Development in Kotlin course on Udacity
App for lesson 8 of the Android App Development in Kotlin course on Udacity

Connect to the Internet - Mars Real Estate This is the toy app for Lesson 8 of t

Android Camper parking app written in kotlin for assignment 2 of Mobile App Development
Android Camper parking app written in kotlin for assignment 2 of Mobile App Development

Technical Report Splash view Application shows a splash screen when opened. The

Asteroid radar app -Second Project from Udacity Advanced Android Development Kotlin Nanodegree
Asteroid radar app -Second Project from Udacity Advanced Android Development Kotlin Nanodegree

Asteroid radar app Using open-source Nasa Api Asteroid Radar is an app to view the asteroids detected by NASA that pass near Earth, you can view all t

This is a food donation android application designed to reduce food wastage by donating excess food to poor or needy people.  Based on Kotlin and currently under development
This is a food donation android application designed to reduce food wastage by donating excess food to poor or needy people. Based on Kotlin and currently under development

FOODONOR This is a food donation android application designed to reduce food wastage by donating excess food to poor or needy people. Based on Kotlin

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

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 Simple and Minimal Quotes Android Application to demonstrate the Modern Android Development tools
A Simple and Minimal Quotes Android Application to demonstrate the Modern Android Development tools

Quotee Android 📑 A Simple and Minimal Quotes Android Application to demonstrate the Modern Android Development tools. Developed with ❤️ by Aminullah

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
A sample Android app which showcases advanced usage of Dagger among other open source libraries.

U+2020 A sample Android app which showcases advanced usage of Dagger among other open source libraries. Watch the corresponding talk or view the slide

Jake Wharton 5.7k Dec 27, 2022
ArchGuard Scanner for scan Git change history, scan source code by Chapi for Java, TypeScript, Kotlin, Go..、Java bytecode use for JVM languages, scan Jacoco test coverage.

Arch Scanner Requirements: JDK 12 Scanner: scan_git - Git commit history scan scan_jacoco - Jacoco scan scan_bytecode - for JVM languages known issues

ArchGuard 27 Jul 28, 2022
A modular and portable open source XMPP client library written in Java for Android and Java (SE) VMs

Smack About Smack is an open-source, highly modular, easy to use, XMPP client library written in Java for Java SE compatible JVMs and Android. Being a

Ignite Realtime 2.3k Dec 28, 2022
A modular and portable open source XMPP client library written in Java for Android and Java (SE) VMs

Smack About Smack is an open-source, highly modular, easy to use, XMPP client library written in Java for Java SE compatible JVMs and Android. Being a

Ignite Realtime 2.3k Dec 21, 2021
An simple image gallery app utilizing Unsplash API to showcase modern Android development architecture (MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit)

Imagine App An simple image gallery app utilizing Unsplash API. Built with ❤︎ by Wajahat Karim and contributors Features Popular photos with paginatio

Wajahat Karim 313 Jan 4, 2023
Shreyas Patil 2.1k Dec 30, 2022
A sample app illustrating Android development using Kotlin with MVVM architecture, Android Jetpack, and other commonly used libraries.

Anime Facts A sample app illustrating Android development using Kotlin with MVVM architecture, Android Jetpack, and other commonly used libraries. Ani

Eugene Javiñas 0 Dec 5, 2021
📒 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 Jan 8, 2023
Taskify - An app to manage your daily tasks and boost your productivity. Taskify is built using kotlin and follows all modern android Development practices and hence is a good learning resource for beginners

Taskify Taskify is an app to manage your daily tasks and boost your productivity Video Introduction ?? This is a small introduction video about Taskif

Vaibhav Jaiswal 101 Jan 4, 2023
The JeTrivia is built on a modern Android Development tech stack with MVVM architecture. Kotlin, Coroutine, Flow, StateFlow, Jetpack Compose, Navigation, Room, Hilt, Retrofit2, OkHttp3, kotlinx.serialization, MockK, Truth

JeTrivia ?? In Progress ?? The JeTrivia application is sample based on MVVM architecture. Fetching data from the network via repository pattern and sa

Tolga Bolatcan 5 Mar 31, 2022