Android Architecture Simplified with Kotlin

I hate Android Architecture since it is based on OO Java, Kotlin is FP cousin of Java, hopefully it could simplify the Android Architecture by getting rid of the complex OO architecture. Here Antonio attempts to simplify it with more terse Kotlin codes, albeit still in the OO architecture spirit.

Kotlin: Co-Variance, Contra-Variance, Invariance, Immutability

Kotlin is a functional programming (FP) language which has the Category concept of co-Variance, contra-Variance. Java is object-oriented not FP, it has only Invariance.


Covariance and contravariance are terms that refer to the ability to use a less derived (less specific = CoVariance eg. Apple instead of Fruit) or more derived type (more specific = ContraVariance, eg. Gala instead of Apple) than originally specified.

Generic type parameters support covariance and contravariance to provide greater flexibility in assigning and using generic types.

  • Invariance

    Means that you can use only the type originally specified; so an invariant generic type parameter is neither covariant nor contravariant. (Java is always invariance.)

Learn Kotlin sans Java

Kotlin is Object-Oriented Java + Functional Programming.

Much as we hate Java, Kotlin can’t run away from the Java “ghost” especially in Android development using the Java API and OO design patterns.

This tutorial, however, teaches you Kotlin “sans Java“, a great news for novices without Java knowledge.

I like this Kotlin teaching approach for 2 reasons:

  1. No need to struggle with Java /Android OO architecture until later stage;
  2. No need to be intimated by the complicated “Android Studio” editor.

Kotlin for Beginners

Most Kotlin tutorial beginners make the mistake of bringing in the legacy Java Object-Oriented thinking into Kotlin — blame the auto-convert feature in Android Studio !

Kotlin, likes Haskell, Scala, has the powerful FP (Functional Programming) as the other side of the dual “FP&OO” features. You can write beautifully re-usuable “bottom-up” software components in FP style, rather than in the GoF pattern adopted by O-O – more lengthy, complex, “top-down”, software components.



3. Android Studio 3.0

4. Data class viewmodel made immutable:

5. Explicit / Implicit Receiver : “this“, “super

6. Six Syntactic Sugars: (sealed class etc)

7. Kotlin a la FP : “maybe” etc

8. Kotlin : Nothing Type & Either

9. Complete guide to learn kotlin for android development

10. Try Exception:

11. Generics: In / Out Type Variants:

Explore Kotlin’s Advanced Functional Programming

Since May 2017 Kotlin released by Google, 12.8% Java developers have converted to Kotlin, yet they still keep to the OO spirit of Java (for Interoperability) , not taking full advantage of FP capability of Kotlin. The OO Design Patterns of Android Java still being used instead of the FP more elegant “Monadic” Design.

1. Interview with Kotlin Designer:

2. Android Studio 3.0 Released :

2a. Android Studio v3.0 with Kotlin built-in & many improvements.

(The previous stable v2.3 needs seperate Kotlin plug-in)

2c: Gradle upgrade

3. Inner / Infix Function

4. Test (Mockito)

Kluent library:

5. Function vs Procedure vs MethodKotlin simplifies all 3 into 1 : Function which always returns a value or UNIT.

View at

6. Kotlin has which Java lacks … “with“, “?”, …

7. Kotlin Operator Overloading aka “Convention”: ‘plus’ / ‘+’

8. JVM Byte Code Generation:

9. Reified Types

10. SICP: Sequence as conventional interfaces: eg. flatmap, map, reduce, fold


12. Generic : Kotlin入门(11)江湖绝技之特殊函数

13. Array <String>: Kotlin入门(4)声明与操作数组

14. ViewPager (Horizontal Swipe)

15. Kotlin 1.2 Beta & Multiplatform iOS

16. Kotlin Edu ( Android Studio 3.0)

17. Kotlin Style Guide

18. Android Layout FoundamentalConstraintLayout

19. Android SDK

20. Javalin v1.0 – Web Framework for Java + Kotlin

21. Ten Modern Features (Kotlin, Clojure, Javascript, Swift…)

View at

22. Function literals with receiver

23. Sealed classes: (restricted types no “else”)

24. Android Intent

25. Top kotlin tutorials

View at

26. Kotlin Contexts & Shared Preferences

27. Delegation – but not Inheritance (which takes all)

28. Functions or Properties Reference (cool *)

View at

29. Web Framework ktor:

Advanced Kotlin Education

Kotlin’s real power is to explore its Functional Programming features, besides the “cosmetic” improvement of concise codes to avoid Java’s annoying boilerplates.

Kotlin Cheatsheet:

1. Sequence over Collection.

2. Sealed Class

3. Object => new Class

4. Function Literal (Lambda Expression)

5. “Either” Monad

6. Kotlin “DI” Dependency Injection – the “Reader” Monad vs Java Dagger:

View at

7. Function Memoization

View at

8. Remove all ugly “!!” with these 6 Kotlin techniques

9. Generic Extension Function (aka Polymorphism)

10. Data Classes:

11. Co-routines

12. Simple Weather App

Simple weather forecast app using Kotlin, Retrofit and RxJava2

Shared from my Google feed

Kotlin 1.2 Multi-Platform Project

This language feature of writing only once and run on many platform was promised by Java in 1999 through JVM.

Kotlin, a successor of Java supported officially by Google since May 2017, goes further than Java on ambitious multi-platform, not only through Android’s Delvick JVM (a modified version of Oracle’s JVM), also on JavaScript engine (in supported browsers), and compiled to (future) native codes on Microsoft Windows…

Kotlin 1.2 Beta Release 30/9/2017:

Kotlin / Java With Android Studio 2017

Antonio Leiva; Kotlin for Android Developers (Online) – preview




ProGuard (Shrink Apps Code Size to remove unnecessary support libraries)


How to Start Programming – Lesson 1

Java & Android Studio


Mobile Apps’ 2 Camps: Android Kotlin vs iOS Swift

Today mobile apps world is divided into 2 camps:

  • The original Apple iOS camp with 2000s ObjectiveC (improved version Swift from 2015), and
  • The Google Android with 1997 Java “JVM” (improved version Kotlin from 18 May 2017).

Both improved languages (Swift & Kotlin) use Functional Programming features eg. “Type” = Category Math, Data immutability (can’t change at run-time for safe concurrency), execution by composition of “componentised” Functions, including ‘advanced’ aka ‘higher-order’ functions eg reduce, map, filter, etc… which are Category-Math Functors, ‘Monoids’ and its cousin ‘Monads’ for handling side-effects (printl, update database, error msg…).

From 2018 we see these 2 mobile camps unifying into 1 language independent of vendors like the Fortran/Cobol in early 1970s, but which one ? may be one of the “Grand-Fathers” of the “Pure” Functional Programming Haskell (1993) would be adopted by an independent consortium ala W3C Consortium (WWW).

JVM Language – Kotlin

Kotlin (Google official support since May 2017) is one of the few JVM (Java Virtual Machine) languages (eg. Scala, Java, etc *), but with added nice “FP” Functional Programming features (See Reference # below).

* Note: Other FP languages like “Haskell”, “Clojure” (Lisp dialect) are unfortuantely not (yet) JVM compatible, hence can’t run on Android. Scala is a hybrid of “Object-Oriented + FP”, which runs on JVM (slow ?).

JVM machine recognises any “.class file” which contains the Java “bytecodes” executable on any platforms (Windows, Linux, Unix, MacOS, Android, iOS).

Any JVM language must produce ” .class” regardless whether it is Java or Kotlin or Scala.

If you intend to run on Android platform but hate Java (like I do), then choose another JVM language such as Kotlin.

[Reference #]
Nice Introduction to Functional Programming : Although FP is based on Math “Category Theory” using abstract concepts : Monad, Monoid, Functor (eg. Map), you don’t need to know it well before coding in FP.

Kotlin: apply, run, let, also

Google officially supports Kotlin from May 2017

Kotlin is the “New Java” officially supported by Google from May 2017! It is less verbose (罗唆) than Java which is clumsy with boilerplates (样板),  interoperates with Java on JVM, with modern functional programming features, and most importantly, it is Multi-Platform : Java, Android, Javascript,  and future versions run as native codes on iOS, MacOS and Linux (Microsoft – work in progress). This eliminates the current headache of having to re-write the same applications for different platforms in different languages.

Google makes Kotlin a first-class language for writing Android apps

Why Kotlin ?
Kotlin Tutorials

Get Started:

Download Android Studio using Kotlin (2 ways)

  1. Android Studio 3.0 Beta (Buggy Preview, not encouraged!)
  2. Stable Android Studio (2.3.2), manually add Kotlin plug-in (Preferred way)

Functional Programming with Kotlin: ( Map, filter, apply, lazy sequence, reactive programming…)

Development IDE: “Android Studio”

  • It is a version of “Intelligent IDEA”
  • Bundled with Build Tool “Gradle”


知乎: 最为有效的Kotlin学习方法:

1. 先快速浏览:Basic Syntax – Kotlin Programming Language

2. 对于Java选手,附加看这个:From Java to Kotlin

3. 上手跟着这个tutorial一步一步完成它的练习:Try Kotlin (Koan)

4. 把 Cheatsheet 打出来贴在显示器旁边没事看看:

5. 文字版本 Cheatsheet:

知乎: Kotlin 之旅 (, sequence, filter, map …)