Skip to main content

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.




To use Kotlin in Android Studio prior to version 3.0 you have to manually install the Kotlin plugin. To do so, start Android Studio and open Plugins from the Configure menu. In the opened dialog, search for Kotlin, select the corresponding plugin and press Install.

Kotlin for Android Development:
Kotlin is a great fit for developing Android applications, bringing all of the advantages of a modern language to the Android platform without introducing any new restrictions.

Compatibility: Kotlin is fully compatible with JDK 6, ensuring that Kotlin applications can run on older Android devices with no issues. The Kotlin tooling is fully supported in Android Studio and compatible with the Android build system.

Performance: A Kotlin application runs as fast as an equivalent Java one, thanks to very similar bytecode structure. With Kotlin's support for inline functions, code using lambdas often runs even faster than the same code written in Java.

Interoperability: Kotlin is 100% interoperable with Java, allowing to use all existing Android libraries in a Kotlin application. This includes annotation processing, so databinding and Dagger work too.

Footprint: Kotlin has a very compact runtime library, which can be further reduced through the use of ProGuard. In a real application, the Kotlin runtime adds only a few hundred methods and less than 100K to the size of the .apk file.

Compilation Time: Kotlin supports efficient incremental compilation, so while there's some additional overhead for clean builds, incremental builds are usually as fast or faster than with Java.

Learning Curve: For a Java developer, getting started with Kotlin is very easy. The automated Java to Kotlin converter included in the Kotlin plugin helps with the first steps. Kotlin Koans offer a guide through the key features of the language with a series of interactive exercises.

Converting Java code to Kotlin:
In general, the easiest way to start using Kotlin is to convert automatically Java activity into Kotlin one. Please note that anytime instead of looking through documentation for a new way to express an old pattern, you can write it in Java, then copy-paste Java code into Kotlin file, and IntelliJ IDEA (or Android Studio) will suggest to convert it.


After the conversion you should have an activity written in Kotlin.






Building and publishing the Kotlin application for Android

  • You are now ready to build the application and run it on an emulator or device. This works in exactly the same way as in Java. You can make a release of the application and sign it similarly to what you do for an Android application written in Java.
  • Kotlin has a rather small runtime file size: the library is approximately 943KB (as of 1.2.41). This means > Kotlin adds just a little to .apk file size.
  • Kotlin compiler produces byte-code, thus there really is no difference in terms of look and feel of Kotlin applications versus those written in Java.

More Kotlin Extensions
One of the biggest moments at last year’s keynote came when Director of Product Management, Stephanie Cuthbertson, announced that Kotlin would become an officially supported language for Android development, so we were always going to see more Kotlin-related news at Google I/O 2018.
Kotlin KTX is one interesting new Kotlin project that got some attention during this year’s I/O. This new project is a collection of modules consisting of extensions that optimize the Android platform for Kotlin. Using these extensions, you can make lots of minor improvements to your code. For example, if you wanted to edit SharedPreferences using vanilla Kotlin, then your code might look something like this:
       

sharedPreferences.edit()    
.putBoolean("key", value)
.apply()

       
 

Wi
th the help of the KTX’s androidx.core:core-ktx module, you can now write code that looks more like this:
       

sharedPreferences.edit {   
   putBoolean("key", value)
}

       
 
Android KTX is currently in preview, so you should expect some breaking changes before it reaches its first stable release. However, if you want to experiment with this early version, then the following modules are available today:
  • androidx.core:core-ktx
  • androidx.fragment:fragment-ktx
  • androidx.palette:palette-ktx
  • androidx.sqlite:sqlite-ktx
  • androidx.collection:collection-ktx
  • androidx.lifecycle:lifecycle-viewmodel-ktx
  • androidx.lifecycle:lifecycle-reactivestreams-ktx
  • android.arch.navigation:navigation-common-ktx
  • android.arch.navigation:navigation-fragment-ktx
  • android.arch.navigation:navigation-runtime-ktx
  • android.arch.navigation:navigation-testing-ktx
  • android.arch.navigation:navigation-ui-ktx
  • android.arch.work:work-runtime-ktx

To start working with Android KTX, you’ll need to add a dependency for each module that you want to use. For example:
      

dependencies {  
 implementation 'androidx.fragment:fragment-ktx1.0.0-alpha1'
}

Comments

Popular posts from this blog

Short explanation on Gradle, minSdkVersion, maxSdkVersion, compileSdkVersion and targetSdkVersion in Android

Gradle : The Android build system compiles app resources and source code, and packages them into APKs that you can test, deploy, sign, and distribute. Android Studio uses Gradle . The Android plugin for Gradle works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android applications. Gradle and the Android plugin run independent of Android Studio. Gradle architecture is shown below. Example: android { compileSdkVersion 27 buildToolsVersion “26.0.2” defaultConfig { applicationId “com.example.checkyourtargetsdk" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName “1.0” } } minSdkVersion : minSdkVersion is the lower bound for your app . The minSdkVersion is one of the signals the Google Play Store uses to determine which of a user’s devices an app can be installed on.  your app’s minSdkVersion must be at least as high as your dependencies’ minSdkVe

Google re-branded the support Android libraries to AndroidX

It is important to note, you cannot mix AppCompat and Jetpack in the same project. You must convert everything to use Jetpack if you want to upgrade. The support library artifacts are being deprecated and all future development is going into AndroidX , so there's no avoiding this migration. Alan Viverette sums this up nicely: “There won’t be a 29.0.0, so Android Q APIs will only be in AndroidX” The stable release of 28.0.0 will be the final feature release packaged as android.support . All subsequent feature releases will only be made available as androidx-packaged artifacts. Below tips will give you a clearer transition path. The current version of AppCompat (v28.x) is exactly the same as AndroidX (v1.x). In fact, the AppCompat libraries are machine generated by changing maven coordinates and package names of the AndroidX codebase. For example, android.support.v7.app.AppCompatActivity is now androidx.appcompat.app.AppCompatActivity For a complete listi

Android Beginners Guide

                                                                                                               Android Operation System: Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine - DVM). Android is created by the Open Handset Alliance which is lead by Google. Android uses a special virtual machine, e.g. the Dalvik Virtual Machine. Dalvik uses special bytecode. Therefore you cannot run standard Java bytecode on Android. Android provides a tool "dx" which allows to convert Java Class files into "dex" (Dalvik Executable) files. Android applications are packed into an .apk (Android Package) file by the program "aapt" (Android Asset Packaging Tool) To simplify development Google provides the Android Development Tools (ADT) for Eclipse. The ADT performs automatically the conversion f