Get Started with Kotlin on Android

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.

Want to hit the ground running? Check out our Kotlin samples. To learn more about the Kotlin language, take a look at Resources to Learn Kotlin.

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

Kotlin

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

Java

public class MyActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
  }
}

Create an on-click listener

Kotlin

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

Java

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    ...
  }
});

Create an item click listener

Kotlin

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
}

Java

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
    = new BottomNavigationView.OnNavigationItemSelectedListener() {
  @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case R.id.navigation_home:
        mTextMessage.setText(R.string.title_home);
        return true;
      case R.id.navigation_dashboard:
        mTextMessage.setText(R.string.title_dashboard);
        return true;
    }
    return false;
  }
};

Best practices

As you gain fluency in Kotlin try to follow these guidelines:

  • Favor readability over minimizing lines of code. It's easy to go overboard with Kotlin syntactic sugar.
  • It's a good idea to establish coding conventions and idioms that work best for your team. The Kotlin and Android Kotlin style guides offer good advice for formatting Kotlin code.