Tech

MobilePDF Software Development Kit (SDK) Kotlin Support

by Conor Smith | April 12, 2018

What is Kotlin?

Kotlin is a statically typed programming language for multiplatform applications. It runs on the Java virtual machine and also can be compiled to JavaScript source code or use the LLVM compiler infrastructure. Kotlin is designed to interoperate with Java code and is reliant on Java code from the existing Java Class Library, such as the collections framework. Kotlin is an official language on Android. Apps already built on Kotlin include Slack, Netflix, Evernote and Udacity.

How does Kotlin work with Foxit MobilePDF SDK?

MobilePDF SDK supports native application development for Android. In May 2017 Google announced Kotlin as an official language for Android. Because of this, it was necessary for Foxit to support Kotlin in our MobilePDF SDK for Android to ensure all our customers get the best option for their application development projects.

Kotlin is designed with Java interoperability, and existing Java code can be called from Kotlin in a natural way. This means that Kotlin can use MobilePDF SDK directly. Also, from Android Studio 3.0, Kotlin code can be converted from Java code.

Here is an example of what it looks like to call Foxit MobilePDF SDK in Kotlin, compared to the same code in Java:

Kotlin
fun loadPDFDoc(context: Context, path: String?, password: ByteArray?): PDFDoc? {
try {
val doc = PDFDoc(path)
if (doc == null) {
Toast.makeText(context, String.format("The path %s does not exist!", path), Toast.LENGTH_LONG).show()
return null
}
doc.load(password)
return doc
} catch (e: PDFException) {
Toast.makeText(context, "Load document error. " + e.message, Toast.LENGTH_LONG).show()
}
return null
}
Java
public static PDFDoc loadPDFDoc(Context context, String path, byte[] password) {
try {
PDFDoc doc = new PDFDoc(path);
if (doc == null) {
Toast.makeText(context, String.format("The path %s does not exist!", path), Toast.LENGTH_LONG).show();
return null;
}
doc.load(password);
return doc;
} catch (PDFException e) {
Toast.makeText(context, "Load document error. " + e.getMessage(), Toast.LENGTH_LONG).show();
}
return null;
}

What does the future hold?

Kotlin has a bright future as a first-class language for writing Android Apps. It is a new programming language that is concise while also being expressive. It contains safety features for nullability and immutability which make Android apps healthy and performant by default. Looking at all the big apps which are built with Kotlin, it seems that Kotlin is here for the long-haul.

 

Work with MobilePDF SDK and Kotlin

[button link=”/solutions/kotlin-support/” type=”big” color=”orange”]Discover More About Our Kotlin Support[/button]