Using Kotlin in Android Studio 3.0 (Part 1)

综合技术 2018-03-07 阅读原文


When I wrote this article, I assumed that you already know about Android programming before. If you are Android beginners, you can read my other article, “ Introduction Android programming using Android Studio 3.0 .”

Why Is Kotlin?

  • Kotlin is very intuitive and easy to learn for Java developers
  • Android Studio 3.0 can understand, compile, and run Kotlin code easily

Installing the Android Studio 3.0 and Kotlin Plugin

  • We must download and install Android Studio 3.0 at (current version is 3.0.1)
  • Checking Kotlin plugin installed by selecting File > Settings

Creating a New Android Application Using Kotlin

  • Creating a new project by selecting File > New > New Project . Enter a text for Application name and check out Include Kotlin Support checkbox as the following screenshot:
  • Click Next and Next and select Empty Activity . Click Next and Finish .
  • So far, we have activity_main.xml file (includes two modes: Design and Text ) and MainActivity.kt file.
  • In right window, select app > res > values > strings.xml . Add a string element name hello_world to strings.xml file as follows:
    My First Kotlin App
    Hello Kotlin
  • Select activity_main.xml file in Design mode. Select TextView and set a value ( message ) for ID and set a value (@string/hello_world) for text in Attributes window:
  • Select MainActivity.kt file and adding the import as follows:
  • Add the following code line to the onCreate method:
message.text = "Hello My Kotlin App in Android Studio 3.0"
  • Do you remember the message ? It is ID of TextView .
  • The complete code of MainActivity.kt , so far, can look like:
package com.example.admin.myfirstkotlinapp    
import android.os.Bundle  
class MainActivity : AppCompatActivity() {        
override fun onCreate(savedInstanceState: Bundle?) {          
     message.text = "Hello My Kotlin App in Android Studio 3.0"      
     }  }
  • Run App and the message " Hello My Kotlin App in Android Studio 3.0 " will display.


In this article, I have only introduced how to install and create a simple Android application using Kotlin in Android Studio 3.0. In my next articles, I will introduce about more Kotlin language and how to create more complicate Android applications.




Android Studio 打包 jar 及 aar 包 Android Studio 打包 jar 及 aar 包 创建工程 New -> Module -> Library 在gradle文件头添加 apply plugin: '' 打包模块 在库工程的 bui...
官方新出的 Kotlin 扩赞库 KTX,到底帮你干了什么?... Kotlin KTX 2月5号的时候,Google 发布了一款 Kotlin 的扩展库,叫 Android KTX,不过现在还处于预览版的状态。它能使 Android 上的 Kotlin 代码更简洁,从而提高开发者的效率和使用体验。 Android KTX 其实就是一个 Kot...
Calling Kotlin from C++ At Highrise we’ve been building the future of our platform in a way that supports Android. We’ve put together a stack that is pretty challenging b...
6个能让你的 Kotlin 代码库更有意思的“魔法糖” —— 第二部分... 尝试新事物。我认为人们所说的许多智力都归结于好奇心。—— Aaron Swartz 本篇是三部曲中的第二部分。你应该先读完第一部分再来阅读本篇。 我们将通过几个我很喜欢的 Kotlin 中的结构来继续我们之前的旅程。在第一部分中,你已经学习了如何使用 sealed 类以及如何简单优雅...
Google releases source code for I/O 2018 app w/ Ko... Every year, Google releases the source code for the I/O Android companion app several weeks after the developer conference. The 2018 version is notab...