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.

https://antonioleiva.com/architecture-components-kotlin/

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.

https://dzone.com/articles/variance-immutability-and-strictness

Note:

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.

https://hackernoon.com/how-to-write-your-first-program-in-kotlin-8554cb526158

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.

1. https://www.udemy.com/kotlin-for-absolute-beginners/

2. https://medium.com/jsguru/the-sensible-ravings-of-a-functional-programming-fanboy-75495985b4d5

3. Android Studio 3.0 https://www.raywenderlich.com/177535/beginning-android-development-with-kotlin-part-two-using-android-studio

4. Data class viewmodel made immutable: https://medium.com/@trionkidnapper/viewmodel-and-kotlin-data-class-7d3a3b854805

5. Explicit / Implicit Receiver : “this“, “superhttps://blog.kotlin-academy.com/programmer-dictionary-implicit-receiver-vs-explicit-receiver-da638de31f3c

6. Six Syntactic Sugars: (sealed class etc) https://medium.com/grand-parade/6-magic-sugars-that-can-make-your-kotlin-codebase-happier-part-1-ceee3c2bc9d3

7. Kotlin a la FP : “maybe” etc

https://blog.plan99.net/kotlin-fp-3bf63a17d64a

8. Kotlin : Nothing Type & Either
https://proandroiddev.com/kotlins-nothing-type-946de7d464fb

9. Complete guide to learn kotlin for android development

https://blog.mindorks.com/a-complete-guide-to-learn-kotlin-for-android-development-b1e5d23cc2d8

10. Try Exception:

https://dzone.com/articles/kotlin-try-type-for-functional-exception-handling

11. Generics: In / Out Type Variants:

https://android.jlelse.eu/in-and-out-type-variant-of-kotlin-587e4fa2944c?gi=1d7e2957c9be

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:

https://www.mappingthejourney.com/single-post/2017/10/26/episode-12-interview-with-andrey-breslav-lead-language-designer-of-kotlin/

2. Android Studio 3.0 Released : https://www.androidauthority.com/android-studio-3-released-810099/

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

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

https://thenextweb.com/dd/2017/10/27/android-studio-3-0-brings-support-for-kotlin-java-8-and-instant-Features

2c: Gradle upgrade

https://zhuanlan.zhihu.com/p/30722989

3. Inner / Infix Function

http://thetechnocafe.com/more-about-functions-in-kotlin/

4. Test (Mockito)

https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-1-6d0b04017e80

https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-2-f3594e5e7bfd

Kluent library:

https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-3-104e1c9f77cc

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

View at Medium.com

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

https://blog.simon-wirtz.de/kotlin-features-miss-java/

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

https://blog.simon-wirtz.de/kotlin-conventions-operator-overloading/

8. JVM Byte Code Generation:

https://blog.simon-wirtz.de/kotlin-on-the-jvm-byte-code-generation/

9. Reified Types

https://blog.simon-wirtz.de/kotlin-reified-types/

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

https://dzone.com/articles/conventional-interfaces-in-functional-programming

11.Kotlin中extensions的用法

https://zhuanlan.zhihu.com/weiweidaolai/27537731

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

https://zhuanlan.zhihu.com/aqi00/28302792

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

https://zhuanlan.zhihu.com/aqi00/27744799

14. ViewPager (Horizontal Swipe)

https://www.raywenderlich.com/169774/viewpager-tutorial-android-getting-started-kotlin

15. Kotlin 1.2 Beta & Multiplatform iOS

https://blog.jetbrains.com/kotlin/2017/11/kotlinconf-keynote-recap/

16. Kotlin Edu ( Android Studio 3.0)

https://www.jetbrains.com/education/kotlin-edu/

17. Kotlin Style Guide

https://android.github.io/kotlin-guides/style.html

18. Android Layout FoundamentalConstraintLayout

https://developer.android.com/guide/topics/ui/declaring-layout.html

19. Android SDK

https://www.raywenderlich.com/171148/android-sdk-versions-tutorial-2

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

https://jaxenter.com/javalin-1-0-is-here-138750.html

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

View at Medium.com

22. Function literals with receiver

https://blog.simon-wirtz.de/function-literals-with-receiver-quick-introduction/#page

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

https://mindorks.com/course/advance-kotlin/chapter/id/1

24. Android Intent

https://www.raywenderlich.com/171071/android-intents-tutorial-kotlin

25. Top kotlin tutorials

View at Medium.com

26. Kotlin Contexts & Shared Preferences

https://blog.stylingandroid.com/kotlin-contexts-sharedpreferences/

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

https://blog.kotlin-academy.com/superpowers-of-kotlin-delegation-e6bd9ff8026c

28. Functions or Properties Reference (cool *)

View at Medium.com

29. Web Framework ktor:

https://blog.simon-wirtz.de/web-app-kotlin-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: https://learnxinyminutes.com/docs/kotlin/

1. Sequence over Collection.

2. Sealed Class

3. Object => new Class

https://medium.com/@manuelvicnt/kotlin-education-d0b958740d6a

4. Function Literal (Lambda Expression)

https://tech.io/playgrounds/6973/kotlin-function-literal-with-receiver

5. “Either” Monad

https://kotlin.link/articles/Exploring-an-Either-Monad-in-Kotlin.html

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

View at Medium.com

7. Function Memoization

View at Medium.com

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

https://android.jlelse.eu/how-to-remove-all-from-your-kotlin-code-87dc2c9767fb

9. Generic Extension Function (aka Polymorphism)

https://dzone.com/articles/kotlin-generic-extension-functions

10. Data Classes:

http://thetechnocafe.com/kotlin-data-class-tutorial/

11. Co-routines

https://hellsoft.se/simple-asynchronous-loading-with-kotlin-coroutines-f26408f97f46

12. Simple Weather App

Simple weather forecast app using Kotlin, Retrofit and RxJava2

https://search.app.goo.gl/1j3j

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…

http://kotlinlang.org/docs/reference/multiplatform.html

Kotlin 1.2 Beta Release 30/9/2017:

https://blog.jetbrains.com/kotlin/2017/09/kotlin-1-2-beta-is-out/

Kotlin / Java With Android Studio 2017

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

https://academy.antonioleiva.com/courses/kotlin-android-developers/lectures/3155590

Exercises:

https://academy.antonioleiva.com/courses/kotlin-android-developers/lectures/3155595#/questions/1

Kotlin:

https://www.raywenderlich.com/132381/kotlin-for-android-an-introduction

recyclerView

https://www.raywenderlich.com/126528/android-recyclerview-tutorial

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

https://developer.android.com/studio/build/shrink-code.html

Java:

How to Start Programming – Lesson 1

Java & Android Studio

https://novicedock.com/learn/computer-science/android-development

Documentation:

https://novicedock.com/learn/computer-science/android-development/project-configuration-screens-and-basic-layouts

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).

https://dev.to/sciencesoft/kotlin-vs-swift-are-android-and-ios-moving-towards-creating-a-universal-language?from=timeline

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.

https://dzone.com/articles/kotlin-on-the-jvm-how-can-it-provide-so-many-featu

[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.

https://academy.realm.io/posts/altconf-2017-james-majors-functional-programming-introduction

Kotlin: apply, run, let, also

https://dzone.com/articles/examining-kotlins-also-apply-let-run-and-with-intentions

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

https://blog.plan99.net/kotlin-fp-3bf63a17d64a

Why Kotlin ?https://www.programiz.com/sites/tutorial2program/files/kotlin.jpg
Kotlin Tutorials

Get Started: https://developer.android.com/kotlin/get-started.html
https://kotlinlang.org/docs/tutorials/

https://developer.android.com/kotlin/get-started.html

Hands-on: 

https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Hello,%20world!/Task.kt
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)

http://www.androidauthority.com/introduction-to-kotlin-for-android-775678/

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

https://blog.jetbrains.com/kotlin/2015/11/webinar-recording-functional-programming-with-kotlin/

Development IDE: “Android Studio”

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

Resources:

https://antonioleiva.com/kotlin-android-developers-book/

https://www.amazon.com/gp/aw/s/ref=is_s_ss_i_5_6?k=kotlin+for+android+developers&sprefix=kotlin

https://m.signalvnoise.com/using-kotlin-to-make-android-apis-fun-again-14690975afb6

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

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

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

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

4. 把 Cheatsheet 打出来贴在显示器旁边没事看看:https://pbs.twimg.com/media/C0yRbHLUUAAgacl.jpg

5. 文字版本 Cheatsheet: https://gist.github.com/dodyg/5823184 https://gist.github.com/dodyg/5616605

https://www.zhihu.com/question/37288009/answer/172132665

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