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
-
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".
-
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
-
Create a C++ Native Code File:
- Inside the
cpp
directory of your project, create a new file namednative-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
andJNICALL
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.
- Inside the
-
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 innative-lib.cpp
.- The returned value is stored in the
text
variable. - Finally, the text is set to the
sampleText
TextView.
- Open the
Build and Run
- Build the project: Click the "Run" button (green play icon) in Android Studio.
- Deploy the app: Select a device or emulator to run the app.
- 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.