Skip to main content
The Android Gradle Plugin seamlessly integrates ndk-build and CMake into your Android app build process. This allows you to build native libraries alongside your Java/Kotlin code with a single ./gradlew command.

Configuration overview

Gradle integration happens in your module’s build.gradle or build.gradle.kts file through the externalNativeBuild block:
build.gradle

NDK version

Specify the NDK version to ensure consistent builds across development machines:
build.gradle
If ndkVersion is not specified, Gradle uses the highest installed NDK version. Always specify the version for reproducible builds.

Finding your NDK version

Build configuration

Basic externalNativeBuild

build.gradle

ABI filters

Control which ABIs to build:
build.gradle
Google Play requires 64-bit support. Always include arm64-v8a and x86_64 if you support x86.

Build variants

Configure builds differently for debug and release:
build.gradle

Advanced configuration

Multiple ABIs with product flavors

Create separate APKs for different ABIs:
build.gradle

CMake arguments

Pass arguments to CMake:
build.gradle

ndk-build arguments

Pass arguments to ndk-build:
build.gradle

Targets configuration

Build specific CMake targets:
build.gradle

Packaging native libraries

Automatic packaging

By default, Gradle packages all built .so files into your APK/AAB:
build.gradle

JNI libs directories

Include additional prebuilt libraries:
build.gradle
Place prebuilt libraries in ABI-specific directories:

Excluding libraries

Exclude specific libraries from packaging:
build.gradle

Build performance

Parallel builds

Enable parallel CMake builds:
gradle.properties

Build caching

Enable build caching for faster incremental builds:
gradle.properties

Selective ABI builds

For development, build only one ABI:
build.gradle
Add local.properties to your .gitignore to keep per-developer settings local.

Debugging build issues

Verbose output

Enable verbose build output:
build.gradle

Build output location

Find generated build files:

Clean native builds

Common issues

NDK not found

Solution: Install the NDK through Android Studio SDK Manager or specify the path:
local.properties

Version conflicts

Solution: Ensure the CMake version in build.gradle matches the installed version:

Missing libraries at runtime

Solutions:
  • Ensure the library is built for the device’s ABI
  • Check that abiFilters includes the device’s ABI
  • Verify the library name matches what you load with System.loadLibrary()

Build cache issues

Solution: Clean and rebuild:

Best practices

1

Pin your NDK version

Always specify ndkVersion in build.gradle for reproducible builds across team members and CI.
2

Use specific ABI filters for development

Build only one ABI during development for faster iteration, but build all ABIs for release.
3

Separate debug and release configurations

Use different compiler flags and optimizations for debug vs release builds.
4

Enable build caching

Configure Gradle build cache to speed up incremental builds.
5

Version control your configuration

Commit build.gradle with NDK settings. Keep local.properties out of version control.

Next steps

CMake

Learn CMake configuration for NDK

ndk-build

Learn ndk-build configuration

ABIs

Understand Android binary interfaces

Debugging

Debug native code in your app