{"id":20534,"date":"2017-07-07T10:49:22","date_gmt":"2017-07-07T16:19:22","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/an-introduction-to-kotlin-for-android-development\/"},"modified":"2023-08-16T23:07:42","modified_gmt":"2023-08-17T04:37:42","slug":"an-introduction-to-kotlin-for-android-development","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/an-introduction-to-kotlin-for-android-development\/","title":{"rendered":"An introduction to Kotlin for Android development"},"content":{"rendered":"<p>Google announced that it is making Kotlin, a statically typed programming language for the Java Virtual Machine, a first-class language for writing Android apps. Kotlin\u2019s primary sponsor is JetBrains, the company behind tools like IntelliJ.<\/p>\n<p>It\u2019s 100 percent interoperable with Java, which until now was Google\u2019s primary language for writing Android apps (besides C++).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Kotlin\"><\/span><strong>What is Kotlin ?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kotlin is a great fit for <a href=\"https:\/\/www.solutionanalysts.com\/android-app-development\/\"><strong>developing Android applications<\/strong><\/a>, bringing all of the advantages of a modern language to the Android platform without introducing any new restrictions:<\/p>\n<p><strong>Compatibility:<\/strong> 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.<\/p>\n<p><strong>Performance<\/strong>: A Kotlin application runs as fast as an equivalent Java one, thanks to very similar bytecode structure. With Kotlin&#8217;s support for inline functions, code using lambdas often runs even faster than the same code written in Java.<\/p>\n<p><strong>Interoperability<\/strong>: Kotlin is 100% interoperable with Java, allowing to use all existing Android libraries in a Kotlin application. This includes annotation processing, so data binding and Dagger work too.<\/p>\n<p>Footprint &#8211; Kotlin has a very compact runtime library, which can be further reduced through the use of<\/p>\n<p>ProGuard &#8211; In a real application, the Kotlin runtime adds only a few hundred methods and less than 100K to the size of the .apk file.<\/p>\n<p><strong>Compilation Time<\/strong>: Kotlin supports efficient incremental compilation, so while there&#8217;s some additional overhead for clean builds, incremental builds are usually as fast or faster than with Java.<\/p>\n<p><strong>Learning Curve<\/strong>: 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.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_Android_APIs_with_Kotlin\"><\/span><strong>Use Android APIs with Kotlin<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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&#8217;s syntax features.<\/p>\n<p>Here are a few examples of what it looks like to call Android APIs in Kotlin, compared to the same code in Java language:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Syntax_differences_with_KOTLIN_and_JAVA\"><\/span><strong>Syntax differences with KOTLIN and JAVA.<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Declare Activity in Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">class MainActivity : AppCompatActivity() {\r\n  override fun onCreate(savedInstanceState: Bundle?) {\r\n    super.onCreate(savedInstanceState)\r\n    setContentView(R.layout.activity_main)\r\n  }\r\n}<\/pre>\n<ul>\n<li>Declare Activity in Java<\/li>\n<\/ul>\n<pre class=\"lang:php\">public class MainActivity extends AppCompatActivity {\r\n  @Override\r\n  protected void onCreate(Bundle savedInstanceState) {\r\n    super.onCreate(savedInstanceState);\r\n    setContentView(R.layout.activity_main);\r\n  }\r\n}<\/pre>\n<ul>\n<li>On-click listener in Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">val fab = findViewById(R.id.fab) as FloatingActionButton\r\nfab.setOnClickListener {\r\n  ...\r\n}<\/pre>\n<ul>\n<li>On-click listener in Java<\/li>\n<\/ul>\n<pre class=\"lang:php\">FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);\r\nfab.setOnClickListener(new View.OnClickListener() {\r\n  @Override\r\n  public void onClick(View view) {\r\n    ...\r\n  }\r\n});<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Kotlin_is_like_Swift\"><\/span><strong>Kotlin is like Swift.<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>BASIC<\/p>\n<p><strong>Hello Android Programme<\/strong><\/p>\n<ul>\n<li>Swift<\/li>\n<\/ul>\n<pre class=\"lang:php\">print(\"Hello, Android!\")<\/pre>\n<ul>\n<li>Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">println(\"Hello, Android!\")<\/pre>\n<p><strong>Variables And Constants<\/strong><\/p>\n<ul>\n<li>Swift<\/li>\n<\/ul>\n<pre class=\"lang:php\">var age = 42\r\nage = 50\r\nlet mConstant = 42<\/pre>\n<ul>\n<li>Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">var age = 42\r\nage = 50\r\nval mConstant = 42<\/pre>\n<p><strong>COLLECTIONS <\/strong><\/p>\n<p>Arrays<\/p>\n<ul>\n<li>Swift<\/li>\n<\/ul>\n<pre class=\"lang:php\">var deviceList = [\"Android\", \"iOS\", \"Window\"]\r\n\tdeviceList[1] = \"iOS\"<\/pre>\n<ul>\n<li>Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">val deviceList = arrayOf(\"Android\", \"iOS\", \"Window\")\r\ndeviceList[1] = \"iOS\"<\/pre>\n<p><strong>Maps<\/strong><\/p>\n<ul>\n<li>Swift<\/li>\n<\/ul>\n<pre class=\"lang:php\">var job = [\r\n  \t \t \"KP\": \"CEO\",\r\n   \t\t \"MS\": \"Sr. Android Developer\",\r\n\t]\r\n\tjob[\"HM\"] = \"Public Relations\"<\/pre>\n<ul>\n<li>Kotlin<\/li>\n<\/ul>\n<pre class=\"lang:php\">val job = mutableMapOf(\r\n    \t\t\"KP\" to \"CEO\",\r\n  \t\t  \"MS\" to \"Sr. Android Developer\"\r\n\t)\r\n\tjob[\"Jayne\"] = \"Public Relations\"<\/pre>\n<p><strong>REFERENCES<\/strong> :<\/p>\n<p>https:\/\/kotlinlang.org\/docs\/reference\/android-overview.html<br \/>\nhttp:\/\/nilhcem.com\/swift-is-like-kotlin\/<br \/>\nhttps:\/\/developer.android.com\/kotlin\/get-started.html#use-android-apis-with-kotlin<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google announced that it is making Kotlin, a statically typed programming language for the Java Virtual Machine, a first-class language for writing Android apps. Kotlin\u2019s primary sponsor is JetBrains, the company behind tools like IntelliJ. It\u2019s 100 percent interoperable with Java, which until now was Google\u2019s primary language for writing Android apps (besides C++). What [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20535,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[200],"tags":[],"class_list":["post-20534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-apps"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20534"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20534\/revisions"}],"predecessor-version":[{"id":32929,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20534\/revisions\/32929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20535"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}