Android C++ Example

5 min read Jun 28, 2024
Android C++ Example

Android C++ Example: A Simple "Hello World" App

This article will guide you through creating a basic Android application using C++ with the Android Native Development Kit (NDK). This example will display "Hello World" on the screen, demonstrating the fundamental process of developing Android apps with C++.

Prerequisites

  • Android Studio: Download and install the latest version of Android Studio from the official website.
  • Android NDK: Android Studio will prompt you to install the NDK during project setup. If not, you can download it separately from the official website.

Project Setup

  1. Create a new project:

    • Open Android Studio and select "Start a new Android Studio project."
    • Choose "Empty Compose Activity" as the template.
    • Name your project (e.g., "HelloWorldCpp").
    • Select your minimum API level.
    • Click "Finish".
  2. Enable C++ Support:

    • Go to "File" -> "Project Structure".
    • Navigate to "Modules" -> YourProjectName (e.g., "HelloWorldCpp").
    • Click on "Properties" in the right pane.
    • Check the box for "C/C++ Support".
    • Click "OK" to save changes.

Code Implementation

  1. Create a C++ Native Code File:

    • Inside the cpp directory of your project, create a new file named native-lib.cpp.
    • Add the following C++ code to native-lib.cpp:
    #include 
    #include 
    
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_helloworldcpp_MainActivity_stringFromJNI(
            JNIEnv *env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    

    Explanation:

    • This code defines a function stringFromJNI that is called from your Java code.
    • JNIEXPORT and JNICALL are macros required for JNI (Java Native Interface) functions.
    • jstring is the JNI data type representing a Java string.
    • The function returns a string "Hello from C++" back to the Java code.
  2. Modify Java Code:

    • Open the MainActivity.kt file.
    • Add the following code inside the onCreate method:
    // ... existing code ...
    
    // Load the native library
    System.loadLibrary("native-lib")
    
    // Access the native function
    val text = stringFromJNI()
    
    // Set the text view
    binding.sampleText.text = text
    
    // ... rest of the code ...
    

    Explanation:

    • System.loadLibrary("native-lib") loads the native library we created (native-lib.cpp).
    • stringFromJNI() calls the native C++ function defined in native-lib.cpp.
    • The returned value is stored in the text variable.
    • Finally, the text is set to the sampleText TextView.

Build and Run

  1. Build the project: Click the "Run" button (green play icon) in Android Studio.
  2. Deploy the app: Select a device or emulator to run the app.
  3. View the result: Once the app launches, you should see "Hello from C++" displayed on the screen.

Conclusion

This simple example demonstrates how to use C++ code in your Android applications using the NDK. You can build upon this basic structure to create more complex and feature-rich Android apps using the power of C++. Remember to learn more about JNI, NDK, and C++ concepts to develop robust and efficient Android apps.

Latest Posts


Featured Posts