Link Gradle to Your Native Library

To include your native library project as a Gradle build dependency, you need
to provide Gradle with the path to your CMake or ndk-build script file. When
you build your app, Gradle runs CMake or ndk-build, and packages shared
libraries with your APK. Gradle also uses the build script to know which files
to pull into your Android Studio project, so you can access them from the
Project window. If you don’t have a build script for your native
sources, you need to create a
CMake build script before you proceed.

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 CMakeLists.txt file
as your top-level CMake build script (which you then link Gradle to) and
add other CMake projects as
dependencies of that build script. Similarly, if you're using ndk-build, you
can include other Makefiles in your top-level
Android.mk 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.

Use the Android Studio UI

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.

From the drop-down menu, select either CMake or
ndk-build.

If you select CMake, use the field next to
Project Path to specify the CMakeLists.txt script
file for your external CMake project.

If you select ndk-build, use the field next to
Project Path to specify the Android.mk script file for
your external ndk-build project. Android Studio also includes the
Application.mk
file if it is located in the same directory as your
Android.mk file.

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 cmake or ndkBuild block:

Note: If you want to link Gradle to an existing ndk-build
project, use the ndkBuild block instead of the cmake block, and provide a relative path to your Android.mk file. Gradle also
includes the Application.mk file if it
is located in the same directory as your Android.mk file.

Specify optional configurations

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.

For example, if your CMake or ndk-build project defines multiple native
libraries and executables, you can use the targets property to build and package only a subset of those
artifacts for a given product flavor. The following code sample describes
some of the properties you can configure:

Include prebuilt native libraries

If you want Gradle to package prebuilt native libraries
with your APK,
modify the default source set configuration
to include the directory of your prebuilt .so files, as shown below. Keep in
mind, you don't need to do this to include artifacts of CMake build scripts that
you link to Gradle.

Specify ABIs

By default, Gradle builds your native library into separate .so
files for the 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 ndk.abiFilters 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 defaultConfig.externalNativeBuild.ndkBuild block). Gradle
builds those ABI configurations but only packages the ones you specify in the
defaultConfig.ndk block.

To further reduce the size of your APK, consider configuring
multiple APKs based on ABI—instead of creating one large APK with the all
versions of your native libraries, Gradle creates a separate APK for each ABI
you want to support and only packages the files each ABI needs. If you
configure multiple APKs per ABI without specifying the
abiFilters flag as shown in the code sample above, Gradle builds
all supported ABI versions of your native libraries, but only packages those
you specify in your multiple APK configuration. To avoid building versions of
your native libraries that you don't want, provide the same list of ABIs for
both the abiFilters flag and your per-ABI multiple APK
configuration.