install android studio

Introduction to Kotlin

Kotlin:

Kotlin is now an official language on Android. It’s expressive, concise, and powerful. Best of all, it’s interoperable with our existing Android languages and runtime.

Kotlin is fully supported in Android Studio 3.0 and higher, so it’s easy to create new projects with Kotlin files, add Kotlin files to your existing project, and convert Java language code to Kotlin. You can then use all of Android Studio’s existing tools with your Kotlin code, such as autocomplete, lint checking, refactoring, debugging, and more.

Modern. Expressive. Safe.

Kotlin is concise while being expressive. It contains safety features for nullability and immutability, to make your Android apps healthy and performant by default.

add Kotlin into an existing app

To gain skills and confidence using Kotlin, we recommend a gradual approach:

  1. Start by writing tests in Kotlin. Tests are useful to check for code regression, and they add a level of confidence when refactoring your code. Tests are especially useful when converting existing Java code into Kotlin. Because tests are not bundled with your app during packaging, they are a safe place to add Kotlin to the codebase.
  2. Write new code in Kotlin. Before converting existing Java code to Kotlin, try adding small pieces of new Kotlin code to your app. Begin with a small class or top-level helper function. Be sure to add the relevant annotations to the Kotlin code to ensure proper interoperability with the Java code.
  3. Update existing code to Kotlin. Once you’re comfortable with writing new Kotlin code, convert your existing Java code to Kotlin. Consider extracting small bits of Java functionality and converting to Kotlin classes and top-level functions.Begin by using the Android Studio code converter which converts the code in a Java file to Kotlin. You can also convert Java code pasted from the clipboard into a Kotlin file.

Android APIs and Kotlin examples

Kotlin provides complete interoperability with the Java language, so calling the Android APIs often looks exactly like the matching Java code. Except now you can combine those method calls with Kotlin’s syntax features.

We’re also working to make all Android API documentation available with idiomatic Kotlin references. You can find links to the available Kotlin references on the Android reference overview.

Below are a few examples of what it looks like to call Android APIs in Kotlin, compared to the same code in Java language:

Declare an Activity

class MyActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity)
  }
}

Create an on-click listener

val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener {
  ...
}

create an item click listener


private val mOnNavigationItemSelectedListener
    = BottomNavigationView.OnNavigationItemSelectedListener { item ->
  when (item.itemId) {
    R.id.navigation_home -> {
      mTextMessage.setText(R.string.title_home)
      return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_dashboard -> {
      mTextMessage.setText(R.string.title_dashboard)
      return@OnNavigationItemSelectedListener true
    }
 }
 false
}

Kotlin on Android FAQ

Why did Android make Kotlin a first-class supported language?

Kotlin is an Android-compatible language that is concise, expressive, and designed to be type- and null-safe. It works with the Java language seamlessly, so it makes it easy for developers who love the Java language to keep using it but also incrementally add Kotlin code and leverage Kotlin libraries. Also, many Android developers have already found that Kotlin makes development faster and more fun, so we want to better support these Kotlin users. You can read more about Kotlin and Android here.

 

I’m already using the Kotlin language, what changes for me?

In the short term, we think the biggest change you’ll notice is that Android Studio 3.0 and higher includes fully tested Kotlin support. We think this will result in a much easier, more stable development experience for you.

 

How do I use Kotlin with Android Studio?

Kotlin is fully supported in Android Studio 3.0 and higher. Previously, using Kotlin required that you add the Kotlin plugin, but all new releases of Android Studio ship with these tools built in. So you can create new projects with Kotlin files, convert Java language code to Kotlin, debug Kotlin code, and more, without any extra steps. See Get Started with Kotlin.

 

How do I debug Kotlin in Android Studio?

Debugging Kotlin works just like debugging Java code. You don’t need to do anything differently.

 

What kind of other IDE support is provided for Kotlin (like lint, autocomplete, refactorings, etc.)?

As of Android Studio 3.0, the IDE has full tooling support for Kotlin. There are, however, some known issues and limitations that we’re working on.

 

What’s the future of Kotlin?

JetBrains’ thoughtful work on Kotlin’s design is one of the reasons we’re embracing the language. Google is partnering with JetBrains to ensure a wonderful overall developer story—from language, to framework, to tools. And, we are excited to be working together to move the Kotlin language into a non profit foundation.

 

Is Kotlin open source?

The preferred license for Kotlin is the Apache Software License, Version 2.0 (“Apache 2.0”), and the majority of the Kotlin software is licensed under Apache 2.0. While the project will strive to adhere to the preferred license, there may be exceptions that will be handled on a case-by-case basis. For example, certain third-party dependencies used by Kotlin may be licensed under different open source licenses which are still compatible with the Apache 2 license.

 

How do I choose between the Java and Kotlin languages?

You don’t have to pick! You can use both together as you see fit. If you need help discovering whether Kotlin is a good fit for you, you can try it on Android or learn more about the language with these Kotlin resources

 

Can I call Android or other Java language library APIs from Kotlin?

Yes. Kotlin provides Java language interoperability. This is a design that allows Kotlin code to transparently call Java language methods, coupled with annotations that make it easy to expose Kotlin-only functionality to Java code. Kotlin files that don’t use any Kotlin-specific semantics can be directly referenced from Java code without any annotations at all. Combined, this allows you to granularly mix Java code with Kotlin code. To learn more, see Kotlin’s interop documentation.

 

Do you have Kotlin reference docs for Android APIs?

Yep! We’re working to make all Android API documentation available with idiomatic Kotlin references. They’re not all ready yet, but you can find links to the available Kotlin references on the Android reference overview. If you’re looking for the core Kotlin language references, go to the Kotlin standard library reference.

 

Can I use both Java files and Kotlin files in the same project?

Yes. You can adopt as much or as little Kotlin as you like and mix it with Java code using Kotlin’s interoperability with Java.

 

Can I use Kotlin with C++?

Yes, JNI is fully supported with Kotlin. Simply mark the JNI method with the external modifier.

 

How do I add Kotlin to my new projects?

When you create a new project in Android Studio, just check the Include Kotlin support checkbox. For more information, see Get Started with Kotlin.

 

How do I add Kotlin to my existing projects?

Select your module in the Project window, and then select File > New, select any Android template, and then choose Kotlin as the Source language. For more information, see Get Started with Kotlin.

 

How do I convert Java language code to Kotlin?

Open a Java file and select Code > Convert Java File to Kotlin File. Or, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file—when prompted, click Yes to convert the code to Kotlin. For more information, see Get Started with Kotlin.

 

Will there be parallel docs, samples, codelabs, and templates in Kotlin?

We’re working to make our documentation and educational materials as useful as possible to both Java and Kotlin language users. In the meantime, developers can rely on Kotlin’s excellent interoperability with the Java language and the ability to automatically translate Java language code to Kotlin in Android Studio.

 

Do Kotlin coroutines work on Android? How about async/await?

Kotlin coroutines should currently work, but they are currently an experimental design. As such, Kotlin makes no guarantees about future status, and thus, neither does Android.

 

What’s Kotlin’s impact on APK size / method count?

The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APK. The net impact might be less if you use Kotlin to replace another library in your project, such as Guava or RxJava. This size also reduces when you optimize the APK for release with Proguard, just like other app code and libraries.

 

Does using Kotlin have any performance impact?

Kotlin doesn’t have a direct performance impact, but just as with the Java language, you should be thoughtful about how you use it. For example, repeated copying between new collection instances can impact GC performance, and calling a method that accepts non-null types adds a method call for the null check (though you can disable runtime null checks in the compiler with -Xno-param-assertions).

 

Which versions of Android does Kotlin support?

All of them! Kotlin is compatible with JDK 6, so apps with Kotlin safely run on older Android versions.

Android KTX

Android KTX is a set of Kotlin extensions that is part of the Android Jetpack family. It optimizes Jetpack and Android platform APIs for Kotlin use. The purpose of Android KTX is to make Android development with Kotlin more concise, pleasant, and idiomatic by leveraging Kotlin language features such as extension functions/properties, lambdas, named parameters, and parameter default values. Android KTX does not add any new features to the existing Android APIs.

To learn more about Android KTX, see our DevBytes video.

 

About the author

Generic Bytes

Hi! we are from Generic Bytes an aspiring bloggers with an obsession of things tech.This blog is dedicated to people who want to learn the technology.

View all posts

Leave a Reply

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