# HelloAndroidModuleInCpp **Repository Path**: weekend/hello_andorid_module_in_cpp ## Basic Information - **Project Name**: HelloAndroidModuleInCpp - **Description**: using c++ in android - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-14 - **Last Updated**: 2025-04-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Using c++ in Android 1. Create a project with Native c++ template 2. Create a new Module with type of Android Native Library ## Using aar 1. Export the Android Native Library to aar ```bash # 1. new Android Native Library Module # 2. generate aar gradle nativelib:assemble ``` 2. Import the aar to your project ```java // 1. copy the aar to the libs folder(create it if not exists) app/libs/nativelib-release.aar // 2. import the aar in app/build.gradle dependencies { implementation fileTree(include: ['*.aar'], dir: 'libs') } // 3. invoke functions in aar // app/src/main/java/com/example/hellondk/MainActivity.java import com.example.nativelib.NativeLib; binding.sampleText.append(String.format("\n%s", NativeLib.stringFromJNI())); ``` ## Pack third-party libs info arr 1. Copy the libs and headers into your project ``` nativelib/src/main/jniLibs/arm64-v8a/libmylib.so | Bin 0 -> 9192 bytes nativelib/src/main/jniLibs/x86_64/libmylib.so | Bin 0 -> 8296 bytes nativelib/src/main/third_party/mylib/include/mylib.h | 14 ++++++++++++++ ``` 2. Set the abi and load libs in build.gradle ```gradle android { defaultConfig { ndk { abiFilters 'arm64-v8a', 'x86_64' } } sourceSets.main { jniLibs.srcDirs = ['src/main/jniLibs'] } } ``` 3. Link mylib to your android native lib in it's CMakeLists.txt ```cmake # thirdparty headers and libs include_directories(src/main/third_party/mylib/include) add_library(mylib SHARED IMPORTED) set_target_properties(mylib PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libmylib.so") # link mylib target_link_libraries(${CMAKE_PROJECT_NAME} # List libraries link to the target library android log mylib) ``` 4. Export mylib through NativeLib ```cpp // NativeLib.cpp #include "../third_party/mylib/include/mylib.h" extern "C" JNIEXPORT jint JNICALL Java_com_example_nativelib_NativeLib_addNumbers( JNIEnv* env, jobject /* this */, jint a, jint b) { return add(a, b); } ``` ```java // NativeLib.java public class NativeLib { // ... public native static int addNumbers(int a, int b); } ``` 5. Gen the aar of NativeLib and copy to your project and Test ```java binding.sampleText.append("\nSum from nativelib: " + NativeLib.addNumbers(2, 3)); ``` ![](./hello_aar.png) ## To build the mylib, please refer to this: [CMake cross-compile a lib with NDK](https://gitee.com/weekend/hello_cmake_ndklib) ## ref Create an Android library https://developer.android.com/studio/projects/android-library Add C and C++ code to your project https://developer.android.com/studio/projects/add-native-code Prepare your library for release https://developer.android.com/build/publish-library/prep-lib-release