官术网_书友最值得收藏!

Configuring Kotlin for the project

Starting from Android Studio 3.0, full tooling support for Kotlin was added. Installation of the Kotlin plugin is not required and Kotlin is integrated even deeper into the Android development process.

To use Kotlin with Android Studio 2.x, we must manually install the Kotlin plugin. To install it, we need to go to Android Studio | File | Settings | Plugins | Install JetBrains plugin... | Kotlin and press the Install button:

To be able to use Kotlin, we need to configure Kotlin in our project. For existing Java projects, we need to run the Configure Kotlin in project action (the shortcut in Windows is Ctrl+Shift+A, and in macOS, it is command + shift + A) or use the corresponding Tools | Kotlin | Configure Kotlin in Project menu item:

Then, select Android with Gradle:

Finally, we need to select the required modules and the proper Kotlin version:

The preceding configuration scenario also applies to all existing Android projects that were initially created in Java. Starting from Android Studio 3.0, we can also check the Include Kotlin support checkbox while creating a new project:

In both scenarios, the Configure Kotlin in project command updates the root build.gradle file and the build.gradle files corresponding to the module(s) by adding Kotlin dependencies. It also adds the Kotlin plugin to the Android module. At the time of writing this book release version of Android Studio 3 is not yet available, but we can review the build script from the pre-release version:

//build.gradle file in project root folder 
buildscript { 
    ext.kotlin_version = '1.1' 
 
    repositories { 
        google() 
        jcenter() 
    } 
    dependencies { 
       classpath 'com.android.tools.build:gradle:3.0.0-alpha9' 
       classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version" } }
... //build.gradle file in the selected modules apply plugin: 'com.android.application' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
... dependencies { ...
implementation 'com.android.support.constraint:constraint-
layout:1.0.2'
} ...
Prior to the Android plugin for Gradle 3.x (delivered with Android Studio 3.0),  compile dependency configuration was used instead of implementation.

To update the Kotlin version (let us say in the future), we need to change the value of the kotlin_version variable in the build.gradle file (project root folder). Changes in Gradle files mean that the project must be synchronized, so Gradle can update its configuration and download all the required dependencies:

主站蜘蛛池模板: 丽水市| 汉寿县| 平乐县| 盈江县| 高密市| 泰安市| 康马县| 遂昌县| 鄢陵县| 高雄县| 鄂伦春自治旗| 石城县| 麻阳| 福鼎市| 华亭县| 农安县| 石渠县| 西乌珠穆沁旗| 闽侯县| 萍乡市| 东乡县| 大庆市| 远安县| 石渠县| 枝江市| 阳城县| 离岛区| 内丘县| 麦盖提县| 达日县| 长垣县| 荆门市| 故城县| 曲麻莱县| 柘城县| 荃湾区| 金华市| 托克逊县| 韶山市| 曲靖市| 开封市|