Quick start guide
This tutorial guides you through creating your first Android application with native C++ code. You’ll build a simple “Hello from C++” app that demonstrates how Java/Kotlin code interacts with native code through JNI (Java Native Interface).Prerequisites
Before starting, ensure you have:- Android Studio installed with NDK and CMake
- Basic knowledge of Android app development
- Basic C++ knowledge (helpful but not required)
Create your first native app
Create a new project with native support
Open Android Studio and create a new project:
- Click File > New > New Project
- Select Native C++ template
- Click Next
- Configure your project:
- Name: HelloNDK
- Package name: com.example.hellondk
- Language: Kotlin (or Java)
- Minimum SDK: API 21 or higher
- Click Next
- Choose C++ Standard: C++14 or higher
- Click Finish
Explore the project structure
Your project now contains both Java/Kotlin and C++ code:Key files:
native-lib.cpp: Your C++ source codeCMakeLists.txt: CMake build configurationMainActivity.kt/java: Loads and calls native code
Examine the native code
Open This function:
app/src/main/cpp/native-lib.cpp. You’ll see a JNI function:- Is called from Java/Kotlin code
- Returns a string created in C++
- Uses JNI to convert C++ string to Java string
The function name follows JNI naming convention:
Java_<package>_<class>_<method>Examine the CMake configuration
Open This configuration:
app/src/main/cpp/CMakeLists.txt:- Creates a shared library named
native-lib - Compiles
native-lib.cpp - Links the Android log library
Examine the Kotlin/Java code
Open The code:
MainActivity.kt (or MainActivity.java):- Loads the native library with
System.loadLibrary() - Declares the native method with
external(Kotlin) ornative(Java) - Calls the C++ function like any other method
Build and run the app
- Connect an Android device or start an emulator
- Click Run (green play button) or press Shift+F10
- Wait for the build to complete
- The app displays “Hello from C++” on the screen
The first build may take longer as CMake compiles the native code for multiple architectures (armeabi-v7a, arm64-v8a, x86, x86_64).
Customize your native code
Now let’s modify the native code to perform a simple calculation.Add a new native function
In This function:
native-lib.cpp, add a new function that adds two numbers:- Takes two integers as parameters
- Returns their sum
- Uses
jint(JNI integer type)
Add logging from native code
Logging is essential for debugging native code. Let’s add Android logging support.Understanding JNI types
When working with JNI, you need to understand type mappings between Java and C++:| Java Type | JNI Type | C++ Type |
|---|---|---|
| boolean | jboolean | uint8_t |
| byte | jbyte | int8_t |
| char | jchar | uint16_t |
| short | jshort | int16_t |
| int | jint | int32_t |
| long | jlong | int64_t |
| float | jfloat | float |
| double | jdouble | double |
| Object | jobject | - |
| String | jstring | - |
Build system configuration
Thebuild.gradle file contains NDK configuration:
Building for fewer ABIs reduces APK size but limits device compatibility. Most production apps target
armeabi-v7a and arm64-v8a.Debugging native code
Android Studio provides full debugging support for native code:Common issues and solutions
UnsatisfiedLinkError
If you seeUnsatisfiedLinkError: No implementation found:
- Verify the native function name matches the JNI naming convention
- Ensure
System.loadLibrary()is called before the native method - Check that CMakeLists.txt includes your .cpp file
Build errors
If the native build fails:- Check Build tab for CMake error messages
- Ensure NDK and CMake are installed via SDK Manager
- Verify CMake version in CMakeLists.txt matches installed version
Missing symbols
If you get linker errors about missing symbols:- Add required libraries to
target_link_librariesin CMakeLists.txt - For Android APIs, use
find_library()to locate system libraries
Next steps
Congratulations! You’ve built your first native Android application. Here’s what to explore next:Build systems
Learn advanced CMake and ndk-build configurations
JNI guide
Deep dive into Java Native Interface programming
Performance
Optimize your native code for maximum performance
Debugging
Master native debugging and crash analysis
Additional resources
- NDK API Reference - Complete native API documentation
- NDK Guides - Comprehensive development guides
- Android NDK Google Group - Community support
- GitHub Issues - Report bugs and request features