Hot Articles Overview | Click Title to Read
2018 is Coming, BAT and Other Internet Companies’ Android Interview Questions Summary
Advanced Android and Java Architecture Video Sharing
Details and Principles of QR Code Generation
MVP Kotlin Official Address https://github.com/googlesamples/android-architecture/tree/dev-todo-mvp-kotlin/
Looking at the official website, we find that the actual code amount comparison is less than Java’s MVP, it seems that Kotlin’s syntactic sugar optimization is quite good.
A simple observation from the file directory shows that there is not much difference from the Todo-mvp project, just that all the files inside have been changed to Kotlin files.
Here, Kotlin version 1.1.3-2 is introduced, without directly introducing the configuration for using Android Studio 3.0 (this big pit, whoever steps in knows).
The root directory’s build.gradle sets the configuration version to api26.
Api26 is Android O, which has been tested. Api26 has many optimized and modified interfaces; if you don’t use Api26, it will not run smoothly.
Let’s continue to see how we can improve configurations in build.gradle.
The useProguard parameter can configure whether to use obfuscation.
applicationIdSuffix can be used to directly add a suffix to the applicationId attribute’s value.
Source Code Optimization Analysis
1.Lateinit
In the View declaration phase, lateinit is needed to delay variable declaration.
Kotlin’s delayed declaration also includes the lazy method.
val name: String by lazy {“cangwang”}
lateinit var drawLayout: drawLayout
The difference is:
(1) .lazy{} can only be used in val type, lateinit can only be used in var type.
(2) .lateinit cannot be used on nullable properties and Java’s basic types.
lateinit var name: String // will report an error
(3) .lateinit can be initialized at any position and can be initialized multiple times because it connects to var variables. Lazy is initialized the first time it is called, connecting to val constants, and can only be changed by redefining.
2.findViewById
Before Api26
After Api26
It is evident that after Api26, the method has been optimized to use generics to suggest object types.
3.Next, I will introduce five functions from Kotlin’s Standard.kt: apply, with, let, run, also.
First, let’s introduce the function of apply.
The source code
Within the function block, this refers to the object, and the return value is the object itself.
The with function
The source code
It takes an object as a function parameter, and within the function block, this refers to the object. The return value is the last line of the function block or a specified return expression.
The let function
The source code
It takes the object as a function parameter, and within the function block, it refers to the object as it. The return value is the last line of the function block or a specified return expression.
The run function
The source code
It has two forms of expression.
The first form has no parameter input.
The second form will pass the object itself (this) to the function call.
The return value is the last line of the function block or a specified return expression.
After Kotlin 1.1, the also function was added.
The source code
This overall screenshot will help you better understand how to invoke.
The common point is that they can all run function blocks, but the four functions have their differences, so choose carefully when using them.
4.Object
Singleton objects are declared using Object.
Kotlin does not have static properties and methods; singleton objects are needed to achieve similar functionality.
Kotlin does not have static properties and methods; singleton objects are needed to achieve similar functionality.
5.data
Equivalent to the data bean class defined in Java.
It can directly write get() and set() methods after the attribute.
6.@JvmOverloads
Implement method overloading in Java calls.
The overloaded constructor of the Kotlin class is illustrated, and the methods that can be initialized are as follows.
Task task1 = new Task(“cang_wang”);
Task task2 = new Task(“cang_wang”,”Programmer”);
Task task2 = new Task(“cang_wang”,”Programmer”,”1″);
7.Decompiling Bytecode
You can see the compiled Java bytecode.
Using the Decompile button, you can understand the written Kotlin through Java’s code logic.
Here are all the optimization points; other basic syntax will not be introduced further.
Conclusion
1. The construction of Kotlin’s MVP is not much different from Java.
2. Kotlin provides many optimized syntaxes.
3. Further understanding of Kotlin’s compilation principles leads to more efficient improvements.
Practical Demo can be accessed directly by clicking the bottom left “Read Original“
If you have good articles to share, feel free to submit the article link directly to me
Java and Android Architecture
Scan the QR code below or click the QR code to receive advanced Android resources
Follow and reply with “Baidu”, “Ali”, “Tencent”, “Resources” for surprises
Public Account:JANiubility
Welcome to join our Java and Android architecture circle, nearly 1000 people have joined for learning and communication, more learning resources will be updated, and more communication for progress.
For more learning materials, click the “Read Original” below to obtain