콘텐츠로 건너뛰기

자주 방문한 페이지

최근 방문한 페이지

navigation

Kotlin and Android

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.

Get started

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.

Safer Code

Write safer code and avoid NullPointerExceptions in your app.


var output: String
output = null   // Compilation error

==================================

val name: String? = null    // Nullable type
println(name.length())      // Compilation error
          

Readable and Concise

Data Classes

Focus on expressing your ideas and write less boilerplate code.


// Create a POJO with getters, setters, equals(), hashCode(), toString(), and copy() with a single line:
data class User(val name: String, val email: String)
          

Lambdas

Use lambdas to simplify your code.


button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        doSomething();
    }
});
          

button.setOnClickListener { doSomething() }
          

Default and named arguments

Reduce the number of overloaded functions by using default arguments. Call functions using named arguments to make your code more readable.


fun format(str: String,
           normalizeCase: Boolean = true,
           upperCaseFirstLetter: Boolean = true,
           divideByCamelHumps: Boolean = false,
           wordSeparator: Char = ' ') {
        …
    }

==================================

// Call function with named arguments.
format(str, normalizeCase = true, upperCaseFirstLetter = true)
          

Say Goodbye to findViewById

Avoid findViewById() calls in your code. Focus on writing your logic with less verbosity.


import kotlinx.android.synthetic.main.content_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // No need to call findViewById(R.id.textView) as TextView
        textView.text = "Kotlin for Android rocks!"

    }
}
          

Extend functionality without inheritance

Extension functions and properties let you easily extend functionality of classes without inheriting from them. Calling code is readable and natural.


// Extend ViewGroup class with inflate function
fun ViewGroup.inflate(layoutRes: Int): View {
    return LayoutInflater.from(context).inflate(layoutRes, this, false)
}

==================================

// Call inflate directly on the ViewGroup instance
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = parent.inflate(R.layout.view_item)
    return ViewHolder(v)
}
          

100% Interoperable with Java

Add as little or as much of Kotlin as you want. Kotlin is a JVM language that's completely interoperable with Java.


// Calling Java code from Kotlin
class KotlinClass {
    fun kotlinDoSomething() {
        val javaClass = JavaClass()
        javaClass.javaDoSomething()
        println(JavaClass().prop)
    }
}

==================================

// Calling Kotlin code from Java
public class JavaClass {
    public String getProp() { return "Hello"; }
    public void javaDoSomething() {
        new KotlinClass().kotlinDoSomething();
    }
}
          

Great Tooling Support

Android Studio 3.0 and higher provides helpful tools to help you start using Kotlin. Convert entire Java files or convert code snippets on the fly when you paste Java code into a Kotlin file.

Kotlin is Open

Just like Android, Kotlin is an open source project under Apache 2.0. Our choice of Kotlin reaffirms our commitment to an open developer ecosystem as we evolve and grow the Android platform, and we are excited to see the language evolve.

See Kotlin on GitHub

Apps built with Kotlin

Many apps are already built with Kotlin—from the hottest startups to Fortune 500 companies.

Latest news

Get Started

이 사이트는 쿠키를 사용하여 사이트별 언어 및 표시 옵션에 대한 환경설정을 저장합니다.

Google Play에서 성공을 거두는 데 도움이 되는 최신 Android 개발자 뉴스 및 도움말을 받아 보세요.

* 필수 입력란

완료되었습니다.

WeChat에서 Google Developers 팔로우하기

이 사이트를 (으)로 탐색할까요?

페이지를 요청했지만 이 사이트의 언어 환경설정은 입니다.

언어 환경설정을 변경하고 이 사이트를 (으)로 탐색할까요? 언어 환경설정을 나중에 변경하려면 각 페이지 하단의 언어 메뉴를 사용하세요.

이 클래스를 사용하려면 API 수준 이상이 필요합니다.

문서에 대해 선택한 API 수준이 이므로 이 문서가 표시되지 않습니다. 왼쪽 탐색 메뉴의 선택기로 문서 API 수준을 변경할 수 있습니다.

앱에 필요한 API 수준 지정에 관한 자세한 내용은 다양한 플랫폼 버전 지원을 참조하세요.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)