Log.v(“ThreeTips”, “#17”)

Kotlin is new trend and I see everyone is switching to Kotlin language in Android Development. So In my next tips, I will use more Kotlin.

1 — Operator Overloading in Kotlin

If you use RxJava in your android project, you need to create a CompositeDisposable and add every disposable that you created to the CompositeDisposable. Without operator overloading, we do it like the following;

val disposables = CompositeDisposable()
val observable1 = your_observable
disposables.add(observable1)

You can define plus operator for adding new disposable to composite disposable.

operator fun CompositeDisposable.plusAssign(disposable: Disposable){
add(disposable)
}

It will simply call this extension function when you use “+” operator to add disposable to composite disposable. So you can use it just like following.

disposables += observable1

2 — Using font in Support Library (As of 26.0.0)

Caused by: java.lang.NullPointerException
at android.graphics.FontFamily.nAddFontFromAssetManager

Solution, declare both sets of attributes, app: and android:, in your fontStyle.

<font
android:font="@font/product_sans_regular"
android:fontStyle="normal"
android:fontWeight="400"

app:font="@font/product_sans_regular"
app:fontStyle="normal"
app:fontWeight="400" />

3 — Gradle 3.0: “api” vs “implementations”

//before gradle 3.0
compile
“android.arch.lifecycle:runtime:1.0.3”
//After gradle 3.0
implementation
“android.arch.lifecycle:runtime:1.0.3”

So what is “implementation” and what is “api”? Should I use “implementation” key instead of “compile”?

No. Actually the key explain itself. If you want to provide your dependency as an API then you should use “api”. If you want to use the dependency as an implementation on your current module, then you should use “implementation”. When you use “implementation” instead of “compile”, you won’t be able to use those classes which belongs to the library in you sub modules.

Basically you can use “api” instead of “compile”.

But if you want faster build time in your app, you can do following.

A module →B module → C module → App Module

If “A module” is only needed in your “B module”, you can use implementation(“:Amodule”) in your “B module”. If you change your code in “A module”, “C” and “App” module won’t be recompiled again. So your build will be faster.

Bedanta has a great blogpost about this. Check it out from here.

Software Engineer at Storytel

Software Engineer at Storytel