preface
I can’t remember when I looked at the Official Android documentation and found that the sample code provided was already Kotlin…
GitHub also found more and more open source libraries based on Kotlin…
Books and materials on Kotlin have also started to accumulate…
Kotlin, an amazing site… Bah… Is a static development language for the JVM, released in version 1.0 in 2016, announced by Google as the official Development language for Android in 2017, and the upcoming WORLDWIDE Developers Conference in 2018…
All these signs make me curious and eager to try Kotlin again from the previous wait-and-see attitude.
Therefore, when time permits, I recently tried to rewrite a module of the project through Kotlin. The result was just like eating Daomai. I found that I could not stop, and directly rewrote all the Java code of the whole project into Kotlin. Finally, even Gradle’s build scripting language was switched to Kotlin DSL.
Well, Kotlin is toxic…
About the project
Because the business functions of this project are not very complex, this practice directly rewrites all the Java codes in the project with Kotlin. Let’s take a look at the main functional modules:
- The login function
- The message center
- Unified container (H5, Weex container)
- Social sharing
- Sweep the code
It can be seen that the App is biased towards the end container, and the main business is written using H5 and Weex pages, while the business on Natvie is not very complex.
It is important to note that this rewrite is not a direct tool to convert Java class files to Kotlin. Although the Kotlin plugin provides this functionality, it is not recommended because it does not take full advantage of the real power of Kotlin. For example, after conversion, you might see a lot of? And!!!!! Symbols, which is caused due to the nature of Kotlin empty security, another is you are to rewrite, equivalent to reconstruct the again before your code or someone else’s code, can be very good to help you to understand and optimize the code before logic, will remove a lot of useless classes and code, test is very useful, but not the investment of time.
practice
There are detailed instructions on how to use Kotlin in existing Android projects, which can be summarized in two steps:
- Upgrade Android Studio to version 3.0 or higher
- Add the Kotlin Plugin and Kotlin dependency libraries
Before and after rewrite
-
In terms of package size, an increase of about 60 KB, due to the need to introduce Kotlin’s dependent libraries, is almost negligible given the current size of Apk.
-
30% code reduction
Other benefits
-
The code is simple
Data class User(val name: String, val sex: String, val email: String, val age: String)Copy the code
-
Extension functions replace many Utils classes
-
Say goodbye to findViewById, which is better than ButterKnife
import kotlinx.android.synthetic.main.view_toolbar.* open class WebContainerActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_web_page) toolbar.title = "I am title " } } Copy the code
-
You can’t get enough of scope functions
-
Powerful expression statements that use when instead of switch and if else
AppConfig.get().apply { when(EnvPreferences.getEnv(app)) { ONLINE -> { ... } PREPARE -> { ... } DAILY -> { ... }}}Copy the code
Input costs
The whole project refactoring took about two weeks (including learning from Kotlin), but the process went well and a version was successfully launched.
feeling
Because I’ve been developing in Java all my life, using Kotlin was like opening up a new world where writing code can be so cool and exciting.
I also recommend you check out this short video, 3 minutes or so, on how Kotlin is viewed by foreign developers, including Android star Jake Wharton
Android Developer Story: Kotlin + Android increasing developer happiness and productivity
Since I’m new to Kotlin, I’ll take a closer look at some of its advanced features and extension libraries, such as:
- Better use of dynamic proxies
- True pansexual
- Kotlinx. coroutines, a more useful alternative thread library
- Android – KTX, easy to use Android extension library
- .
Last but not least, we expect Kotlin to become the mainstream Android development language soon, thanks to Google’s support. Hopefully, at Kotlin WORLDWIDE Developers Conference in October, there will be more big announcements.
Kotlin is so easy to use that, in the end, I want to summarize some of the benefits of using Kotlin:
- Clean code,
- Compatible with Java, bilingual mixed writing
- Functional programming, all kinds of syntax sugar
- Improve programming efficiency
Finally, please allow me to borrow a picture to express it:
Related reading
Gradle Guide to migrating from Groovy to Kotlin