Initial experience of developing Android applications with kotlin

Last night, the latest Google IO conference officially identified kotlin as the official development language. As an android development bird, how can we not taste it in time?

Brief introduction to kotlin

Before development, many students must have many questions. What are the benefits of kotlin and how to coexist with existing projects? Do kotlin have all the features of Java? Well, let's look at them one by one.

Kotlin is very suitable for developing Android applications, bringing all the advantages of modern language into the Android platform without introducing any new restrictions:

Kotlin case study for Android

Kotlin has been successfully adopted by some large companies, some of which share their experience:

The kotlin team has provided a set of tools for Android development that go beyond the functions of standard languages:

next step

Developing Android applications using kotlin

Before learning to use kotlin to develop Android applications, we first need to prepare our environment, that is, we must first configure the environment of Android studio 3.0.

Create kotlin development project

The project creation here is no different from the ordinary Android application project creation. The only difference is that the include kotlin support option is checked when creating the project. As shown in the figure:

Kotlin's code is already in the default created activity, which looks very similar to the Java code itself:

Kotlin files end in. KT.

By default, the created kotlin file will still be placed in the same location as the java file. If you need to separate the kotlin file from the java file, you can add the following configuration to the corresponding gradle file:

In this way, all kotlin files created by default will be placed in the / kotlin / directory.

Add kotlin to an existing project

If you need to add kotlin support to an existing project, you only need to select kotlin in the source language option when creating a new activity:

Convert java code to kotlin code

Tools for converting java code into kotlin code are provided in Android studio 3.0. When using, you only need to open the corresponding java file, and then select code - > Convert java file to kotlin file on the menu bar to complete the conversion.

However, it should be noted that this tool can complete most syntax conversion. There may be some problems in the conversion of individual types, such as arrays, which need to be adjusted manually. Blogger, I found this problem during my practice, but I quickly corrected it through the grammar document. It is found that kotlin is indeed much better than Java in syntax simplicity. For example, the following code:

When polling an array, this code is much simpler than the foreach loop of Java. I represents a bitmap object here, and bitmaps is an array. Is it simple?

Using java code through kotlin

Kotlin provides very strong interoperability. What the hell is this interoperability? That is, kotlin can easily call java code, and Java code can also easily call kotlin code. How convenient is this? Kotlin can call java code directly. If you know JNI, you know how concise and convenient kotlin interoperability is.

Declare activity with kotlin:

Set click events using kotlin:

See, setcontentview above and toast below are the APIs provided by Android, which we can use directly. And the new onclicklistener object is not required inside the setonclicklistener method! Write directly and click to realize it! In addition, the first parameter of toast, this, must be written as activity.this in Java, and only this is required here! Isn't it niubility?

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
分享
二维码
< <上一篇
下一篇>>