Google Test的Android NDK [英] Android NDK with Google Test

查看:247
本文介绍了Google Test的Android NDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



根据我的理解,最新版本的NDK包含gtest。



我没有找到明确的指导如何去做。



我遵循



但它不能识别 #include gtest / gtest.h



另外,


  • 如何在最后运行adb?

  • 我创建了一个android.mk文件,但是我应该在哪里调用它?

解决方案

如果您选择 cmake 来驱动您的 externalNativeBuild (根据Android Developers CMakeLists.txt 中: CMakeLists.txt

  set(GOOGLETEST_ROOT $ {ANDROID_NDK} / sources / third_party / googletest / googletest)
add_library(gtest STATIC $ {GOOGLETEST_ROOT } /src/gtest_main.cc $ {GOOGLETEST_ROOT} /src/gtest-all.cc)
target_include_directories(gtest PRIVATE $ {GOOGLETEST_ROOT})
target_include_directories(gtest PUBLIC $ {GOOGLETEST_ROOT} / include)

add_executable(footest src / main / jni / foo_unittest.cc)
target_link_libraries(footest gtest)

如果您的构建成功,您会发现 app / .externalNativeBuild / cmake / debug / x86 / footest 。从这里开始,您可以按照自述文件中的说明进行操作。 NDK 在模拟器或设备上运行。






备注: p>


  • 确保ABI与您使用的目标匹配(指南对此不太清楚)。
  • 构建的ABI列表由 build.gradle 中的 abiFilters 控制。在Android Studio中,甚至 ndk-build 都会忽略 Application.mk 中设置的APP_ABI。 / strong>和 Application.mk 在使用 cmake 时会被忽略。 gradle-3.3 classpath'com.android.tools.build:gradle :2.3.3',就像在当前的Android Studio版本2.3.3中一样,你可能需要在 build.gradle 中显式指定unittest目标:

      android {defaultConfig {externalNativeBuild {cmake {targetsfoo_unittest}}}} 

  • 使用Android Studio 3.0的
  • gradle-4.1 classpath'com.android.tools.build:gradle:3.0.0-beta6'这个可执行文件更容易找到应用程序/生成/中间体/ C make / debug / obj







要在共享库中测试 foo.cpp 中的 foo(int x,int y)函数(使其尽可能接近 NDK说明 ),你在 CMakeLists.txt 脚本中需要更多行:

 #build libfoo.so 
add_library(foo SHARED src / main / jni / foo.cpp)
target_link_libraries(footest foo)

您可以在 app / build / intermediates / cmake / debug / obj 下找到 libfoo.so 手动复制到您的设备。



为了减少麻烦,您可以使用 STATIC 来代替 SHARED 或将 foo.cpp 添加到 footest 可执行文件: add_executable(footest src / main / jni / foo _unittest.cc src / main / jni / foo.cpp)


I'm trying to use GoogleTest on Android Studio.

According to what I understood, the latest version of NDK has the gtest included.

I did not find a clear guide how to do it.

I followed this document:

So, I opened a new project, created jni folder and the following files (inside the files I wrote exactly what the document):

But it does not recognize the #include gtest/gtest.h

In addition,

  • how to run the adb at the end?
  • I created an android.mk file but where should I call it?

解决方案

If you choose cmake to drive your externalNativeBuild (and this is the preferred option, according to Android Developers NDK guide), then you can simply add the following lines to your CMakeLists.txt:

set(GOOGLETEST_ROOT ${ANDROID_NDK}/sources/third_party/googletest/googletest)
add_library(gtest STATIC ${GOOGLETEST_ROOT}/src/gtest_main.cc ${GOOGLETEST_ROOT}/src/gtest-all.cc)
target_include_directories(gtest PRIVATE ${GOOGLETEST_ROOT})
target_include_directories(gtest PUBLIC ${GOOGLETEST_ROOT}/include)

add_executable(footest src/main/jni/foo_unittest.cc)
target_link_libraries(footest gtest)

If your build succeeds, you will find app/.externalNativeBuild/cmake/debug/x86/footest. From here, you can follow the instructions in README.NDK to run it on emulator or device.


Notes:

  • make sure that the ABI matches the target you use (the guide is not very clear about this).
  • the list of ABI's that are built is controlled by abiFilters in build.gradle. In Android Studio, even ndk-build ignores APP_ABI set in Application.mk.
  • the files Android.mk and Application.mk are ignored when you use cmake.
  • for gradle-3.3, and classpath 'com.android.tools.build:gradle:2.3.3', as in the current Android Studio release 2.3.3, you may need to explicitly specify the unittest target in build.gradle:

    android { defaultConfig { externalNativeBuild { cmake { targets "foo_unittest" }}}}
    

  • with Android Studio 3.0, gradle-4.1, and classpath 'com.android.tools.build:gradle:3.0.0-beta6' the executable is easier to find under app/build/intermediates/cmake/debug/obj.


To test the foo(int x, int y) function from foo.cpp in a shared library (to make is as close as possible to the NDK instructions), you need some more lines in your CMakeLists.txt script:

# build libfoo.so
add_library(foo SHARED src/main/jni/foo.cpp)
target_link_libraries(footest foo) 

You will find libfoo.so to copy manually to your device under app/build/intermediates/cmake/debug/obj.

To reduce the hassle, you can use STATIC instead of SHARED, or simply add foo.cpp to footest executable:

add_executable(footest src/main/jni/foo_unittest.cc src/main/jni/foo.cpp)

这篇关于Google Test的Android NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆