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

对着背影说爱祢 提交于 2019-11-30 03:56:25

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.

add-native-code

android {
  ...
  defaultConfig {...}
  buildTypes {...}

  // Encapsulates your external native build configurations.
  externalNativeBuild {

    // Encapsulates your CMake build configurations.
    cmake {

      // Provides a relative path to your CMake build script.
      path "CMakeLists.txt"
    }
  }
}

Note: If you want to link Gradle to an existing ndk-build project, use the ndkBuild {} block instead of cmake {}, and provide a relative path to your Android.mk file. Gradle also includes the Application.mk file if it is located in the same directory as your Android.mk file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!