I’ve been developing Android apps in Java since 2011. A year ago I made the switch to KOTLIN and never looked back. There are a lot of advantages: it’s easier to use, maintain and generally makes your life happier and development faster. I’m not going to cover why you should choose KOTLIN over Java here — just Google “Switch from Java to Kotlin” and you’ll get all the info you want. This article is written for programmers who already know about the existence of this awesome language but don’t really know where to start with when it comes down to practical stuff like setting up IntelliJ IDEA for their project or debugging an error when they encounter it. To know more check RemoteDBA.com.
Let me start by saying that I’m not a Scala or Kotlin specialist. I work as a full-time Android developer and take every chance to experiment with the new things that happen in our world — always looking for the simpler solutions. That’s why I love KOTLIN so much: it’s becoming more and more popular, is really well supported by JetBrains (the company behind IntelliJ IDEA) and gives me an excuse to write about it here on Medium — you know; sometimes we all need some inspiration to write down our thoughts!
10 mistakes to avoid when starting your projects in Kotlin:
- Don’t neglect to mention kotlin-stdlib dependency version in your buildscript dependencies section.
The most common error related to this mistake is shown in the following screenshot:
The cure for this disease is to add one line of code into your build.gradle file in the dependencies section, right before applies plugin: ‘kotlin-android’:
- Don’t forget to add JvmTarget parameter while targeting a JRE version in gradle.
You might get IntelliJ IDEA complaining that the selected target doesn’t match the JDK , if you neglect to handle this. In order for your project to be able to build successfully, just add the below line of code into your build.gradle file:
- Don’t forget to register all needed extensions from Kotlin stdlib using kotlin-allopen Gradle plugin
The most common error related with this mistake is shown in the following screenshot:
In order for your project not to complain about missing items from the stdlib, just add these lines of code to your build.gradle file right before apply plugin: ‘kotlin-android’:
- Don’t ignore Gradle errors related to annotation processors
If you receive the following error: Error 🙁 53, 0) error: The Kotlin compiler does not support annotation processing. Please compile your Java code together with Kotlin code or configure Kotlin compiler to use the standard javac compiler, Note that this dependency may be included in future versions. You can read more about it here. Just register com.fasterxml.jackson as a kapt dependency like below (be careful to put it right after other dependencies). Beware though, not all of your project’s dependencies will work properly without registering kapt at first place! Here is how it looks like on my buildscript dependencies: Now, all you need to do is to click on the “Run” button and everything should work as expected.
- Don’t neglect using retrolambda Gradle plugin
If you don’t use this plugin, IntelliJ IDEA will complain of usage of lambda expressions in your code: Error 🙁 45, 23) error: Argument 1: incompatible with Expanded Function Type Use -Xlint:all,-get-class if necessary to suppress it. To solve that just add this line into your buildscript dependencies section: And then — run the application on a device or emulator, everything should seem fine! 🙂
- Try not to ignore unresolved Kotlin extension function errors
You might get some interesting errors like this one: Error :(1, 9) error: cannot find class/field: ‘String’ (Make sure you import it and provide a valid path to the file containing the missing element. You can read more about this here. To solve that just add these lines of code into your build.gradle file:
- Don’t forget to use Kotlin Android Extensions Gradle plugin
A lot of people neglect using this plugin because they have no idea what is all those useful things included in it. This is basically a must-have tool for every Android developer who wants to write cleaner code without compromising speed or experience! Just go ahead and follow the documentation on how to integrate it with your project…easy as 🙂
- Try not to ignore unresolved Kotlin dependency errors
Just add these lines of code into your build.gradle file:
- Try not to ignore Gradle version mismatch warnings, produced by the kotlin-android Gradle plugin on old or newer versions of Android Studio
In order for your project not to complain about this, just add the line shown below in your build gradle file:
- Don’t forget to add non existing imports from IntelliJ IDEA inspections just add these lines of code into your build script dependencies section: You can read more about this here.
Conclusion:
That was all with the most common mistakes that I have seen people doing while writing their first Java-Kotlin apps! Do you have any that I missed? Let me know in the comments, I would love to hear about them 🙂
As I said at the top of the blog post, these are only 10 most common mistakes. If you want to see more, make sure to check out this awesome article, it’s a good read!