Open the Settings panel, find the Plugins option, click Browse Repositories (browse the warehouse), enter "Kotlin" to find it, and then install it. Restart Android Studio after installation is complete (remember!) )。
After the installation is complete, as shown in.
The current latest version of the plugin is 1.1.2-release-Studio-2.3-3
.
Step Two: Configure the Kotlin development environment
Click the Tools option in the menu bar, select Kotlin, and then select Configure Kotlin in Project. As shown in.
In the pop-up window, select the module that needs to use Kotlin and the version of the Kotlin compiler and runtime , as shown in.
After clicking "OK", the Kotlin plugin will start the configuration automatically. Once the configuration is complete, synchronize the project (Sync Project).
[OPTIONAL]: Click on the "Code" menu item in the menu bar and select "convert Javafile to Kotlin file" to convert the existing Java files to the Kotlin file according to the previous configuration.
Attached: Recommended configuration
Open the file under the module build.gradle
and apply plugin: ‘kotlin-android‘
add a line below: apply plugin: ‘kotlin-android-extensions‘
. This is a Kotlin extension module that allows activity to automatically correlate the view in the XML layout without the need findViewById
.
For more information, please refer to: http://kotlinlang.org/docs/tutorials/android-plugin.html
Attached: Writing unit tests using Kotlin
In Android development, it is unavoidable to do a variety of unit tests, writing unit tests using Kotlin can simplify code and improve efficiency.
Switch the project to Project view, expand the SRC directory under the module, and the directory will have three folders by default. The main folder is usually used to store module code; Androidtest folders are often used to store Android-related unit tests; The test folder is typically used to store Java (Kotlin)-related unit tests.
Unit Tests for Kotlin
Create a new Kotlin Class under the test package, for example named UnitTest1. In this class, you can write multiple test methods without detailed descriptions.
Package CC.Duduhuo. kotlintestimport org.junit. Testimport org.junit. Assert.*class UnitTest1 {@Test fun addition_iscorrect() { assertequals( 4, (2 + 2). Tolong())}}
Unit Tests for Android
Create a new Kotlin Class under the test package, for example named AndroidTest1. In this class, you can write multiple test methods without detailed descriptions.
Package CC.Duduhuo.KotlintestImport Android.Support.Test.InstrumentationregistryImport Android.Support.Test.Runner.AndroidJUnit4import org.Junit.Assert.Assertequalsimport org.Junit.Testimport org.Junit.Runner.runwith@RunWith (androidjunit4::class< Span class= "Hljs-class" >) class androidtest1 { @Test fun //Context of the app under test. val appContext = instrumentationregistry.< Span class= "Fu" >gettargetcontext () assertequals ( " Cc.duduhuo.kotlintest ", AppContext. packagename)}}
Attached: Some useful information about Kotlin
- Kotlin Official website: http://kotlinlang.org/
- Kotlin User's Manual (English): http://kotlinlang.org/docs/reference/
- Kotin Open Source Address: Https://github.com/JetBrains/kotlin
- Official introduction How to get started with kotlin:http://kotlinlang.org/docs/tutorials/getting-started.html
- Some libraries, frameworks, and applications related to Kotlin: http://kotlinlang.org/docs/resources.html
The simplest way to configure the Kotlin development environment for Android Studio