We are happy to announce that we just released our latest beta version of Android SDK written in Kotlin, on our developers hub. Comparing to the old Java implementation of the SDK, the Kotlin version has a list of features which makes it much better:  

First of all, it has an additional Kotlin style interface methods which makes implementation much easier in Kotlin.

Secondly, it is much smaller in size. Because we are using Android NDK instead of go-mobile to bind the native code the SDK size shrunk from 3.8MB to 1.2MB - a huge difference.

Lastly, it is about 2 times faster than old Android SDK in terms of CPU load.

Let's go step by step to see the changes in detail.

Kotlin lambda expressions

In our Chirp Android SDK we have ConnectEventListener interface which has the following implementation:

//java ConnectEventListener interface implementation
ConnectEventListener connectEventListener = new ConnectEventListener() {

  @Override
  public void onSending(byte[] payload, byte channel) {
  
  }

  @Override
  public void onSent(byte[] payload, byte channel) {

  }

  @Override
  public void onReceiving(byte channel) {

  }

  @Override
  public void onReceived(byte[] payload, byte channel) {

  }

  @Override
  public void onStateChanged(byte oldState, byte newState) {

  }

  @Override
  public void onSystemVolumeChanged(int old, int current) {

  }

});

The issue with this is that you have to override each of the interface methods even if you need just one. In comparison, with our new version of Android - Kotlin SDK we introduced lambda expressions for callbacks. Here is how to implement a callback in Kotlin:

//Kotlin onReceived lambda
chirpConnect.onReceived { payload: ByteArray?, channel: Int ->
  /**
  * onReceived is called when a receive event has completed.
  * If the payload was decoded successfully, it is passed in payload.
  * Otherwise, payload is null.
  */
  Log.v(TAG, "onReceived: $payload on channel: $channel")
}

So if you don't need to get events when the state is changed or you don't care about when system volume is changed you can just ignore these events. Same for the `setConfigFromNetwork` method,  it just looks much better in Kotlin:

//setConfigFromNetwork interface in Java
chirpConnect.setConfigFromNetwork(new ConnectSetConfigListener() {

  @Override
  public void onSuccess() {
    //You can start and use the SDK from here
  }

  @Override
  public void onError(ChirpError chirpError) {
    Log.e(TAG, chirpError.getMessage());
  }
});

And here is the Kotlin version:

//setConfigFromNetwork lambda implementation in Kotlin
chirpConnect.setConfigFromNetwork { success: Boolean, setConfigError: ChirpError? ->
  if(!success) {
    Log.e(TAG, setConfigError!!.message)
    return@setConfigFromNetwork
  }
  //You can start and use the SDK from here
}

Android NDK

In our previous version of SDK we where using go-mobile to bind native code to the SDK. Go is actually great for binding native code however there are a couple of limitations which makes it unusable in some cases plus it adds a lot of dependencies which makes the final package a couple of times bigger.

One of the biggest issues with `go-mobile` is that it is impossible to have 2 different go packages in the same app which is a big problem for us and our 3rd party developers.  Usually, you can just embed all your native dependencies within the same package however this is impossible to do when you have to deal with an SDK which already compiled. Switching from go-mobile to Android NDK removes all this issues plus it makes the final package much smaller because we don't have Go dependencies anymore.

Before and after

Performance

Due to the fact that we are using Android NDK  to directly to embed native code to the SDK we have a performance improvements of about 50%. In the following image you can see the performance difference of the SDK's running on a google pixel device.

Chirp Android SDK v3.5.2 performance
Chirp Android SDK v3.6.0 performance

As you can see from the Android profiler the overall CPU usage when SDK is listening is around 5% on Android SDK v3.5.2 and around 3% on new version of SDK v3.6.0. At the same time the there are small peaks when CPU usage spikes, these are the moments when encoding/decoding is done and again, the difference is almost 50% better for new version of SDK.

In conclusion, the latest beta SDK release is faster, smaller and easier to use with Kotlin.  

If you want to give it a try you can download it from developers.chirp.io.  You can also find the getting started guide developers documentation page.