Developing C/C++ Applications for Android: A Comprehensive Guide to NDK and JNI Integration

Nov 21, 2025 · Programming · 11 views · 7.8

Keywords: Android NDK | C/C++ Development | JNI Interface | CMake Build | Native Code Integration

Abstract: This technical paper provides an in-depth exploration of C/C++ application development on the Android platform, focusing on the core functionalities and implementation methods of the Android NDK (Native Development Kit). By analyzing Q&A data and official documentation, the article details how to integrate C/C++ code into Android projects, covering key technical aspects such as project configuration, CMake build system, and JNI interface design. Complete code examples and best practices are provided to help developers understand the complete workflow and considerations for Android native development.

Overview of C/C++ Development on Android Platform

Although the Android system is primarily designed around the Java language, official native development support has been available since SDK version 1.6. The Android NDK (Native Development Kit) is a specialized toolset for integrating C and C++ code into Android applications, enabling developers to directly use native code to build high-performance application components.

Core Features of Android NDK

The NDK provides a complete toolchain including compilers, debuggers, and platform libraries. Through the NDK, developers can:

Project Configuration and Build Systems

Integrating C/C++ code in Android Studio requires proper configuration of build systems. Android supports two main build tools: CMake and ndk-build.

CMake Configuration Example

The following is a typical CMakeLists.txt configuration file:

cmake_minimum_required(VERSION 3.10.2)
project("native-lib")

add_library(
    native-lib
    SHARED
    src/main/cpp/native-lib.cpp
)

find_library(
    log-lib
    log
)

target_link_libraries(
    native-lib
    ${log-lib}
)

Gradle Integration Configuration

Add CMake configuration in the module's build.gradle file:

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
            }
        }
    }
    
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

JNI Interface Design and Implementation

Java Native Interface (JNI) serves as the bridge connecting Java/Kotlin code with C/C++ code. Proper JNI interface design is crucial for successful native code integration.

Native Function Declaration

Declare native methods in Java class:

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    
    public native String stringFromJNI();
}

C++ Implementation Example

Corresponding C++ implementation code:

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapp_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

File Organization Structure

Standard Android project file structure should include:

Build and Debug Process

The complete build process includes:

  1. Writing C/C++ source code and placing it in the correct directory structure
  2. Configuring CMake or ndk-build build scripts
  3. Specifying build tools and configuration parameters in Gradle
  4. Compiling native libraries using Android Studio's build system
  5. Debugging native code through LLDB debugger

Performance Optimization and Best Practices

When developing with NDK, consider:

Migrating Existing Projects

For projects using the deprecated ndkCompile tool, migration to CMake or ndk-build is recommended. The migration process includes:

Conclusion

The Android NDK provides developers with powerful capabilities for using C/C++ code on the Android platform. Through proper project configuration, correct JNI interface design, and optimized build processes, developers can fully leverage the performance advantages of native code while maintaining good integration with the Android ecosystem. As the Android platform continues to evolve, the NDK toolchain is also continuously improving, providing better support for high-performance application development.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.