如何在 Android Studio 2.2 Preview 1 中正确使用 NDK-Build
How to properly use NDK-Build in Android Studio 2.2 Preview 1
Android Studio 2.2 Preview 1 有一个新的外部 ndk 构建功能,但是从 app/build.gradle
官方博客 post 中显示的片段来看,它是完全不清楚如何设置额外的 ndk 构建参数,Application.mk
文件通常包含
我可以通过 externalNativeBuild
设置 Android.mk
ndk 构建文件,但是如何设置所需的 Application.mk
变量?
我的 Application.mk
包含:
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-16
APP_ABI := armeabi
APP_STL := c++_static
APP_CPPFLAGS += -std=c++11
Android Studio 2.2 Preview 3 更新后的 gradle 插件增加了对额外参数的支持。您可以像这样设置 Application.mk 和其他配置:
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"
}
}
如果可能,我建议迁移到 CMake 构建系统,因为 Android Studio 中有更好的 C++ 代码编辑器和调试集成。您可以在此处找到有关 gradle 插件配置的更多信息:
https://sites.google.com/a/android.com/tools/tech-docs/external-c-builds.
编辑:
从 Android Studio 2.2 Preview 5 开始,您必须将 cmake
和 ndkBuild
组包装在 externalNativeBuild
组下:
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"
}
}
}
编辑 2: 由于构建工具中的错误,似乎将 ndkBuild
包装在 externalNativeBuild
组下不起作用。
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.
Android Studio 2.2 Preview 1 有一个新的外部 ndk 构建功能,但是从 app/build.gradle
官方博客 post 中显示的片段来看,它是完全不清楚如何设置额外的 ndk 构建参数,Application.mk
文件通常包含
我可以通过 externalNativeBuild
设置 Android.mk
ndk 构建文件,但是如何设置所需的 Application.mk
变量?
我的 Application.mk
包含:
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-16
APP_ABI := armeabi
APP_STL := c++_static
APP_CPPFLAGS += -std=c++11
Android Studio 2.2 Preview 3 更新后的 gradle 插件增加了对额外参数的支持。您可以像这样设置 Application.mk 和其他配置:
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"
}
}
如果可能,我建议迁移到 CMake 构建系统,因为 Android Studio 中有更好的 C++ 代码编辑器和调试集成。您可以在此处找到有关 gradle 插件配置的更多信息: https://sites.google.com/a/android.com/tools/tech-docs/external-c-builds.
编辑:
从 Android Studio 2.2 Preview 5 开始,您必须将 cmake
和 ndkBuild
组包装在 externalNativeBuild
组下:
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"
}
}
}
编辑 2: 由于构建工具中的错误,似乎将 ndkBuild
包装在 externalNativeBuild
组下不起作用。
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.