Five minutes to teach you how to write Android kotlin project

background

I've seen kotlin before, and many great gods and brothers and sisters who are willing to walk in the forefront of knowledge say how good this language is, but I didn't know him until Google conference directly said that they would support kotlin language some time ago, so I took some time to prepare for learning, Personally, I don't think this thing is just needed so far. If you are interested in learning, you can learn it. If you don't want to learn it, the impact is not great. Well, I don't have much about how good the language is and how much it carries. If you want to know, go out Baidu and give the source code link kotlin Android project here.

Plug in installation

Of course, setting up the environment is the first and most important step, but the kotlin environment is very simple. As long as you install plug-ins, you are personally developed by Android, so take Android studio as an example:

File ― > setting ― > plugins ― > search kotlin

Then click Install, and finally restart Android studio. I have installed this screenshot

Create project

I won't say much about the creation of Android project, because it is Android itself and does not involve kotlin language, so it won't go out to Baidu like the rules we usually create.

Create a kotlinactivity

When we create the plug-in, we will find that there are two more after installing the plug-in:

Kotlin file / class is the corresponding kotlin object. We won't talk about it this time.

Kotlin activity is the kotlinactivity we want to create. If we choose it, the compiler will create an ordinary activity to help us create a layout file and register the activity in androidmanifest.xml.

Groud configuration

I got stuck in this place when I was doing the first HelloWorld. Some people on the Internet said that when creating kotlinactivity, the compiler would help us generate the required configuration in build.gradle, but to be honest, it didn't help me at that time, so how can we configure it manually.

The first is the outermost build. Gradle:

This is my complete configuration file. In fact, you only need to add the top sentence to the root directory

Here's a question. What version number should we write? I met this Ken at that time. I copied a version on the Internet, but it didn't work. In fact, this version number needs to be consistent with the version number of the kotlin plug-in we installed. So how can we check the kotlin version number we installed?

Tools―>Kotlin―>Config Kotlin Plugin Updates―>Check for updates Now

OK, next, let's configure the build.gradle configuration of the module that needs to use kotlin language

This is my configuration file, which is very clear. It is mainly added at the top of the root directory

The next step is to reference kotlin in dependencies

OK, the configuration step is really over. Next, let's take a look at the code.

Code

Let's take a look at the code of the kotlinactivity just created

There is no change in the layout file. First, we define two controls, a textview and a button, and set their IDs

Next, get the two controls in the activity. Let's take a look at the code:

These three sentences have completed setting text for textview, setting text for button and adding click events. Have you found that my wipe is really super simple, and the annoying findviewbyid() has finally disappeared, with some unnecessary new and; No, it's just too clean.

summary

Well, this completes the simplest Android project. Here is the source code address: kotlin Android project

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>