Kotlin vs. Java : Which one is a Better for Android App Development?

What happens when you convert your app's code from Java to Kotlin? It takes longer to compile!  

Right?

I can understand……………

The codebase is smaller and more maintainable on Kotlin than its Java predecessor. And because of it, the transition is worth it. But some devs might be worried about trying out Kotlin that their code will compile slower than Java.

Well! Their concern is valid since no one wants to spend more time converting builds that take longer for Android app development. Hence, let's look at whether there is a significant difference in compile times before and after converting your Java code to Kotlin of the App Lock app, and we will also be focusing on judging the overall build time.

How do we test build times?

Write a few shell scripts to run Gradle repeatedly builds in various ways. It is to perform tests ten times consecutively. 

Remember, What these shell scripts do is that they clean your project before each scenario. And also for times that use the Gradle daemon. Doing this stops the daemon once before benchmarking any situation that becomes a hurdle in compiling the code successfully.

Note: To perform all mentioned benchmarks, an Intel Core i7–6700 processor system ran at 3.4 GHz, with 32-GiB of DDR4 memory and a Samsung 850 Pro SSD. And to build the source code, Gradle 2.14.1 was picked.

Tests

Run benchmarks in several common usage scenarios -

  • Clean builds with and without the Gradle daemon.

  • Incremental builds with no file changes.

  • Incremental builds with a changed file.

So, once the codebase is transitioned from Java to Kotlin, App Lock's Java codebase with 5,491 methods and 12,371 lines turns into 4,987 processes and 8,564 lines of Kotlin code.

You can observe the numbers showing no significant architectural changes. Thus, testing and judging the compiled times for the app's codebase before and after rewriting should give you a pretty good idea of the build times between Kotlin and Java.

Now, let's see what happens with each use case mentioned above.

Clean builds without Gradle daemon

Running a clean build for Android from a cold start is a worst-case scenario for build times in Kotlin and Java. So, for this test, you can disable the Gradle daemon.

Here's how long each of the ten builds took -

The result is -

Java's average build time = 15.5 seconds,

and Kotlin's average build time = 18.5 seconds.

Thus, net increase = 17%.

Java compiles 17% faster than Kotlin for clean builds.

Outcome - Starting with Kotlin could be better. But this is different from how most developers will compile the code.

And here is something to note: repeatedly compiling the same code base while making changes is expected, and that's what the Gradle daemon is built for. So, let's explore the numbers when using them.

Clean builds using Gradle daemon

Just-in-time (JIT) compilers like the JVM (Java Virtual Machine) have problems. They take longer to compile a code executed on them, increasing a process's performance over time as it runs.

But remember, stopping the JVM process will lose the gain of that performance. And while building a Java code, one would typically start and stop the JVM each time the build-making process begins, forcing the JVM to redo all work every time one builds the code.

But there is a way to combat this; using Gradle!!!

Gradle has a daemon tool that stays alive among builds to manage the performance gains from JIT compilations.

How to enable this tool?

You can do it by passing — daemon to Gradle on the command line or adding "org.gradle.daemon=true" to the "gradle.properties" file.

Below is the same series of clean builds but with the Gradle daemon running:

Result:

The time for the first run is about the same as without the daemon, but as the test runs increase, you can observe an apparent increase in performance until the fourth test run.

After the third run, where the daemon is warmed up, looking at the average build time is helpful.

For the warm runs -

The average time is 14.1 seconds for a clean build in Java, and Kotlin clocks at 16.5 seconds.

Hence, a net increase of 13%.

Java compiles 13% faster than Kotlin for clean builds with the Gradle daemon warmed up.

Kotlin is new. It is a general-purpose programming language and is fit to interoperate fully with Java. But Kotlin is still trailing behind slightly and mainly targets the JVM.

But remember, it also compiles JavaScript. 

For example, in frontend web apps using React or native code via LLVM for native iOS apps.

If you want to build times to get reduced, Gradle daemon is a definite tool that will reduce this time by over 40%, no matter which language you use.

If you're not using the Gradle daemon, it would be best to use it.

In conclusion, complete builds made with Kotlin compile a bit slower than Java. Devs usually collect a build post doing some changes to a few files, and incremental builds show different performance outcomes.

Let's find out whether Kotlin can catch up with Java where it matters.

Incremental builds

One of a compiler's most critical performance features is incremental compilation. A standard build is good enough to recompile all source files in a project. But in the case of keeping track of files that have changed since the last appearance in the project, an incremental form is away only to recompile project-dependent files.

Incremental builds greatly impact compile times, especially for big projects. The addition of incremental builds to Kotlin occurred for version 1.0.2, and you can enable using the script -

"Kotlin.incremental=true to the "gradle.properties."

OR, just hit a few scripts on the command line!!!

So, to observe and have a basic idea about the compile time difference for Kotlin Java during incremental compilation, here is the case of incremental compilation with no changed files:


Now, let's walk through testing incremental compilation with one changed file.

Here, you can clearly observe that the Gradle daemon took 2 to 3 runs to warm up. And after a while, the performance of Kotlin and Java is nearly the same.

That is, Java takes 4.6 sec. Per warm build with no changes, whereas Kotlin approaches 4.5 sec. And when a file not used by other files is changed, Java needs an avg. of 7.0 sec. to do a warm-build, for which Kotlin clocks in at 6.1 sec.

Finally, if you alter an imported file from various other files, Java does an incremental build in about 7.1 sec. Once the Gradle daemon is all geared up. And for the same purpose, Kotlin averages 6.0 sec.

Hence,

"In the most usual configuration — partial builds with incremental compilation enabled — Kotlin compiles as fast or slightly faster than Java."


The point is that Java still beats Kotlin by 10–15% for clean builds. These differences are relatively rare. Most developers' common scenario is partial builds, where incremental compilation improves considerably.


But using Gradle daemon and incremental compilation options, Kotlin compiles slightly faster than Java. And that's impressive!!! The Kotlin devs team created a great language with many great features and a language that can compile so quickly. Try out Kotlin because it collects as promptly as Java.

Top five reasons to choose Kotlin over Java

Lesser and more productive coding -

Kotlin has been designed to require manner less coding in assessment to Java and provide more excellent performance. If we try to share this numerically, in keeping with an estimate, it's around 20%, much less than Java. It reduces the amount of boilerplate coding, which previously the programmers had to write—making the execution of the app development process higher and faster.

Kotlin is Interoperable -

This is likely one of the crucial factors. Once the project is compiled, it will be tricky to recognize which part is coded with Kotlin and Java. With this interoperability, Kotlin enables you to use its elements and ease of operation without altering the codes from the current projects. It results in low mobile app maintenance costs since it's cheaper to develop applications in Kotlin.

Easy to learn and implement -

Like Android O, you can spend your time learning Kotlin. You can get your hands on it by taking a little reference and investing your time reading official docs from Kotlin's web page. It is a language with an intuitive and lean syntax designed to provide tender learning ways for Java developers.

It is a professional language for Android app developers -

For Android app development using Google, Kotlin is an official language. So it is the right choice to select Kotlin. It also has a structured document for every task, and one can quickly get a reference from it.

Kotlin is a free and open-source project -

The best part about Kotlin is - it is an open collaboration project or an open-source language in which developers around the globe contribute for free. Hence, there is no need to put money into licensing or else. Using a Java-to-Kotlin converter device to transform your existing Java records, you could use this excessive-end language with no trouble. The instrument enables you to change the complicated codes to a more straightforward type without compromising the time and effort you store.

Comments

Popular posts from this blog

How Data Analytics Boost Mobile App Development

Typography For a Better UI and UX

Top 5 Web Application Development Trends in 2022 & Beyond