Kotlin (1) Build the Kotlin development environment and kotlin

Source: Internet
Author: User

Kotlin (1) Build the Kotlin development environment and kotlin

As a programming language, Kotlin has been around for several years, but it is not well-known in China. Since Google announced it as the official Android development language in May, Kotlin has suddenly become popular. Although Kotlin cannot replace Java in the short term, we still need to learn a new technology.

Google claims to have fully supported Kotlin since Android Studio 3.0, but the current stable version of Android Studio is 2.3, so this article is still based on Android Studio 2.3, describes how to build a Kotlin Development Environment Based on Android Studio.

First, you must download and install Android Studio 2.3. If you have not used this version of Android Studio, go to the Android official website to obtain the installation package. The download page is https://developer.android.google.cn/studio/index.html. To learn how to install and use Android Studio: from scratch to App launch. If Android Studio is upgraded from a lower version to 2.3, compilation may occur. For specific solutions, see the blog Android Studio upgrade to 2.3 compilation solution.

After Android Studio 2.3 is installed, you must also install the Kotlin plug-in. Choose "File"> "Settings" from the menu, and select "Plugins"> "Browse repositories..." in the displayed window, as shown in:

The browser window of the plug-in library is displayed. Enter "Kotlin" in the search box in the upper-left corner of the page. The plug-in list below will locate the plug-in that meets the search criteria, click "Kotlin" (AGEs). The details of the Kotlin plug-in are displayed on the right of the window, as shown in:

You can see that the latest version of Kotlin is 1.1.2 released on April 9, June 13. Click the "Install" button to perform the installation. Then Android Studio starts automatically downloading the Kotlin plug-in, as shown in the following figure:

After Kotlin is downloaded and installed, the original "Install" button changes to "Restart Android Studio", prompting you to Restart Android Studio to make the new plug-in take effect, as shown in:

Close Android Studio and then open it as prompted to use the Kotlin plug-in Android Studio. However, to develop Kotlin encoding, you must modify the compilation configuration file as follows:
1. Open build. gradle of the project and set the Kotlin version number and plug-in path respectively. The modified compilation configuration is as follows:

Buildscript {// specify the version of the kotlin plug-in ext. kotlin_version = "1.1.2" repositories {jcenter ()} dependencies {classpath 'com. android. tools. build: gradle: 2.3.0 '// specify the path of the kotlin plug-in classpath "org. jetbrains. kotlin: kotlin-gradle-plugin: $ kotlin_version "}}

2. Open build. gradle of the module, and apply the Kotlin plug-in at the beginning of the file, that is, add the following line:

apply plugin: 'kotlin-android'

Add the following line under the dependencies node to compile the kotlin plug-in into this module:

// Compile the kotlin plug-in into compile "org. jetbrains. kotlin: kotlin-stdlib: $ kotlin_version"

After compiling the configuration file, try whether Kotlin can be used. Assuming that the reader does not have the Kotlin foundation yet, follow the general process of App development to create a New Module, and choose "File"> "New"> "New Module" from the menu in sequence ", click "Next" to create the module. Next, open MainActivity. java, and the content of this file is no longer familiar, that is, the simplest lines of Java code, as shown below:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

Now we need to switch the Java code to the Kotlin code. Select MainActivity. java first, and then select "Code" -- "Convert Java File to Kotlin File" on the main interface. The menu position is shown in:

After the code is converted, the original MainActivity. java becomes MainActivity. kt, and the file content becomes the following Kotlin code:

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }}

It seems that the syntax of this Kotlin is different from that of Java. If you want to explain the detailed syntax rules of Kotlin, it is necessary to write a long article. Therefore, we should first run the demo here, choose "Run"> "Run 'hello'" from the menu to start the application. Normally, you can see "hello world" in the connected simulator or on the real machine, as shown in:

This is a real App developed with Kotlin. It all says that everything is difficult at the beginning. Setting up the Kotlin development environment is just the first step in the long journey. In the next article, we will continue to learn how to use Kotlin for Android development.

This article has now been published to the Public Account "Old Europe says android", scan the QR code below, or directly search the Public Account "Old Europe says android" to add attention, read technical knowledge faster and more conveniently.


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.