【问题标题】:How to get the BUILD TYPE while using Android NDK built via CMAKE?使用通过CMAKE构建的Android NDK时如何获取BUILD TYPE?
【发布时间】: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 的值。请帮忙。

【问题讨论】:

标签: android c++ cmake android-ndk


【解决方案1】:

添加到 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
        }
    }
}

【讨论】:

    【解决方案2】:

    因此,在评论部分中的建议的帮助下,我能够实现我想要的,如下所示:

    我首先在模块级别添加我的 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 是不同构建类型的值。

    然后在我的 native-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());
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-13
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2015-01-14
      • 2011-11-30
      • 1970-01-01
      相关资源
      最近更新 更多