Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Building NDK Projects

François-Xavier Thomas edited this page May 31, 2013 · 5 revisions

Note: While standard JARs are automatically picked up by SBT when they are in the lib folder at the root of the project, native libraries (.so files) should go to src/main/libs to be recognized by the plugin.

Building NDK projects

There is some basic NDK support in the android-plugin. For now, it doesn't do anything else than call ndk-build during compilation and clean up the obj and libs directories during cleanup. This depends on an environment variable being set up: either ANDROID_NDK_HOME or ANDROID_NDK_ROOT.

Place your Android NDK sources in src\main\jni. Add the AndroidNdk.settings to your project:

  lazy val someProjectUsingNDK = Project(
    id = ...,
    ...
    settings = ... ++ AndroidBase.settings ++ AndroidNdk.settings
  )

When using JNI one typically uses javah to generate a C header containing the constants and function declarations for classes that contain native methods.

Setting jniClasses in Android specifies the classes with native methods for which C headers should be generated.

By default, javah will generate a separate C header file for each JNI class. To generate a single C header file instead, set javahOutputFile in Android.

  // Project using javah generated C headers
  lazy val someProjectUsingNDK = Project(
    id = ...,
    ...
    settings = ... ++ AndroidBase.settings ++ AndroidNdk.settings ++ Seq(
      jniClasses in Android += "org.example.UsingNative",
      // Single C header with custom name:
      javahOutputFile in Android := Some(new File("my_native.h"))
      )
  )

The make environment variable SBT_MANAGED_JNI_INCLUDE can be used to refer to the directory containing the generated header files.

# Android.mk
LOCAL_C_INCLUDES += \
        ... \
        $(SBT_MANAGED_JNI_INCLUDE)

For more details see the settings defined in AndroidNdkKeys.