NdkBuildFlags

@Incubating
public interface NdkBuildFlags


DSL object for per-variant ndk-build options, such as ndk-build arguments and compiler flags.

To learn more about the ndk-build toolchain, read the official NDK documentation about Building Your Project.

Summary

Public methods

abstract void
abiFilters(@NonNull String abiFilters)

Specifies the Application Binary Interfaces (ABI) that Gradle should build outputs for.

abstract void
arguments(@NonNull String arguments)

Specifies arguments for ndk-build.

abstract void

Specifies flags for the C compiler.

abstract void
cppFlags(@NonNull String cppFlags)

Specifies flags for the C++ compiler.

abstract @NonNull Set<@NonNull String>

Specifies the Application Binary Interfaces (ABI) that Gradle should build outputs for.

abstract @NonNull List<@NonNull String>

Specifies arguments for ndk-build.

abstract @NonNull List<@NonNull String>

Specifies flags for the C compiler.

abstract @NonNull List<@NonNull String>

Specifies flags for the C++ compiler.

abstract @NonNull Set<@NonNull String>

Specifies the library and executable targets from your ndk-build project that Gradle should build.

abstract void

Specifies the library and executable targets from your ndk-build project that Gradle should build.

Public methods

abiFilters

abstract void abiFilters(@NonNull String abiFilters)

Specifies the Application Binary Interfaces (ABI) that Gradle should build outputs for. The ABIs that Gradle packages into your APK are determined by android.defaultConfig.ndk.abiFilter

In most cases, you need to specify ABIs using only android.defaultConfig.ndk.abiFilter, because it tells Gradle which ABIs to both build and package into your APK. However, if you want to control what Gradle should build, independently of what you want it to package into your APK, configure this property with the ABIs you want Gradle to build.

To further reduce the size of your APK, consider configuring multiple APKs based on ABI—instead of creating one large APK with 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.

By default, this property is null.

since 2.2.0

arguments

abstract void arguments(@NonNull String arguments)

Specifies arguments for ndk-build.

The properties you can configure are the same as those available in your Android.mk and Application.mk scripts. The following sample specifies the Application.mk for the ndk-build project:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor you configure.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Passes an optional argument to ndk-build.
arguments "NDK_MODULE_PATH+=../../third_party/modules"
}
}
}
}

By default, this property is null.

since 2.2.0

cFlags

abstract void cFlags(@NonNull String cFlags)

Specifies flags for the C compiler.

The following sample enables format macro constants:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Sets an optional flag for the C compiler.
cFlags "-D__STDC_FORMAT_MACROS"
}
}
}
}

By default, this property is null.

since 2.2.0

cppFlags

abstract void cppFlags(@NonNull String cppFlags)

Specifies flags for the C++ compiler.

The following sample enables RTTI (RunTime Type Information) support and C++ exceptions:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Sets optional flags for the C++ compiler.
cppFlags "-fexceptions", "-frtti"
}
}
}
}

By default, this property is null.

since 2.2.0

getAbiFilters

abstract @NonNull Set<@NonNull StringgetAbiFilters()

Specifies the Application Binary Interfaces (ABI) that Gradle should build outputs for. The ABIs that Gradle packages into your APK are determined by android.defaultConfig.ndk.abiFilter

In most cases, you need to specify ABIs using only android.defaultConfig.ndk.abiFilter, because it tells Gradle which ABIs to both build and package into your APK. However, if you want to control what Gradle should build, independently of what you want it to package into your APK, configure this property with the ABIs you want Gradle to build.

To further reduce the size of your APK, consider configuring multiple APKs based on ABI—instead of creating one large APK with 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.

By default, this property is null.

since 2.2.0

getArguments

abstract @NonNull List<@NonNull StringgetArguments()

Specifies arguments for ndk-build.

The properties you can configure are the same as those available in your Android.mk and Application.mk scripts. The following sample specifies the Application.mk for the ndk-build project:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor you configure.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Passes an optional argument to ndk-build.
arguments "NDK_MODULE_PATH+=../../third_party/modules"
}
}
}
}

By default, this property is null.

since 2.2.0

getCFlags

abstract @NonNull List<@NonNull StringgetCFlags()

Specifies flags for the C compiler.

The following sample enables format macro constants:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Sets an optional flag for the C compiler.
cFlags "-D__STDC_FORMAT_MACROS"
}
}
}
}

By default, this property is null.

since 2.2.0

getCppFlags

abstract @NonNull List<@NonNull StringgetCppFlags()

Specifies flags for the C++ compiler.

The following sample enables RTTI (RunTime Type Information) support and C++ exceptions:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// Sets optional flags for the C++ compiler.
cppFlags "-fexceptions", "-frtti"
}
}
}
}

By default, this property is null.

since 2.2.0

getTargets

abstract @NonNull Set<@NonNull StringgetTargets()

Specifies the library and executable targets from your ndk-build project that Gradle should build.

For example, if your ndk-build project defines multiple libraries and executables, you can tell Gradle to build only a subset of those outputs as follows:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// The following tells Gradle to build only the "libexample-one.so" and
// "my-executible-two" targets from the linked ndk-build project. If you don't
// configure this property, Gradle builds all executables and shared object
// libraries that you define in your ndk-build project. However, by default,
// Gradle packages only the shared libraries in your APK.
targets "libexample-one.so",
// You need to configure this executable and its sources in your
// Android.mk file like you would any other library, except you must
// specify "include $(BUILD_EXECUTABLE)". Building executables from
// your native sources is optional, and building native libraries to
// package into your APK satisfies most project requirements.
"my-executible-demo"
}
}
}
}

If you don't configure this property, Gradle builds all executables and shared object libraries that you define in your ndk-build project. However, by default, Gradle packages only the shared libraries in your APK.

since 2.2.0

targets

abstract void targets(@NonNull String targets)

Specifies the library and executable targets from your ndk-build project that Gradle should build.

For example, if your ndk-build project defines multiple libraries and executables, you can tell Gradle to build only a subset of those outputs as follows:

android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
externalNativeBuild {
ndkBuild {
// The following tells Gradle to build only the "libexample-one.so" and
// "my-executible-two" targets from the linked ndk-build project. If you don't
// configure this property, Gradle builds all executables and shared object
// libraries that you define in your ndk-build project. However, by default,
// Gradle packages only the shared libraries in your APK.
targets "libexample-one.so",
// You need to configure this executable and its sources in your
// Android.mk file like you would any other library, except you must
// specify "include $(BUILD_EXECUTABLE)". Building executables from
// your native sources is optional, and building native libraries to
// package into your APK satisfies most project requirements.
"my-executible-demo"
}
}
}
}

If you don't configure this property, Gradle builds all executables and shared object libraries that you define in your ndk-build project. However, by default, Gradle packages only the shared libraries in your APK.

since 2.2.0