One of the frequently asked questions among Android developers for the usage of Kotlin during conferences is “What’s the best practices to incorporate this platform to any available Android app?”
When you are introducing Kotlin in a team with a handful of staff, learning a new language is a complex endeavor. Below is a guide to help you understand the successful patterns in understanding Kotlin while identifying potential solutions to current challenges in the projects being handled by the bigger teams (part 1).
Hiring qualified Kotlin developers to most teams knowledgeable about Google ensures the Android developer relations group to use the techniques with ease successfully. Two of the best examples include the (1) 2018 Google I/O app, which was rewritten entirely in Kotlin; and (2) Plaid, which is a combination of Java and Kotlin.
Hiring a Kotlin developer champion
To succeed in using Kotlin, designate an expert in your team to focus as a mentor and a go-to person for the staff willing to learn this language. Oftentimes, this individual is the most interested in understanding further the benefits of Kotlin to daily work-related operations.
If this person happens to be you, you should have an open mind to learn as much as you can in a short period about Kotlin and adopting ideas on how to best apply Kotlin into the available applications.
A good attitude on understanding Kotlin is being proactive when sharing your Kotlin background and becoming a resource person to respond to inquiries of the team. This Android developer should actively take part in Java and Kotlin code reviews to implement those modifications that promote Kotlin conventions and undertake language interoperability like nullability annotations.
Learn the Kotlin basic patterns
Although you have a Kotlin Android developer champion with in-depth expertise into the language, every staff in the team should establish a baseline understanding about Kotlin’s usage, best practices, and possible solution how this language is advantageous to your daily office work.
In a team simply starting to get acquainted with Kotlin, take extra effort to identify the available resources to use when learning this innovative language and how it works with Android apps. It is highly advised to begin with Kotlin Koans, a series of mini-activities that promotes the Kotlin’s features. This serves as a handy means to learn Kotlin.
Kotlinlang.org is Kotlin’s official website. This online site provides reference documents when applying the Kotlin standard library and easy-to-follow tutorials for completing various Kotlin tasks. Also, the Android developers’ site is a good repository of several resources regarding how to use Kotlin in Android development.
Create a study team
Once the team is ready and confident to write basic Kotlin, this is the best time to develop a study group. Currently, Kotlin rapidly evolving with several new features in the pipeline. Best examples are Coroutines and Multiplatform. Through constant group discussions, this can assist in understanding further any new concepts about the language features. Also, this provides reinforcement in embracing Kotlin’s best practices in your company’s operations.
Write Kotlin tests
Most Kotlin teams say that writing tests using this language is highly applicable in their projects. Kotlin does not affect the production code and not being bundled with your traditional app package.
Your team has the option to write new tests or shift the current tests using Kotlin. Evaluations are best to use to validate code regressions. These may add confidence level during code refactoring. The tests are useful when transferring the current Java code into Kotlin.
Create new Kotlin code
Prior to converting the available Java code to Kotlin, begin this by combining mini Kotlin pieces to your codebase application. Start with a top-level or small class helper function, ensuring to provide the relevant annotations to the Kotlin code and proper interoperability using the Java code.
Identify APK size impact and establish the functionality
Combining Kotlin to your app may boost both the build times and APK size. It is highly advised to use Proguard to provide that your release APKs are at the right size and to lower any increase in several processes applied. After operating Proguard, how Kotlin impacts your APK size should be minimal, especially at the start.
In both Kotlin-only and mixed-language projects, which are Java and Kotlin written, there is a quite increase in their compile and build times. However, many hired Android developers consider that the high productivity of Kotlin writing is a notable trade-off.
Both the Kotlin and Android teams are fully aware of longer build times and continue to improve this vital part of the development processes, patterns, and solutions. As a user, you should evaluate and identify the build performance impact on your projects.
Upgrade current Kotlin code
When your team becomes adept in using Kotlin, you can begin to shift your existing code to Kotlin. One main option is to start over and rewrite your Kotlin app completely. This approach is adopted using the 2018 Google I/O Android app. However, in most teams, this is not a good option, as they should ship software while applying new technologies. Generally, Kotlin and the Java programming language are both fully interoperable. These enable you to migrate your existing tasks one class at a time.
A classic process is employing the code converter in Android Studio. This shifts the code from a Java file to Kotlin. Also, the IDE provides a way to convert to Kotlin in a Java taken from the clipboard into a Kotlin file. The used converter is not always helpful in developing the most idiomatic Kotlin. A review is necessary for every file. This is the best approach to save time and check how Kotlin appears in your codebase.
Although Java and Kotlin can be 100% interoperable, they are not considered source compatible. It is impossible to create a single file using both Java and Kotlin. It is best to seek the advice of the Kotlin and Android guides about more tips on writing interoperable code for Java and Kotlin.