Explain in detail the integration of kotlin and anko for Android Development

Kotlin is a programming language based on JVM. Recently, kotlin and anko have been studied. Combined with the current app design mode, the preliminary development mode is envisaged. And ready for application in new projects.

Kotlin and anko

Kotlin is a JVM language developed by the famous JB company. Its official website address is; http://kotlinlang.org/

The official website introduces:

Kotlin's design idea is very lightweight. It reuses java code as much as possible. Generally, it will not implement a set of large and complete libraries by itself when it is a last resort. This makes kotlin very lightweight. The project integrated into Android will not significantly affect the final packaging size.

I have summarized the advantages of kotlin:

1. Seamless call with Java, which does not need to invest a lot of energy in the initial stage. Even in case of uncertain pit, you don't have to worry about affecting the progress of business development. Just change to Java directly.

2. A lot of syntax sugar makes the code very concise, and the development efficiency after familiarity is higher than that of Java. For example, the simple encapsulation of extension functions no longer needs to write a lot of utils tool classes. It is OK to add extension methods to some classes directly and flexibly. For example:

In this way, there is an extra toast method in the activity class. In fact, there are also a large number of written extension methods in anko, which can directly use DSL syntax to write UI. Another example is when statement:

Obviously, compared with the traditional Java writing method, the language is more concise after using anko.

3, more secure, Kotlin seems to want to eliminate empty references. In Java, calling a null object throws NullPointException. In Kotlin, objects that cannot be empty, such as String objects, will be written as:

4. Good ecological environment and development community. Kotlin is still a relatively new technology, and many people are trying its advantages. Including RX series, rxkotlin, the kotlin version of rxjava, is also available

Used by kotlin and anko

Anko's GitHub address is https://github.com/Kotlin/anko 。 The steps to integrate anko are as follows:

First, add the following code to the project build file:

Then, add dependencies in the build file of your app moudle:

According to kotlin's advantages, choose anko instead of XML.

Such benefits are described in anko's GitHub readme file as follows:

Specifically, first write the mainactivity class to display UI and event logic.

Next, write the UI layout in oncreate. The login layout is relatively simple, that is, two input boxes and a button. Of course, in order to try out common multi-point controls, I added a toolbar at the top.

The ID in the code uses constants. There is no concept of static in kotlin, but there is a company object (companion object) that can simulate and implement functions similar to static.

As you can see, the code for writing UI in this way is very concise. And the readability is very high. I believe that students who are familiar with XML writing layout can understand the meaning of the code. At the same time, the official also released an Android stduio plug-in called anko SDL preview. It is convenient for developers to preview the effect of writing. Next, according to the idea of MVP, write baseview and basepresenter interfaces.

Use kotlin to write maincontract interface, which establishes the communication between V layer and p layer.

Write p-layer code in Java.

This is just a simulation of the login logic, not a real login

Go back to mainactivity and add our code here.

Implement the callback method that layer V should implement:

Here, we have completed the example of kotlin and anko developing Android. If you want to use it more skillfully, you need to practice more.

Attachment: anko's GitHub address is https://github.com/Kotlin/anko

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