When it comes to Android development, I’d hazard a guess that one programming language springs to mind: Java.

There’s no denying that most Android apps are written in Java, but Java isn’t the only option for Android development.

In fact, Android apps can be written in any language that runs on the Java Virtual Machine (JVM). One JVM-compatible programming language that has been catching the attention of the Android community these days is Kotlin, a static programming language from JetBrains.

If you’ve heard a lot of people raving about Kotlin and are itching to give it a try, this is a good time. This is the first of three articles that step by step introduce and share how to develop Android apps using Kotlin.

First, I suggest you think about why you want to switch from Java to Kotlin, and then I’ll give you my opinion on your choice.

Why do you have to change it?

Java is one of the most widely used programming languages and is considered the official language for Android development. However, Java is not the best choice for a number of reasons.

The biggest reason is that Java is not a modern language anymore. While Java8 has many eagerly awaited new features (including lambda expressions), only some of the new features are available on the Android platform, and that’s not going to change anytime soon. So if you’re still developing Android in Java, you’ll probably have to play with Java7.

Also, Java has a lot of “documented” syntax black spots, such as multi-layer try-catches, poor extensibility, dangerous empty variables (and the infamous NullPointerException), not to mention no support for functional programming. Java has been aggressively adding functional programming features, such as lambda expressions and functional interfaces, but that doesn’t help because Java is a procedural language.

Interoperable with Java

One of Kotlin’s great skills is 100% interoperable with Java. What this means is that you can masturbate Java and Kotlin in one project without any problem. In the following diagram, you can see that there are separate activities written in Java and Kotlin, and the user is not aware of which is written in Java and which is written by Kotlin.

Since Kotlin and Java are interoperable in the same project, instead of having to move the entire project to Kotlin or restart a Kotlin project, you can work directly on your existing Java project.

Because of this interoperability, we can try to develop one part of the code with Kotlin directly on the project at hand without worrying about affecting other parts of the project. We can use Kotlin to develop new modules in an existing project while keeping the existing Java code, or simply migrate the entire project to Kotlin at once.

Even better, Kotlin can use almost any Java library, even a high-level framework based on annotation processing.

A gentle learning curve

Kotlin is more here to enhance and improve Java than to tear it down. So, a lot of what you learn in Java can actually be applied to Kotlin.

At the same time, Kotlin is designed to provide a smooth learning curve for Java developers. Java developers will find many of Kotlin’s syntax similar to Java, such as Kotlin’s declaration of a class:

class MainActivity : AppCompatActivity() {Copy the code

Kotlin was also designed to be clever and easy to read, so we could figure out what the code meant even if it was very different.

The perfect support

Kotlin was developed by JetBrains. Yes, Android Studio is based on his IntelliJ, so the Android Studio platform has great support for Koltin. After installing the Kotlin plug-in, you can configure Kotlin in your project by clicking a button or something.

After that, Android Studio will definitely recognize the Kotlin code and compile it. Android Studio also provides Debugging, auto-completion, code searching, unit testing, and full refactoring support for Kotlin.

Then, with a click of the mouse, you can turn the Java file into a Kotlin file.

Concise code

A comparison of Kotlin’s code with Java’s shows that the former is much more compact and powerful than the latter. You and I both know the truth.

For example, here is an Activity that contains the Floating Action button, but clicking on the FAB pops up the snackbar:

public class MainActivity extends AppCompatActivity {
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
 
       FloatingActionButton myfab = (FloatingActionButton) findViewById(R.id.myfab);
       myfab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "This is a snackbar".Snackbar.LENGTH_LONG)
                       .setAction("Action".null).show(); }}); }}Copy the code

Kotlin is much cleaner, especially when it comes to creating FAB and binding listeners:

class MainActivity : AppCompatActivity() {
 
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       val toolbar = findViewById(R.id.toolbar) as Toolbar
       setSupportActionBar(toolbar)
 
       val myfab = findViewById(R.id.myfab) as FloatingActionButton
       myfab.setOnClickListener { view ->
           Snackbar.make(view, "This is a snackbar", Snackbar.LENGTH_LONG)
                   .setAction("Action".null).show()
       }
   }
 
}Copy the code

Kotlin can effectively reduce the amount of sample code to be written, and has a much better programming experience than Java, which requires a lot of verbose code to be written.

Increase package volume

Kotlin’s standard library and runtime increase the SIZE of APK by about 800KB.

Lack of official support

(In late 2016, we know that Kotlin was officially appointed to the East Palace at this year’s IO Conference.)

conclusion

In this article, we consider why we switched from Java to Kotlin as the Android language of choice, and look in more detail at the unique strengths and weaknesses of Kotlin as a potential alternative to Java.

Java vs. Kotlin: Should You Be Using Kotlin for Android Development?