YAC - Kotlin WrapupJuly 04, 2017
3 minutes to read.
Oh my god!!! Kotlin is now officially supported by Android. When I was watching Google I/O livestream, it was the sleeping time here in India, and once I seen that lock pattern being emerged as our favorite Kotlin logo, I errupted with joy and almost wokeup my elders…
But it was worth it, without doubt this announcement was accepted with wide cheers and enthusiasm. And working with it since a year, this comes as acceptance and relief both for us. Thanks a lot Google.
Initially I was planning this post to highlight some of small details of how Kotlin can be benifitial for us. But between 17th May and today, lot has been highlighted and written about Kotlin. By not repeating all here and writing millionth post about how kotlin is awesome about null pointer etc… I will change the cource a bit.
At first I have some experience to share on some small tips and tricks that may help you working with Kotlin, and in later (keeping original idea in mind) I will announce some open source works.
Some tips and gotchas.
If you are starting with kotlin exploration, I suggest to start with kotlin koans. They are great tutorials to get hold on the language.
Don’t start with browser, the temptation is there, you can start right away. But IMO best way to start koans is in your IDE. Using IDE, you will get a better idea of language then using browser.
If you can’t start koans in Android Studio, go with Intellij Idea and Kotlin Education Plugin. With Android studio preview (3.0 preview2) we could not run tests. We had couple of intellij community edition installed to try flutter, and discovered Kotlin Education plugin is nearly same way as running koans in android studio.
Converting your code.
Have a shortcut to convert your files(I have
Alt+Ctrl+Shift+K), try to have a commit before you convert your file, so you can compare and revert if needed.
Start with most standalone files first, so that you have less changes to commit. We tried to convert utils file (Had all our static utility methods), it changed almost every classes in our app. We ended up reverting that changes.
Keeping with above point, your presenters in MVP can be considered as standalone classes. With each presenters we had to convert only 3 classes-Presenter Interface, Presenters Implementation and View Interface. And rest remained unchanged (SideNote: Just by converting presenters, we ended up reducing 1000 lines and effectively reducing all nullpointers and index related crashes)
When your java classes call kotlin presenters, be open to add Nullable notation (?) in all params. It’s highly possible that java can pass null where Kotlin does not expect it to be null. And compiler won’t complain. In java,
@NotNullannotation is forced when kotlin is not returning any null, but when passing to kotlin no kind of annotation is forced. (Can’t say about Lint or inferred annotations, I did not try them)
We can convert our data classes to parcelables without plugin (It works in Latest Android Studio 3.0 Preview 5). It works perfectly as far as there are primitive types as your properties. If your class has Other type or ArrayList, parcelable intention does not generate read or write implementaion for them. It generates a
TODO(). You can overcome this by using this plugin.
As I said in starting of this post. Android officiating Kotlin has happily changed a lot of things, including course of this post. I was to discuss more kotlin code from YAC, which tried to point out the power of this language. And since I/O those points are discussed many times over many posts.
But this does not change sharing some open-source work which born out of YAC, or YAC gave upgrade to it.
The first one is Kotlin’s port of old library Android External File Writer, The kotlin port is nearly same code that opens file browser and reads response from it in YAC app. You can use it by dropping below line in your gradle file
The second one is Kutils. This library is collection of extension functions I use in my projects. This library has started as fork of Kaffeine, which isn’t updated since last 2 years. Retaining structures of this project, I have developed KUtils adding methods and solving some mistakes as Kotlin and YAC evolved.
I have also added some extensions and methods from Anko. When I have started, Anko was a big mammoth library for layuot, extensions and sqlite. From that day and now, anko is split into multiple libraries for peace of mind. Wouldn’t it be nice to have my aditions to anko itself? Why to copy the code and develop my own library for the sake of it?
The answer is I feel anko is either stagnant or not very useful for the people who use it. I have never felt the urge of using anko out of having the extension functions. There are members in community who use Anko for layout, but for me anko is good (and official) collections of extension functions, for common things, Sqlite and Layouts. The instructions to build Anko are (For me) unclear for me whether they are for Plugin or for library as well. I have some questions, and till the confusion regarding this is cleared, I have hosted this code in Kutils library. You can use KUtils using
These is it for kotlin and my libraries. In next sub-series about YAC, I will talk about how I used firebase for some trivial usecases for YAC.
This Series states some stories behind the App I have developed, to try the app, click below to download it from playstore…comments powered by Disqus