• On Strategies to apply Kotlin to Existing Java code
  • Originally written by Enrique Lopez Manas
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Luolc
  • Proofread by: Skyar2009, PhxNirvana

A policy to apply Kotlin to existing Java code


A policy to apply Kotlin to existing Java code

Things have been going crazy since Google announced at I/O that Android Studio will support Kotlin by default. Over the past two weeks, the number of subscribers to Kotlin’s weekly mailing list has grown by more than 20% and the number of article submissions has grown by more than 200%. The offline conversation I organized (the Kotlin Munich user group) saw a huge increase in participation. All this has been accompanied by an explosion of growth in the developer community as a whole.

A trend that will continue to grow.

At the moment, it is clear what the future holds. Despite Google’s promise to continue supporting Java, the legal battle between Google and Oracle, and the clear fact that Kotlin is a much cleaner, more efficient, and more powerful language, are signaling your direction. I found the following tweet rather prescient.

When I appeared on kotlin-related discussions and communities a few months ago, the question I was probably most frequently asked was whether now was a good time to move to Kotlin. My answer remains the same: yes. There are many benefits to doing a Kotlin migration, and few disadvantages. The only technical side effect I can think of is that the number of methods will increase as the Kotlin standard library (currently) adds 7,191 new methods. Taken together, this is a perfectly acceptable deficiency.

Now that the answer to that question was an undisputed yes, I realized another question was emerging: What steps should I take to get started with Kotlin?

The purpose of this article is to provide some thoughts of your own for those who are wondering where to start or looking for inspiration.

1.- Start with testing

Yes, I know the tests are limited. Unit tests are exactly what you test: individual units and modules. It’s hard to develop a complex architectural web when all you have is a bunch of separate classes and maybe a few helper classes. But it’s a very cheap and efficient way to build awareness and expand in a new language.

One of the most common arguments I hear against Kotlin is to avoid deploying Kotlin code in a production environment. While this seems to me to be a very biased view, I want to emphasize to you that if you start with testing, none of the code will actually be deployed (into production). Instead, the code may be used in your continuous integration environment, which is a way to expand your knowledge.

Start writing new tests in Kotlin that directly collaborate and interact with other Java classes. When you have free time, you can migrate your Java classes, examine the generated code and make manual changes as needed. In my experience, 60% of the conversion code is ready-to-use, and the percentage is even higher for simple classes with no complex functionality. I found this to be a very safe scenario to start with as a first step.

2.- Migrate existing code

You’ve already started writing some Kotlin code. You learn something about the basics of language. Now you’re ready to use Kotlin in production!

When you first start using them in a production environment, it’s very efficient to start with low-coupling classes (Dtos and data classes). These classes have very little impact and can be easily refactored in a very short time. This is the perfect time to learn about data classes and drastically reduce your code.

This is the PR you want to launch.

After that, migrate the single class. It could be a class like LanguageHelper or Utils. Although they are invoked in many places, such classes generally provide only limited functionality with limited impact and dependencies.

At some point, you’ll feel comfortable enough addressing the larger and more core classes in your architecture. Don’t be afraid. Pay special attention to nullability, one of the most important features in Kotlin. If you’ve been programming Java for years, it requires a new way of thinking. But trust me, new programming paradigms will eventually form in your head.

Remember: you don’t need to force an entire code base to migrate. Kotlin and Java interact seamlessly, and now you don’t have to make your code base 100% Kotlin. Do it when you feel comfortable enough.

3.- Use Kotlin freely

By this stage you should be writing all your new code in Kotlin. Put it in the past and don’t look back. When you start writing your first feature in pure Kotlin, you’ll need to pay attention to the default parameters in addition to nullability mentioned above. Think more about extending functionality than inheritance. Initiate Pull requests and code reviews, and discuss with your colleagues how you can improve.

Final advice, enjoy!

Resources for learning Kotlin

Listed below are links to resources I have tried and can recommend for learning Kotlin. I especially like books, even though some people hate them. I find it important to read them out loud, and writing and practicing them on a computer helps to sink in.

  1. Kotlin Slack: Lots of JetBrains and Kotlin fanatics gather here.
  2. Kotlin Weekly: I manage a Weekly mailing list of Kotlin related posts.
  3. Kotlin Koans: A series of online exercises that train and strengthen your Kotlin skills.
  4. Kotlin in Action: Book from some Kotlin workers at JetBrains.
  5. Kotlin for Android Developers: a book that focuses on how to use Kotlin to do Android development.
  6. Resources to Learn Kotlin: Google provides more Resources to Learn Kotlin.

I share some ideas about software engineering and life on my Twitter account. If you liked this post or if it really helped you, we’d love for you to share, like, or comment. This is the biggest motivation for amateur writers.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. Android, iOS, React, front end, back end, product, design, etc. Keep an eye on the Nuggets Translation project for more quality translations.