I recently converted one of my personal Java projects, originally written a decade ago, to Kotlin, my favorite programming language. The recommended way to do this is to use an IDE, such as IntelliJ IDEA or Android Studio (which is based on IntelliJ). Without an IDE converter, you'd have to use an online converter or do it manually.
I used IntelliJ IDEA to perform the conversion, one file at a time. The main codebase consists of 32 files, so this wasn't that difficult. The overall process took around 8 hours, spread over a couple of weeks.
I'm writing this article because the process involves more than just pressing "Convert." IntelliJ, as smart as it is, still cannot seamlessly convert a Java 8 file into well-written Kotlin 2 code (specifically, Kotlin 2.1). If you are converting your code, you must make manual edits both to get the code to compile and to make it more Kotlinic. Let's review the biggest caveats you'll encounter when automatically converting your Java project to Kotlin.
Java 8 Stream API Calls Are Not Converted to Proper Kotlin
Java streams are great, but they cannot compare to the fluency of Kotlin's Core API. Thus:
uris.stream().distinct().map(
uri -> new ImageUriAndSize(uri, getImageSize(uri))
).collect(Collectors.toList());
should become:
uris.distinct().map {
ImageUriAndSize(it, getImageSize(it))
}.toList()
However, this conversion is not done automatically. You must make this change manually.
Use of @JvmStatic
The @JvmStatic annotation is quite useful when you will have Java code calling your Kotlin code. However, if the plan is to convert the whole project to Kotlin, there's no need to add this annotation. IntelliJ inserted this annotation anyway.
@JvmStatic is also useful for declaring your main() function inside a companion object. But it would be even better if IntelliJ moved your main() to the top level, as is customary in Kotlin.
Most Objects Become Nullable
In Java, any object can be set to null, and thus all objects are potentially nullable. Only primitives are safe from returning null (although their boxed counterparts are not). When converting code to Kotlin, IntelliJ recognizes these nullable types and accordingly adds a nullable type marker (?) to many variables and return types. This makes sense because these values truly might be null. However, it's not a particularly Kotlinic design, especially if null is not actually a possibility.
In Kotlin, we want to avoid null whenever possible. In fact, we are rewarded for designing functions that never return null. You can return String instead of String?, indicating that your function's return type is guaranteed to be non-null. Users of your non-null function will know that there is no need to use the safe-call operator (?.) or the Elvis operator (?:) on values returned by your function.
In Java, there's not as much benefit to guaranteeing that a method's return type is non-null. Say the method returns a String. Regardless if that String can be null or not, either way you must return String. You can go through the effort of annotating all of your non-null-returning methods with @NotNull, but that's more effort than what must be done in Kotlin ((i.e. simply removing a question mark).
In my project, a lot of my Java methods used null as a valid return value and accepted null parameters, only checking for null when absolutely necessary. You never know when an object might be null, despite hints from annotations, so null checks are needed.
All this is to say that while IntelliJ's Kotlin converter may correctly identify nullable object types, you may want to redesign your variables and return types to be non-null. This requires additional work.
Inheritance Issues
Auto-converting Java to Kotlin may cause inheritance issues, due in no small part to the nullability problem. If a function in your interface allows nullable parameters, but an implementation expects a non-null parameter, then the implementation no longer correctly implements that interface and must be manually updated. For example, if your interface has a function:
fun downloadAll(thumbLinks: MutableList<String?>?)
and you decide that the parameter should never be null nor contain null elements:
override fun downloadAll(thumbLinks: MutableList<String>) { … }
then that declaration won't compile. The override modifier is inaccurate because the function doesn't actually override anything. You must either update the interface or the implementation.
Automatic Conversion of "Get" Methods to Properties
My Java project had a lot of methods whose names started with "get". For example, getImgLinks(), getClient(), getDomain(), getIt(), etc. Most of these methods did more than simply return a private variable; they performed work on the class's internal state and then returned a result. Despite the complexity of these methods, IntelliJ converted them into properties with complex custom getters. That is, getImgLinks() became the property imgLinks with its own custom getter; getClient() became the property client with its own custom getter, etc. These new getters often contained multiple lines of complex code.
This didn't make sense. Just because a method name starts with "get", accepts no parameters, and returns an object, it doesn't mean the method is a good candidate for conversion to a property. I had to convert each of these properties back into functions. Luckily, IntelliJ provides an operation to "Convert property to function" in its context menu. Using that operation, this:
val client: OkHttpClient
get() {
val newBuilder = OkHttpClient.Builder()
newBuilder.sslSocketFactory(
sslContext.socketFactory,
trustyMcTrusterson,
)
newBuilder.hostnameVerifier {
_: String?,
_: SSLSession? -> true
}
return newBuilder.build()
}
becomes:
fun getClient(): OkHttpClient {
val newBuilder = OkHttpClient.Builder()
newBuilder.sslSocketFactory(
sslContext.socketFactory,
trustyMcTrusterson,
)
newBuilder.hostnameVerifier {
_: String?,
_: SSLSession? -> true
}
return newBuilder.build()
}
Aside from these major issues, there are small style improvements you may want to make after converting your Java project to Kotlin. Those improvements include fixing the overuse of init blocks to initialize properties, lifting returns out of if blocks, turning utility class functions into top-level functions, and formatting issues, like hard wrapping at a given line-length.
IntelliJ does a good job of converting Java to Kotlin, but some manual work may still be necessary to produce idiomatic Kotlin code. Make sure to review your converted code, paying special attention to nullability issues, inheritance, and Java-specific conventions.





