如何在 Android Studio 中构建本地/主机操作系统 C++ 单元测试?

问题描述 投票:0回答:2

我正在开发一个包含许多本机模块的大型项目,我想引入 C++ 代码的单元测试,这些单元测试与 Java JUnit 本地测试(即在主机操作系统上运行,而不是插装)测试一样简单/快速运行.

我已按照官方说明设置 gtest,但运行我的外部本机构建会导致链接器错误:

> Task :app:externalNativeBuildDebug FAILED
Build GoogleTests x86_64
[1/2] Building CXX object CMakeFiles/GoogleTests.dir/googletest_unittests/shared/TautologyTe
sts.cpp.o

[2/2] Linking CXX executable GoogleTests
FAILED: cmd.exe /C "cd . && C:\Users\Jeffrey.Creswell\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android21 --gcc-toolchain=C:/Users/Jeffrey.Creswell/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Jeffrey.Creswell/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=c++14 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections CMakeFiles/GoogleTests.dir/googletest_unittests/shared/TautologyTests.cpp.o CMakeFiles/GoogleTests.dir/googletest_unittests/Tautology.cpp.o  -o GoogleTests  C:/Users/Jeffrey.Creswell/androidstudio_projects/NativeAndroidSandbox/app/src/main/cpp/googletest_unittests/libs/windows/x86_64/gtestd.lib -latomic -lm && cd ."
C:\Users\Jeffrey.Creswell\androidstudio_projects\NativeAndroidSandbox\app\src\main\cpp\googletest_unittests\shared/TautologyTests.cpp:19: error: undefined reference to 'testing::internal::GetTestTypeId()'
C:\Users\Jeffrey.Creswell\androidstudio_projects\NativeAndroidSandbox\app\src\main\cpp\googletest_unittests\shared/TautologyTests.cpp:19: error: undefined reference to 'testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
...

我在 Visual Studio 中构建了 gtestd.lib 并将其作为 CMakeLists.txt 中的预构建引用,以尝试在某种程度上简化方程式:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

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).
        native-lib.cpp)

set(gtest_INC googletest_unittests/include)
include_directories(${gtest_INC})
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/googletest_unittests/libs/windows/x86_64/gtestd.lib)
add_executable(GoogleTests
        ${CMAKE_CURRENT_SOURCE_DIR}/googletest_unittests/shared/TautologyTests.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/googletest_unittests/Tautology.cpp)
target_link_libraries(GoogleTests gtest)
# todo: insert add_test() cmake commands once the linker error above is resolved

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

我认为主要问题是

"--target=x86_64-none-linux-android21"
clang++.exe
的争论;它意味着为 Android/Linux 编译测试可执行文件,而我真正想要的是为主机操作系统(Windows 10)编译。我不知道这是否会产生我所看到的链接器错误,但尝试将 Linux 可执行文件链接到 Windows 静态库似乎也会类似地失败。我如何告诉 Android NDK 的 clang++ 以主机操作系统为目标(最好通过修改我的
CMakeLists.txt
)?假设这不是问题,是否有一种已知的方法可以让本地(在主机操作系统上运行)C++ 单元测试场景在 Android Studio 中工作?

注意:我查看了一个类似的问题,接受的答案似乎有效,但生成了一个 Android 可执行文件——我更喜欢这样一种情况,理想情况下我可以在 Android Studio 中点击绿色箭头并运行我的 C++ 单元测试自动在本地计算机上。

android c++ android-ndk
2个回答
1
投票

Android Studio不支持主机开发。

如果您想针对主机构建测试并在主机上运行它们,则必须从 Android Studio 外部调用 CMake(命令行是一个选项,Visual Studio 也是如此)。


0
投票

现在 androidx 支持它 - https://github.com/android/ndk-samples/tree/main/unit-test 。您可以在测试设备或模拟器上将 google 测试包装为 androidTest 包中的 Kotlin 测试并运行。

© www.soinside.com 2019 - 2024. All rights reserved.