Why use Kotlin on Pepper QiSDK?
At SoftBank Robotics, we use Kotlin to develop the QiSDK API for Pepper robots and most libraries and applications. In this article, we will introduce Kotlin and the KDoc, explain why we made that choice and how to make a smooth transition from Java to Kotlin.
What is Kotlin and KDoc?
The first version of Kotlin was released in 2011, very recently compared to Java in 1995 and C++ in 1983.
Its main characteristics are:
- Statically-typed language
- Compilation to Java bytecode
- Execution in a Java Virtual Machine (JVM)
Kotlin is more generic and not restrained to one only platform. It can run on any platform with a JVM. It can be used for backend, frontend in JS, as well as Android of course.
The language was created by the JetBrains company, founded in 2000 in Prague (Czech Republic). This company also created IntelliJ IDEA, a popular IDE for Java, on which Android Studio is based. Thus Kotlin has an excellent support in Android Studio like Java does.
Kotlin is officially supported by Google since I/O 2017 which ensures the language's longevity.
KDoc is the language used to document Kotlin code (the equivalent of Java's JavaDoc). By extension KDoc is the documentation released from Kotlin code.
In its essence, KDoc combines JavaDoc's syntax for block tags (extended to support Kotlin's specific constructs) and Markdown for inline markup.
Pepper QiSDK KDoc
Why do we use Kotlin at SoftBank Robotics?
Kotlin has become a standard for Android app development. We tend to use Kotlin for real apps as Kotlin is more effective and also recommended by Google for the development of Android apps. Kotlin is used by more than 50% of Android app developers.
At SoftBank Robotics we decided to develop the QiSDK API for Pepper robots and most libraries and applications in Kotlin for several reasons.
The main reasons are the following:
- Compatibility with existing Kotlin libraries
- Shorter and safer code
SoftBank Robotics Labs Kotlin libraries
Many SBR libraries are developed in Kotlin. You can access these Kotlin based libraries from our Github repository.
Pepper extras is one of these libraries and is interesting to focus on. Pepper extras is a collection of Kotlin utilities to create apps easily using the Pepper QiSDK: StubbornGoTo, coroutines, various geometry functions for frames and transforms, a helper for instrumented tests, etc.
From Java to Kotlin in Android Studio
The lessons published on the Developer Center use the Kotlin language.
As you have seen you can choose the default language (Java or Kotlin) of a project when you create it.
You may be familiar with Java and not Kotlin. Kotlin was integrated into Android Studio which provides some useful helper features to ease migrating from Java to Kotlin. The first is a conversion feature and the second is a "copy-paste Java to Kotlin" feature.
Conversion of a Java file to Kotlin
If you have a Java file you can convert it to Kotlin through the menu 'Code > Convert Java File to Kotlin' or with the shortcut 'Ctrl+Alt+Maj+K'. There is no rollback.
Copy pasting of Java code in a Kotlin file
You may want to use some Java examples or code from a previous project that is coded in Java for your new Kotlin project. This is not a big deal. It is extremely simple to transform your pieces of code from Java to Kotlin.
It is possible to copy-paste Java code directly to a Kotlin file. When you copy Java code to a Kotlin project Android Studio proposes to convert this Java code to Kotlin, even a single function. That is a pretty nice feature you should know about. Note that transforming Kotlin to Java is not possible.
Be aware that if this code contains references to layout elements, Android Studio might automatically insert the "import android.R" statement at the top of the file which might be wrong. This will display errors on references to your project resources. Delete the import line and your robot app resources will become visible again in your package.
// Create a phrase. Phrase phrase = new Phrase("Hello world"); // Build the action. Say say = SayBuilder.with(qiContext) .withPhrase(phrase) .build(); // Run the action synchronously. say.run();
You will get these Kotlin lines:
// Create a phrase. val phrase: Phrase = Phrase("Hello") // Build the action. val say: Say = SayBuilder.with(qiContext) .withPhrase(phrase) .build() // Run the action synchronously. say.run()
Pretty simple, isn’t it?
Can I still code in Java?
Yes, you can!
Co-existence of Java and Kotlin code in the same project is possible. Kotlin works well with the Java programming language as both are strongly compatible and completely interoperable.
Many libraries, tools and plenty of frameworks are coded in Java, you can just add these to your Kotlin projects with no need to convert the entire project to Java.
You can therefore use Kotlin alongside Java without needing to convert the entire project to Kotlin.
This is a major advantage to developers since you do not really have to learn a completely new language. Anyone who knows Java will be familiar with Kotlin and be able to code in Kotlin.
Enjoy Kotlin coding!