The Android Developer Challenge is back! Submit your idea before December 2.

Get Started with Kotlin on Android

Android Studio fully supports Kotlin, enabling you to create new projects with Kotlin files, add Kotlin files to your existing project, and convert Java language code to Kotlin. You can use all of Android Studio's existing tools with your Kotlin code, including code completion, lint checking, refactoring, debugging, and more.

Unfamiliar with the Kotlin language? Check out these links:

Add Kotlin into an existing app

To gain skills and confidence using Kotlin, we recommend the following 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.

Android Studio also includes a code converter that 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.

Many Android APIs are available with idiomatic Kotlin references. For more information, see the KTX guide and the Kotlin on Android reference documentation.

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 onNavigationItemSelectedListener
    = BottomNavigationView.OnNavigationItemSelectedListener { item ->
  when (item.itemId) {
    R.id.navigation_home -> {
      textMessage.setText(R.string.title_home)
      return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_dashboard -> {
      textMessage.setText(R.string.title_dashboard)
      return@OnNavigationItemSelectedListener true
    }
 }
 false
}

Java

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

Best practices

As you gain fluency in Kotlin, you should 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 advice for formatting Kotlin code.