Google is committed to advancing racial equity for Black communities. See how.

Using native dependencies

Starting with Android Gradle Plugin 4.0, C/C++ dependencies can be imported from AARs linked in your build.gradle file. Gradle will automatically make these available to the native build system, but your build system must be configured to make use of the imported libraries and headers.

For information on adding dependencies in build.gradle, see Add build dependencies.

Native depdendencies in AARs

AAR dependencies of your Gradle modules can expose native libraries for use by your application. Libraries are exported using the Prefab package format.

Each dependency can expose at most one Prefab package, which comprises one or more modules. A Prefab module is a single library, which could be either a shared, static, or header only library.

The package and module names need to be known to make use of the libraries. By convention the package name will match the Maven artifact name and the module name will match the library name, but this is not required. Consult the dependency's documentation to determine what names it uses.

Build system configuration

Dependencies imported from an AAR are exposed to CMake via CMAKE_FIND_ROOT_PATH. This value will be set automatically by Gradle when CMake is invoked, so if your build modifies this variable be sure to append rather than assign to it.

Each dependency exposes a config-file package to your build. These are imported with the find_package command. This command searches for config-file packages matching the given package name and version and exposes the targets it defines to be used in your build. For example, if your application defines libapp.so and it uses cURL, your CMakeLists.txt should include the following:

add_library(app SHARED app.cpp)

# Add these two lines.
find_package(curl REQUIRED CONFIG)
target_link_libraries(app curl::curl)

app.cpp is now able to #include "curl/curl.h", libapp.so will be automatically linked against libcurl.so when building, and libcurl.so will be included in the APK.