Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use NDK-Build in Android Studio 2.2 Preview 1

Android Studio 2.2 Preview 1 has a new external ndk build feature, but from app/build.gradle snippet shown in official blog post it's not clear at all how to set additional ndk build parameters which Application.mk file usually contains enter image description here

I'm able to set Android.mk ndk build file via externalNativeBuild, but how could I set the required Application.mk variables?

My Application.mk contains:

NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-16
APP_ABI := armeabi
APP_STL := c++_static
APP_CPPFLAGS += -std=c++11
like image 443
Tadas Avatar asked May 22 '16 19:05

Tadas


1 Answers

Android Studio 2.2 Preview 3 with updated gradle plugin added support for additional arguments. You can set Application.mk and additional configuration like this:

defaultConfig {
  ndkBuild {
    arguments "NDK_APPLICATION_MK:=Application.mk"
    cFlags "-DTEST_C_FLAG1"  "-DTEST_C_FLAG2"
    cppFlags "-DTEST_CPP_FLAG2"  "-DTEST_CPP_FLAG2"
    abiFilters "armeabi-v7a", "armeabi"
  } 
}

If possible I would recommend migrating to CMake build system, because of better C++ code editor and debugging integration in Android Studio. You will find more info on gradle plugin configuration here: https://sites.google.com/a/android.com/tools/tech-docs/external-c-builds.

Edit: From Android Studio 2.2 Preview 5 you must wrap cmake and ndkBuild groups under externalNativeBuild group:

defaultConfig {
  externalNativeBuild {
    ndkBuild {
      targets "target1", "target2"
      arguments "NDK_APPLICATION_MK:=Application.mk"
      cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
      cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
      abiFilters "armeabi-v7a", "armeabi"
    } 
  }
}

Edit 2: It seems that wrapping ndkBuild under externalNativeBuild group does not work because of a bug in build tools.

like image 78
Tadas Avatar answered Oct 07 '22 11:10

Tadas