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
Although the VR field has only started to flouris...
May 28 He is an academician of the Chinese Academ...
Before deciding to upgrade your SSD, you should fi...
Facebook recently authorized Deloitte to quantify...
Live streaming sales are in full swing, how will ...
Plastic fragments with a diameter of less than 5 ...
In 2014, as the Internet TV industry entered anot...
Review of recent rumors related to the epidemic R...
Image from: freepik.com It’s the season for eatin...
On May 1, the clinical study of the inactivated C...
Your browser does not support the video tag Edito...
When you are in a hurry on the road, you should a...
The cost remained at 180 a few days ago, why did ...
As we all know, the cost of acquiring P2P custome...
Following Jia Yueting's Weibo post last month...