We’re going to call a C++ function from Kotlin/Java and display the result in a TextView. This will introduce you to JNI (Java Native Interface), which is the bridge between Java/Kotlin and native C/C++ code.

π What You’ll Learn
- β How to set up NDK in Android Studio
- β How to write a C++ function
- β How to call native C++ code from Kotlin/Java using JNI
- β How to pass data between Java and C++
π Step 1: Create a New Android Project
- Open Android Studio.
- Create a new project using Empty Activity.
- Name it: NDKHelloWorld
- Package:
com.example.ndkhelloworld
- Language: Kotlin
- Minimum SDK: API 21 or higher.
- Click Finish and wait for Gradle sync.
π Step 2: Add NDK Support
1. Enable C++ support by adding this to your gradle.properties file:
android.useAndroidX=true
android.enableJetifier=true
2. Open app/build.gradle, and inside the android block, add:
externalNativeBuild {
cmake {
path("src/main/cpp/CMakeLists.txt")
}
}
3. Make sure the NDK is installed:
- Go to File β Preferences β Android SDK β SDK Tools.
- Enable NDK (Side by Side) and CMake.
- Click Apply & OK.
π Step 3: Create Native C++ Code
1. Inside app/src/main, create a new folder called cpp.
2. Inside the cpp folder, create a new file called native-lib.cpp.
3. Add the following code:
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndkhelloworld_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++!";
return env->NewStringUTF(hello.c_str());
}
π What This Code Does
- extern “C” β Prevents C++ name mangling so JNI can find our function.
- JNIEXPORT jstring JNICALL β Declares a function returning a Java String.
- JNIEnv* env β Pointer to the Java environment (used for JNI calls).
- NewStringUTF() β Converts a C++ string into a Java String.
π Step 4: Create CMakeLists.txt
1. Inside app/src/main/cpp/, create a new file called CMakeLists.txt.
2. Add this content:
cmake_minimum_required(VERSION 3.10.2)
project("ndkhelloworld")
add_library(native-lib SHARED native-lib.cpp)
find_library(log-lib log)
target_link_libraries(native-lib ${log-lib})
π What This Code Does
β’ add_library(native-lib SHARED native-lib.cpp) β Builds native-lib.cpp as a shared library.
β’ find_library(log-lib log) β Links the Android log library (not used in this example but useful later).
β’ target_link_libraries(native-lib ${log-lib}) β Links our library to Android’s logging system.
π Step 5: Load the Native Library in Kotlin
1. Open MainActivity.kt.
2. Add the following code:
package com.example.ndkhelloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
companion object {
// Load native library
init {
System.loadLibrary("native-lib")
}
}
external fun stringFromJNI(): String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Set text from native function
// Ensure your textView has an ID called `textView1` in activity_main.xml
findViewById<TextView>(R.id.textView1).apply {
text = stringFromJNI()
}
}
}
π What This Code Does
- System.loadLibrary(“native-lib”) β Loads our C++ shared library.
- external fun stringFromJNI(): String β Declares a native function (implemented in C++).
- textView.text = stringFromJNI() β Calls the C++ function and displays the result.
π Step 7: Build & Run
1. Sync the project: Click File β Sync Project with Gradle Files.
2. Run the app on a real device or emulator.
3. You should see:
Hello from C++!
π Congratulations! You’ve successfully called C++ code from Kotlin using the NDK! π
π Bonus Challenges
β Challenge 1: Modify stringFromJNI() to return “Hello from C++ and Kotlin!”.
β Challenge 2: Pass a string from Kotlin to C++, modify it in native code, and return it.
β Challenge 3: Call a C++ function that performs addition and return the result to Kotlin
Source Code & Solutions
The source code for this recipe can be found at https://github.com/advaitsaravade/Android-NDK-1-Hello-JNI. The source contains the answers to the Bonus Challenges above as well!
π Whatβs Next?
Using the NDK unlocks major performance gains, this is why it’s used extensively in the gaming niche. In the next blog post in this series, let’s build an app that demonstrates the performance gains you can unlock by using native code via the Android NDK vs good ol’ Kotlin. We all love a fast, snappy app, and NDK is the ultimate boss battle.
Let’s dive in to the next recipe: Building a Fast Math app for Performance Gains with Android NDK
Happy coding!