Reviews

Android Growth Fundamentals: Find out how to add Kotlin to an present Java Android challenge

Related Articles

Kotlin is fairly nice. It is received an entire bunch of helpful language options, and the syntax is usually clear and easy. It is also cross-platform, and the bottom language can compile to an entire bunch of various languages and platforms.


Kotlin for Java (known as KotlinJVM) compiles to the identical factor Java compiles to — JVM bytecode for pure Java, and no matter Android is utilizing as of late for its model of Java. This implies code written in KotlinJVM has entry to all present Java and Android APIs, together with any Java lessons, strategies, and fields which might be already in your app.

That compatibility works the opposite approach round, too. That means, you possibly can entry Kotlin APIs from Java. The code may find yourself trying just a little messy if you happen to attempt to use extra superior options, nevertheless it’s attainable.

This tutorial goes to go over how one can add Kotlin to an present Android challenge that is made in Java. It additionally assumes you are utilizing Android Studio. Earlier than we get began although, you might need to familiarize your self a bit on how Kotlin works, if you have not completed so already. JetBrains, the corporate behind the language, has a helpful FAQ for this.


Dependencies

Kotlin is technically a library. It is a fancy library, with numerous options and an accompanying IntelliJ/Android Studio plugin, nevertheless it’s a library. So so as to add it, you may want so as to add some dependencies.

In your challenge degree construct.gradle, add the Kotlin dependency.

 buildscript {
    ...
    dependencies {
        ...
       
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
    }
    ...
}

In your module degree construct.gradle, apply the Kotlin Android plugin and add the dependencies.

 ...
apply plugin: 'kotlin-android'

...

dependencies {
    ...
   
   
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.10'
}

And that is it for implementing Kotlin. The most recent model of Android Studio already comes bundled with the IDE plugin.

Primary Utilization

Now the combination is finished, you can begin truly utilizing Kotlin. To make a brand new class, simply right-click on the bundle the place you need the file to be created, click on New, and click on Kotlin Class/File.

When you click on that, you may be proven a dialog asking for the identify of the file, together with what sort of object it ought to be (Class, Interface, Object, plain file, and many others). That is fairly much like creating a brand new Java Class.

An image showing the New Kotlin Class/File dialog

Select what you need, and the file shall be created. Now you may get began programming in Kotlin.

Conclusion

Including Kotlin to an present Java Android challenge is straightforward. Simply embrace a number of Gradle dependencies, apply a plugin, and you can begin programming within the language.

For extra superior utilization, together with the best way to robotically convert Java code to Kotlin, take a look at Google’s official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button