【发布时间】:2021-07-19 09:23:18
【问题描述】:
我对集成 Android NDK 还很陌生。我试图在向我的主应用程序调用本机函数时返回不同的文本。我有两种构建类型 - 发布和调试。如何针对不同的构建类型向我的主应用发送不同的字符串?
下面是代码:
native-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 的值。请帮忙。
【问题讨论】:
-
参见stackoverflow.com/questions/44084950/… 相同的原理,不同之处在于您将使用
cmake而不是ndkBuild,并且您可能必须使用cppFlags而不是cFlags。 -
如何获取cpp文件中flags的值?
-
要么使用
#ifdef根据是否设置标志将字符串初始化为不同的值,要么使用stackoverflow.com/questions/46771588/…之类的东西 -
谢谢!!这非常有效。
标签: android c++ cmake android-ndk