This project demonstrates how to invoke C++ functions from Android's MainActivity
using Java Native Interface (JNI) and receive callbacks utilizing the Observer design pattern.
- JNI Integration: Facilitates seamless communication between Java and C++ layers in an Android application.
- Observer Pattern: Implements a callback mechanism to notify Java code of events occurring in the native C++ layer.
- Android Studio with NDK support
- Basic knowledge of Java and C++
-
Clone the Repository:
git clone https://github.com/youssefmyh/Android-JNI.git
This command will download the project files to your local machine.
-
Open the Project in Android Studio:
- Launch Android Studio.
- Select "Open an existing project" and navigate to the cloned repository's directory.
-
Build the Project:
- Ensure the Android NDK is installed and configured in your Android Studio setup.
- Click on "Build" > "Make Project" to compile the project.
-
Run the Application:
- Connect an Android device or start an emulator.
- Click on "Run" > "Run 'app'" to install and launch the application.
app/src/main/java/com/example/androidjni/
: Contains Java source code, includingMainActivity
which interacts with the native layer.app/src/main/cpp/
: Holds C++ source code and native method implementations.app/src/main/cpp/CMakeLists.txt
: Build script for compiling the native code.
- Java to C++:
MainActivity
declares native methods and loads the native library. When these methods are called, control is passed to the corresponding C++ functions. - C++ to Java (Callback): The C++ layer maintains references to Java callback interfaces. Upon certain events, the C++ code invokes these callbacks, effectively notifying the Java layer of the event.
Contributions are welcome! Feel free to fork this repository, make enhancements, and submit pull requests.
This project is licensed under the MIT License.
- Android NDK for enabling native code integration in Android apps.
- JNI Documentation for providing insights into Java and native code interoperability.
For more information, visit the GitHub repository.