如何在使用通过 CMAKE 构建的 Android NDK 时获取 BUILD TYPE?
How to get the BUILD TYPE while using Android NDK built via CMAKE?
我对集成 Android NDK 还很陌生。我正在尝试 return 不同的文本,同时向我的主应用程序调用本机函数。我有两种构建类型——发布和调试。如何针对不同的构建类型向我的主应用程序发送不同的字符串?
下面是代码:
原生-lib.cpp
extern "C"
JNIEXPORT jstring JNICALL
Java_test_main_MainActivity_getStringFromJNI(JNIEnv *env, jobject thiz) {
std::string stringToBeReturned = "Hello";
return env->NewStringUTF(stringToBeReturned.c_str());
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
我想在 native-lib.cpp 中获取构建类型,以便我可以更改 stringToBeReturned[=30 的值=] 取决于构建类型。请帮忙。
所以我在评论部分的建议的帮助下能够实现我想要的,如下所示:
我首先在模块级别添加 build.gradle。
{
defaultConfig
...
...
...
//other properties
externalNativeBuild{
cmake{
cppFlags "-DBUILD_TYPE=debug"
}
}
}
buildTypes {
release {
...
...
//other properties
externalNativeBuild{
cmake{
cppFlags "-DBUILD_TYPE=release"
}
}
}
}
这里,BUILD_TYPE 是传递的变量的名称,debug 和 release 是不同构建类型的值。
然后在我的本地-lib.cpp,我应用了以下代码:
#define xstr(s) str(s) //important header
#define str(s) #s //important header
extern "C"
JNIEXPORT jstring JNICALL
Java_test_main_MainActivity_getStringFromJNI(JNIEnv *env, jobject thiz) {
std::string stringToBeReturned;
std::string build_type = xstr(BUILD_TYPE); //get the value of the variable from grade.build
if(build_type=="debug"){
stringToBeReturned = "Yay!! String returned from debug variant!"
}else if(build_type=="release"){
stringToBeReturned = "Yay!! String returned from release variant!"
}
return env->NewStringUTF(stringToBeReturned.c_str());
}
添加到 CMakeLists.txt:
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-DMY_DEBUG")
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-DMY_RELEASE")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
之后您可以在代码中使用#ifdef
另一种选择是传递参数以从 gradle 脚本构建
debug {
externalNativeBuild {
cmake {
arguments "-DMY_DEBUG
}
}
}
我对集成 Android NDK 还很陌生。我正在尝试 return 不同的文本,同时向我的主应用程序调用本机函数。我有两种构建类型——发布和调试。如何针对不同的构建类型向我的主应用程序发送不同的字符串?
下面是代码:
原生-lib.cpp
extern "C"
JNIEXPORT jstring JNICALL
Java_test_main_MainActivity_getStringFromJNI(JNIEnv *env, jobject thiz) {
std::string stringToBeReturned = "Hello";
return env->NewStringUTF(stringToBeReturned.c_str());
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
我想在 native-lib.cpp 中获取构建类型,以便我可以更改 stringToBeReturned[=30 的值=] 取决于构建类型。请帮忙。
所以我在评论部分的建议的帮助下能够实现我想要的,如下所示:
我首先在模块级别添加 build.gradle。
{
defaultConfig
...
...
...
//other properties
externalNativeBuild{
cmake{
cppFlags "-DBUILD_TYPE=debug"
}
}
}
buildTypes {
release {
...
...
//other properties
externalNativeBuild{
cmake{
cppFlags "-DBUILD_TYPE=release"
}
}
}
}
这里,BUILD_TYPE 是传递的变量的名称,debug 和 release 是不同构建类型的值。
然后在我的本地-lib.cpp,我应用了以下代码:
#define xstr(s) str(s) //important header
#define str(s) #s //important header
extern "C"
JNIEXPORT jstring JNICALL
Java_test_main_MainActivity_getStringFromJNI(JNIEnv *env, jobject thiz) {
std::string stringToBeReturned;
std::string build_type = xstr(BUILD_TYPE); //get the value of the variable from grade.build
if(build_type=="debug"){
stringToBeReturned = "Yay!! String returned from debug variant!"
}else if(build_type=="release"){
stringToBeReturned = "Yay!! String returned from release variant!"
}
return env->NewStringUTF(stringToBeReturned.c_str());
}
添加到 CMakeLists.txt:
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-DMY_DEBUG")
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-DMY_RELEASE")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
之后您可以在代码中使用#ifdef
另一种选择是传递参数以从 gradle 脚本构建
debug {
externalNativeBuild {
cmake {
arguments "-DMY_DEBUG
}
}
}