CameraKit and Kotlin

Here at CameraKit we are always trying to keep up to date with modern Android development tools. In upcoming releases, we will be transitioning our codebase from Java into Kotlin. I’ll discuss Kotlin and how it can help take CameraKit to the next level.

A Bit About Kotlin

Kotlin is a programming language created by JetBrains in 2011, the makers of Android Studio. Kotlin aims to bring the convenience of modern language features to Android with clean, concise style. The standard library is small with little runtime overhead and relies heavily on compile-time inlining to increase performance.

In addition to being a great language to read and write, it’s very easy to migrate from Java to Kotlin. Kotlin fully supports interoperability with native code. That means that all the libraries you are familiar with in Java will work just the same in Kotlin. Interfacing with existing Java code does not require any special treatment like adapters.

Kotlin also has strong support from its creator JetBrains and Google, who made Kotlin an official language of Android. Kotlin is an easy transition from existing Java code and will likely be around for many years to come.

There’s lots great articles and resources for Kotlin, here are a few of my favorites: Kotlin Reference, Why you should totally switch to Kotlin, Why Kotlin is my next programming language.

Kotlin in CameraKit

Earlier this year, Android KTX was announced, a set of Kotlin extensions specifically for Android development. It continues on the same ideas that make Kotlin such a great language to begin with, clear concise code resulting in a simpler to understand project. Google and JetBrains are adding more coverage of the Android Framework all the time. With CameraKit, we aim to create our own extensions that will make the native camera APIs easier to use.

The first aspect of CameraKit that we’ve rewritten in Kotlin is the CameraSurfaceView and its rendering components such as the CameraSurfaceRenderer and TextureProgram. As you’ll see in the code snippets below, the Kotlin code is easy to read and digest. Kotlin is also still very performant as it compiles to Java bytecode to run on the JVM.

Here’s some sample code from our latest commit:

class CameraSurfaceView : GLSurfaceView {

internal val renderer: CameraSurfaceRenderer

constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

init {

renderer = CameraSurfaceRenderer(context)

keepScreenOn = true

fun setShader(shader: Int) {
renderer.shader = shader

The class is defined in a similar fashion to Java followed by the new inheritance syntax, which makes a lot of sense when you see how variables are defined.

Another huge benefit to Kotlin is its null safety. Kotlin distinguishes between non-null types and nullable types. Take a look at the following snippet.

private var surfaceTexture: SurfaceTexture? = null

The question mark signifies that the variable can be made null and in this case, is set right to null. Now when accessing this variable you must do one of two things.

if (surfaceTexture != null) {
return surfaceTexture!!
surfaceTexture?.run {...}

One must first check if the value of the object is null or use a safe call like in the second example which will evaluate to null instead of throwing a null pointer exception.

Feel free to dig into our code over on the GitHub page.


For so many reasons discussed above, migrating to Kotlin is an easy choice for us at CameraKit. Kotlin allows for digestable and concise code, JetBrains has created great editing tools and migration from Java won’t make you want to pull your hair out.

With Android KTX we will be able to add amazing features to CameraKit in the coming months. Stay tuned for more updates focused around integrating Kotlin into our platform!