background Recently we are working on a blockchain-related wallet project. The new App uses a brand new technology stack. In Android, we use Kotlin+RxJava+Android Architecture Components, and in iOS, we use Swift+RxSwift. This article does not discuss the architecture of the App, but only discusses the features of Kotlin used in the project. In Android App, it is no exaggeration to say that more than 95% of our code is developed using Kotlin. Therefore, it is necessary to make a brief summary of the use of Kotlin in this stage. Kotlin features used: 1. Extension Function Kotlin allows developers to add new functions to a class without changing the existing class. This feature is called extension function. Let's take a simple example: if you want to close an I/O stream, you might write a utility method in Java.
For Kotlin, you can extend Closeable with a function closeQuietly().
After that, any class that implements the Closeable interface can use its own closeQuietly() method to close the stream. We no longer need that utility method. In the project, we use extension functions to encapsulate Glide, which greatly simplifies the use of Glide.
In addition, we use extension functions in many places. I also updated my Kolin tool library, which includes various utils and extensions. https://github.com/fengzhizi715/SAF-Kotlin-Utils 2. Trailing Closure I didn't understand this concept at first. By chance, I saw our friends write the following code when using RxBus:
I was confused at the time because I wrote RxBus and I remember that it didn't provide such a method. After clicking on the register() method, I found that register is like this:
Since Kotlin is used, the use of the register method can be simplified as follows:
Since the first parameter of register() is a method or a closure, you can move the method or closure to the outermost part. It will become what you see in the project:
This is the trailing closure, which makes the code look more concise. 3. Usage of with With is to use an object as a parameter of a function. In the function block, this can refer to the object. In the function block, you can directly call the method or property of the object.
An Adapter before using with
After using with, the function block can omit "content."
IV. Others This part is not a feature of Kotlin, but a tool I developed using Kotlin, such as the logging framework L and Retrofit's logging interceptor. These libraries were actually developed a long time ago, and their functions have been slightly upgraded recently. L's github address:
The github address of Retrofit log interceptor:
The effect diagram of the log interceptor:
Summarize Kotlin absorbs the advantages of many languages and has many exciting features compared to Java, which greatly improves development efficiency. The features introduced in this article are just a drop in the ocean. Next, I will sort out more Kotlin features used in projects. BTW, when I was writing this article, the first version of the domestic wallet had just been completed and the first round of testing began. |
>>: Make iPhone fingerprint unlocking more sensitive | Qinggong
Before reading this article, you need to make sur...
Today is World Fisheries Day. Fish are the most d...
Friends who have watched the science fiction movi...
Author: Fang Yangxuan, Zhao Jingyuan Review | Zou...
Mount Emei is one of the four famous Buddhist mou...
Li Ruigang did something that outsiders "cou...
Recently, a toilet-like remains dating back about...
Could Black Swans Become an Invasive Species in m...
Starts May 26 The "drama" of rising tem...
This article provides a comprehensive analysis of...
As the most classic chat software in China, QQ is...
After several consecutive years of Double Eleven ...
This article will use a case to fully interpret t...
Reviewer of this article: Chen Haixu, Deputy Dire...
Ionic 1.0.0 released, codenamed "uranium-uni...