带有 Google 测试的 Android NDK [英] Android NDK with Google Test

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

问题描述

我正在尝试在 Android Studio 上使用 GoogleTest.

据我了解,最新版的NDK已经包含了gtest.

我没有找到明确的指南.

我遵循了

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

此外,

  • 最后如何运行 adb?
  • 我创建了一个 android.mk 文件,但我应该在哪里调用它?

解决方案

如果您选择 cmake 来驱动您的 externalNativeBuild(根据 Android,这是首选选项开发人员 NDK 指南),然后您只需将以下行添加到您的 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 在模拟器或设备上运行它.

<小时>

注意事项:

  • 确保 ABI 与您使用的目标匹配(指南对此不是很清楚).
  • 构建的 ABI 列表由 build.gradle 中的 abiFilters 控制.在 Android Studio 中,即使 ndk-build 也会忽略 Application.mk 中设置的 APP_ABI.
  • 使用 cmake 时会忽略文件 Android.mkApplication.mk.
  • 对于 gradle-3.3classpath 'com.android.tools.build:gradle:2.3.3',与当前的Android Studio 2.3.3版本一样,您可能需要在build.gradle中明确指定unittest目标强>:

    android { defaultConfig { externalNativeBuild { cmake { 目标foo_unittest"}}}}

  • 使用 Android Studio 3.0,gradle-4.1,和 classpath 'com.android.tools.build:gradle:3.0.0-beta6' 可执行文件更容易在 app/build/intermediates/cmake/debug/obj.

<小时>

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

# 构建 libfoo.soadd_library(foo 共享 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 测试的 Android NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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