Google SmartLock and using RxJava/Kotlin to escape the callback hell of the GoogleApiClient

David Langley
HubSpot Product
Published in
2 min readMar 14, 2017

Google provides some pretty awesome services. Take for example it’s SmartLock Credentials API. Creating an app’s login that securely handles credentials, provides a frictionless experience and is flexible enough to support multiple types of login is not a trivial task but SmartLock really delivers on a lot of these fronts. We recently used it in our our new HubSpot CRM beta app for Android and were really happy with the results.

Google SmartLock’s frictionless flow

Engineers no longer have to come up with “creative” solutions to storing passwords client-side using cryptography in ways that really just slow attackers down. They can now offload this responsibility to Google via SmartLock. The user experience is also really nice. Users should only ever really have to enter their password at most once, and possibly not at all (credentials can be shared with your web application if the user has stored the password on the web using Chrome). So you may not even have to show them a login screen, you can just drop them right in to your app. Music to your product manager’s ears, right?

I have always found Googles API’s to be very easy to follow and with plenty of examples. However managing the lifecycle of their API client and all of the callbacks that ensue can result in a lot of imperative code that can be cumbersome to follow. Looking at the flow diagram above you can see how there are a bunch of cases you need to cover and plenty of asynchronous processing. Just check out Google’s own sample code for SmartLock Credential API’s that covers some of this picture.

Wouldn’t it be great if we could just get an API client, link together a few API calls and destroy the client gracefully without all of these helper methods and callbacks?

I took a stab recently at creating some Reactive Extensions to help straighten some of this out and was pretty happy with the result. Check out the gist below (this is not really production ready, just a proof of concept) for an example of how you can use RxJava’s stream processing and Kotlin’s class extensions to boil all this code down into something much more readable and easy to follow. These extensions can be used with any Google APIs that use the standard Google API client.

If you are interested in HubSpot’s awesome product team you can find more details here.

--

--