Introducing Kotlin in an Android Project (BlrDroid Meetup, April 2018)
Kotlin is a great language addition to Android. In this presentation, we discuss details of how we introduced kotlin in a live project, tackled various challenges and finally put it in production.
as an result of when closure if (fragment instanceof HomeFragment) { // Logic here } val view = when (viewType) { 1 -> layoutInflater.inflate(R.layout.item_pax_checkbox, parent, false) else -> layoutInflater.inflate(R.layout.item_pax_type, parent, false) } when(fragment) { is HomeFragment -> { } }
public • All classes are declared final by default. Use open to make classes extendible. • Subclasses should be defined as inner to access variables of outer class(useful in ViewHolders) File level Module Level Everywhere Subclass level
String): String? { val value = this[key] as String return if (value.startsWith("#")) { value } else { this.colorFinder(value) } } val appVersion: String get() = BuildConfig.VERSION_NAME
of interoperability. • void is converted to Unit when calling in Kotlin. • Empty getters and setters are converted to Properties. • Annotations in Java are respected by Kotlin to specify nullability of a certain variable.
getActivity() { return mHost == null ? null : (FragmentActivity) mHost.getActivity(); } Fragment.java(API 27) • context, activity and bundle with Nullable annotation are no longer platform types. • It’s always good to use Annotations on existing java code which could be used in Kotlin
announced with Jack toolchain March 2017 Jack toolchain is deprecated April 2017 Java 8 support is merged into dx toolchain May 2017 Kotlin is announced as first class language for Android Nobody cares about Java 8 anymore
Java List<String> filtered = filter(list, value -> value.startsWith("0")); List<Integer> mapped = map(list, Integer::parseInt); Statically import methods from Kotlin Collection framework • Kotlin Utility methods could be directly used in your Java code. More idiomatic usage in Kotlin, but still…
lint checks for Kotlin. • Android now has a dedicated style guide and interop guide for Kotlin. • https://android.github.io/kotlin-guides/style.html • https://android.github.io/kotlin-guides/interop.html • https://github.com/shyiko/ktlint
Kotlin’s compilation time is slower for clean builds. • However incremental compilation is faster than Java(enabled by default from 1.1.1). • Android Studio builds generally showed a difference of 10-20% as we moved from Gradle 2.14 -> 4.1, Kotlin 1.1.x -> 1.2.x
• Jacoco didn’t provide any coverage reports for Kotlin. • Sonarqube dashboard was affected due to unavailability of code coverage for Kotlin. When we started
class directories. • Sonarqube code coverage works fine as it uses Jacoco binary. • Jacoco has teeny tiny issues with generated methods. • Kotlin is still not supported by Sonarqube officially(third party plugins are present, though) • Quality checks and line-by-line coverage not available on Sonarqube Where we are now
Kotlin files. • Better Kotlin education among developers. • Newer projects migrated to Kotlin completely. • Backend team also want a piece of Kotlin(probably got fed up of our bad java puns). • Human interaction with iOS Developers (Swift <=> Kotlin).