Get Started with #Kotlin on #Android
Kishan Prajapati
Senior Software Engineer | Android | Kotlin | Java | Jetpack Compose | KMP/KMM | React.js | Blockchain
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.
https://kotlinlang.org/docs/tutorials/kotlin-android.html
Create a new project with Kotlin
Using Kotlin with a new project requires just one extra click in the New Project wizard:
- In Android Studio, click File > New > New Project. Or if you've just opened Android Studio and see the Welcome to Android Studio window, click Start a new Android Studio project.
- On the first screen, check Include Kotlin support. That's the only difference.
- Click Next and continue through the wizard until you're done.
#Modern. #Expressive. #Safe.
Safer Code
Write safer code and avoid NullPointerExceptions in your app.
//Kotlin
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.
//Kotlin
// 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.
//Java
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
doSomething();
}
});
//Kotlin
button.setOnClickListener { doSomething() }
Say Goodbye to findViewById
Avoid findViewById() calls in your code. Focus on writing your logic with less verbosity.
//Kotlin
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 the functionality of classes without inheriting from them. The calling code is readable and natural.
//Kotlin
// 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.
//Kotlin
// 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();
}
}