Building a Remote Communication App for Android using JNI and Kotlin

Ryan Burnsworth

Using JNI in Android is something that has always excited me. It opens up a new world of challenges and possibilities. In most cases I am working with Android using Kotlin or Java rarely using C++, but recently I wanted to know more about using JNI with Kotlin.

This article highlights the steps I took in order to integrate C++ code into an Android app that uses Kotlin. To start the project I decided on writing a simple TCP/IP socket client in C++ and call it’s functions using Kotlin.

We’ll start this post off by creating a new Android C++ project with Kotlin. Open Android Studios and choose Create a New Project. On the Choose Your Project screen scroll down and select Native C++.

Once the project is loaded in Android Studios, navigate to the cpp folder, right click and add a new C++ class, named klient.h/klient.cpp. Don’t worry, Android Studios will create these both for you.

We need to add klient.h and klient.cpp to our CMakeLists.txt file so they are included in the compilation of the C++ files.

Ok, now we are ready to write some yummy C++ code. We will build out our klient.h library by adding some basic functions to connect, send/receive data and disconnect from the target server.

Now we will add the function definitions in klient.cpp

The TCP/IP code is complete and now we can connect to a remote server, send data, receive data and close our connection. Before we can integrate with Kotlin we have to implement our JNI code into native-lib.cpp which will act as the intermediary between the C++ and Kotlin layers.

Once the init function is called successfully it will return a socket to the Kotlin side which can be stored and used to perform actions such as sending data, listening for incoming data or disconnecting the socket.

Before we go any further let’s add an internet permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

Now let’s add Kotlin Coroutines to handle the remote connection on a background thread. Add these lines to the app’s Build.gradle file

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'

Time to sprinkle in the Kotlin.

It is important to remember that we need a Companion Object to load the native library when the class is initialized.

companion object {
init {
System.loadLibrary("native-lib")
}
}

Then we will want to define the native methods we created in native-lib.cpp

private external fun init(hostname: String, port: Int): Int
private external fun sendData(sock: Int, data: String): Int
private external fun recvData(sock: Int): String
private external fun disconnect(sock: Int)

Now the native methods are ready to be called from Kotlin. Simply initiate the connection with init(“your-ip”, yourport) and store the socket that is returned. This socket can be used to send data, listen for incoming data or disconnect the connection.

To test this code you can use a Raspberry Pi or a remote Linux server. On the server/Pi side you can use netcat to open a listening port. For example,

nc -l -p 9090

You can browse the full project on GitHub here:

I hope this helped by giving a different example of how to use Kotlin and C++ together with JNI.

Helpful Links:

Ryan Burnsworth

Written by

Remote Software Engineer. Android Enthusiast.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade