Parse-SDK Android and Kotlin

I have some projects running with Parse Server, Game On’s backend. Game On is a game developer’s community http://gameon.lat. Game On’s iOS App is working fine with Swift and Parse. I’m an iOS Developer, but I know about Android development with Android Studio. I’m really excited about Kotlin.

kotlinlogo.png

When creating Game On’s Android App, I really wanted it to be coded with Kotlin. When searching about Parse SDK and Kotlin, there is no much help out there. (https://github.com/gameonhack/gameon-android)

So if you are reading this, you maybe you are having the same problem I had or you are totally curios about it. Anyways let me show how to have a Android Studio Project with Kotlin and the Parse SDK.

1. Create a Android Studio Project

I’m using Android Studio 3.0 (it’s a beta for me) https://developer.android.com/studio/preview/index.html

Open Android studio. Create a new project and make sure to mark: Include Kotlin support

For this example I selected the Basic Activity template.

2. Installing Parse-SDK-Android

We are going to follow Parse’s official getting started guide. http://docs.parseplatform.org/android/guide/#getting-started

Open the build.gradle (Module App) file and add the dependency.

dependencies {
 ...
 compile 'com.parse:parse-android:1.15.7'
}

Sync your Gradle file, and you should be ready to use Parse-SDK :).

In the Java folder create a new Kotlin Class and name it App.

We want this class to have the full lifecycle of our application. It’s the place where we should configure and initialize our Parse-SDK.

package co.estamp.parsekotlin

import android.app.Application
import com.parse.Parse

class App : Application() {

 override fun onCreate() {
super.onCreate()

Parse.initialize(Parse.Configuration.Builder(this)
 .applicationId("")
 .clientKey("")
 .server("")
 .build()
 )

 }

}

Now we should register our App class with the Manifest:

<application
android:name=".App"
...>
...
</application>

You are now ready to use the Parse-SDK all over the project. (๑˃̵ᴗ˂̵)و

3. ParseObjects

ParseObjects in Kotlin are similar to Java. You can create nonmutable variables to create ParseObjects.

val gameScore = ParseObject("GameScore")
gameScore.put("score", 1337)
gameScore.put("playerName", "Sean Plott")
gameScore.put("cheatMode", false)
gameScore.saveInBackground {
 Log.d("DEBUG", "Game Score saved, id: " + gameScore.objectId )
}

Notice the saveInBackground callback, there is no need to manually create a SaveCallback object. This is possible due to the Java Interoperability that Kotlin supports (https://kotlinlang.org/docs/reference/java-interop.html).

The same happens with the objectId property. Remember in Java, getCreatedAt(), getUpdateAt(), getObjectId(). The idea of Kotlin is that you don’t need to call the functions get/set, instead you need to call it’s properties.

4. Subclasses

One thing I love about Parse is being able to Subclass the Parse Objects. http://docs.parseplatform.org/android/guide/#subclasses

The following code is the Parse example in Kotlin:

import com.parse.ParseClassName
import com.parse.ParseObject

@ParseClassName("Armor")
class Armor: ParseObject() {
 fun getDisplayName(): String {
return getString("displayName")
 }
 fun setDisplayName(value: String) {
put("displayName", value)
 }
}

You also need to register your Parse Object Subclass on the App class (the file we created before). In Kotlin to get Java class, we can use ::class.java. https://kotlinlang.org/docs/reference/java-interop.html#getclass

import android.app.Application
import com.parse.Parse

class App : Application() {

override fun onCreate() {
super.onCreate()
ParseObject.registerSubclass(Armor::class.java)
Parse.initialize(Parse.Configuration.Builder(this)
 .applicationId("")
 .clientKey("")
 .server("")
 .build()
)

 }

}

Seems to be okay? Yeah… but I really don't like using the set and get functions. (・_・ヾ

I came up with a simple solution using getters and setters properties. https://kotlinlang.org/docs/reference/properties.html#getters-and-setters

import com.parse.ParseClassName
import com.parse.ParseObject

@ParseClassName("Armor")
class Armor: ParseObject() {

 var displayName : String?
get() = getString("displayName")
set(value) {
put("displayName", value)
}
}

Now you can call an object’s property without writing getters or setters functions. In the following example I query all the Armors objects and log the display name.

val query = ParseQuery.getQuery(Armor::class.java)
query.findInBackground { objects, e ->
 if (e == null) {
for (armor in objects) {
Log.d("DEBUG", armor.displayName)
}
 } else {
Log.d("score", "Error: " + e!!.message)
 }
}

I’m really glad with the results so far. (/^▽^)/

Well that’s all! I really hope you found this useful. We learn how to configure and install Parse-SDK with Android Studio 3.0. We also learn how to query ParseObjects and to efficiently subclass them.

I do believe Kotlin is a great language with a lot of potential. Let me any if you have any comments or questions.