The simplest way to configure the Kotlin development environment for Android Studio

Source: Internet
Author: User

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
    1. Kotlin Official website: http://kotlinlang.org/
    2. Kotlin User's Manual (English): http://kotlinlang.org/docs/reference/
    3. Kotin Open Source Address: Https://github.com/JetBrains/kotlin
    4. Official introduction How to get started with kotlin:http://kotlinlang.org/docs/tutorials/getting-started.html
    5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.