Link Gradle to your native library
Each module in your Android project can link to only one CMake or ndk-build script file. So, for example, if you want to build and package outputs from multiple CMake projects, you need to use one file as your top-level CMake build script (which you then link Gradle to) and as dependencies of that build script. Similarly, if you're using ndk-build, you can include other Makefiles in your top-level script file.
Once you link Gradle to a native project, Android Studio updates the Project pane to show your source files and native libraries in the cpp group, and your external build scripts in the External Build Files group.
Note: When making changes to the Gradle configuration, make sure to apply your changes by clicking Sync Project in the toolbar. Additionally, when making changes to your CMake or ndk-build script file after you have already linked it to Gradle, you should sync Android Studio with your changes by selecting Build > Refresh Linked C++ Projects from the menu bar.
You can link Gradle to an external CMake or ndk-build project using the Android Studio UI:
- Open the Project pane from the left side of the IDE and select the Android view.
- Right-click on the module you would like to link to your native library, such as the app module, and select Link C++ Project with Gradle from the menu. You should see a dialog similar to the one shown in figure 4.
Click OK.
Manually configure Gradle
To manually configure Gradle to link to your native library, you need to add the externalNativeBuild
block to your module-level build.gradle
file and configure it with either the or ndkBuild
block:
Note: If you want to link Gradle to an existing ndk-build project, use the block instead of the block, and provide a relative path to your file. Gradle also includes the Application.mk
file if it is located in the same directory as your file.
You can specify optional arguments and flags for CMake or ndk-build by configuring another externalNativeBuild
block within the defaultConfig
block of your module-level build.gradle
file. Similar to other properties in the defaultConfig
block, you can override these properties for each product flavor in your build configuration.
To learn more about configuring product flavors and build variants, go to . For a list of variables you can configure for CMake with the arguments
property, see Using CMake Variables.
If you want Gradle to package prebuilt native librarieswith your APK,to include the directory of your prebuilt .so
files, as shown below. Keep inmind, you don't need to do this to include artifacts of CMake build scripts thatyou link to Gradle.
By default, Gradle builds your native library into separate files for the Application Binary Interfaces (ABIs) the NDK supports and packages them all into your APK. If you want Gradle to build and package only certain ABI configurations of your native libraries, you can specify them with the flag in your module-level build.gradle
file, as shown below:
In most cases, you only need to specify abiFilters
in the ndk
block, as shown above, because it tells Gradle to both build and package those versions of your native libraries. However, if you want to control what Gradle should build, independently of what you want it to package into your APK, configure another abiFilters
flag in the defaultConfig.externalNativeBuild.cmake
block (or block). Gradle builds those ABI configurations but only packages the ones you specify in the defaultConfig.ndk
block.