Android 12 Developer Preview is here! Try it out, and give us your feedback!

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. Since C/C++ dependencies are distributed as AARs, the following links about generic AARs may be helpful:

  • Creating an Android Library for generic AAR documentation and how to integrate it into your project, especially when you want to use the AAR as a local C/C++ dependency.
  • Add build dependencies for information on adding dependencies to your build.gradle file, especially for the remote dependencies.

This document focuses on how to configure your native build system and assumes you've already added a C/C++ dependency AAR into your project's Gradle build environment.

Native dependencies in AARs

AAR dependencies of your Gradle modules can expose native libraries for use by your application. Inside the AAR, the prefab directory contains a Prefab package, which includes the headers and libraries of the native dependency.

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 C/C++ library name, but this is not required. Consult the dependency's documentation to determine what names it uses.

Build system configuration

The prefab feature must be enabled for your Android Gradle module.

To do so, add the following to your project's file:


Each dependency exposes an file to your build. These are imported with the import-module command. This command searches for files with the given path within the build's import directories (as configured by import-add-path) and exposes the modules it defines to be used in your build. For example, if your application defines and it uses cURL, your file should include the following:

include $(CLEAR_VARS)
LOCAL_MODULE := libapp
LOCAL_SRC_FILES := app.cpp

# If you don't need your project to build with NDKs older than r21, you can omit
# this block.
ifneq ($(call ndk-major-at-least,21),true)
    $(call import-add-path,$(NDK_GRADLE_INJECTED_IMPORT_PATH))

$(call import-module,prefab/curl)

Note that the explicit import-add-path is only required when using NDKs prior to r21. As of r21 NDK_GRADLE_INJECTED_IMPORT_PATH will automatically be added to the import paths.

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