编辑本页

Android 与 Kotlin 入门

本教程将引导我们为 Android 创建一个简单的 Kotlin 应用程序。

It’s extremely easy to start using Kotlin for Android development. In this tutorial we’ll follow the warming up process with Android Studio. If you're using Intellij IDEA with Android, the process is almost the same.

Creating a project

First, create a new Kotlin Android Project for your application:

  1. Open Android Studio and click Start a new Android Studio project on the welcome screen or File | New | New project.

  2. Select an activity that defines the behavior of your application. For your first "Hello world" application, select Empty Activity that just shows a screen, and click Next.

    Choosing empty activity

  3. In the next dialog, provide the project details:

    • name and package
    • location
    • language: select Kotlin

    Leave other options with their default values and click Finish.

    Project configuration

Once you complete the steps, Android Studio creates a project. The project already contains all the code and resources for building an application that can run on your Android device or an emulator.

Building and running the application

The process of building and running the Kotlin application in Android Studio is exactly the same as with Java.

To build and run your application on an emulator:

  1. Run the predefined app configuration by clicking Run on the toolbar or Run | Run 'app'.
  2. Select Create New Virtual Device.

    Select target

  3. Select a device you like and click Next.

    Select device

  4. Select a system version and download its image.

    System image

  5. Verify the emulator configuration and click Finish.

  6. Click OK and here it is - your first Kotlin application for Android!

Kotlin有着极小的运行时文件体积:整个库的大小约 964KB(1.3.41 版本)。这意味着 Kotlin 对 apk 文件大小影响微乎其微。

就对比 Kotlin 与 Java所编写的程序而言,Kotlin 编译器所生成的字节码看上去几乎毫无差异。

If you want to customize your builds or run configuration, refer to the Android Studio documentation.

后续?