如何在 Android Studio 中构建 local/host OS C++ 单元测试?
How can I build local/host OS C++ unit tests in Android Studio?
我正在处理一个包含许多本机模块的大型项目,我想介绍 C++ 代码的单元测试,这些代码从 simple/fast 到 运行 Java JUnit 本地(即 运行 在主机上 OS,未检测)测试。
我已经按照 official instructions 设置 gtest,但是 运行 我的外部本地构建导致 linker 错误:
> 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 编译测试可执行文件,而我真正想要的是为主机 OS (Windows 10) 编译。我不知道这是否会产生我看到的 linker 错误,但是尝试 link 一个 Linux 可执行文件到 Windows 静态库似乎会同样失败。我如何告诉 Android NDK 的 clang++ 以主机 OS 为目标(最好通过修改我的 CMakeLists.txt
)?假设这不是问题,是否有一种已知的方法来获得在 Android Studio 中工作的本地(运行ning 在主机 OS 上)C++ 单元测试场景?
注意:我查看了 并且接受的答案似乎有效,但生成了一个 Android 可执行文件——我更希望在理想情况下我可以点击绿色箭头Android Studio 并在本地计算机上自动进行我的 C++ 单元测试 运行。
AndroidStudio不支持主机开发。
如果您想针对您的主机构建测试并 运行 它们在那里,您必须从 Android Studio 外部调用 CMake(命令行是一个选项,Visual Studio).
我正在处理一个包含许多本机模块的大型项目,我想介绍 C++ 代码的单元测试,这些代码从 simple/fast 到 运行 Java JUnit 本地(即 运行 在主机上 OS,未检测)测试。
我已经按照 official instructions 设置 gtest,但是 运行 我的外部本地构建导致 linker 错误:
> 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 编译测试可执行文件,而我真正想要的是为主机 OS (Windows 10) 编译。我不知道这是否会产生我看到的 linker 错误,但是尝试 link 一个 Linux 可执行文件到 Windows 静态库似乎会同样失败。我如何告诉 Android NDK 的 clang++ 以主机 OS 为目标(最好通过修改我的 CMakeLists.txt
)?假设这不是问题,是否有一种已知的方法来获得在 Android Studio 中工作的本地(运行ning 在主机 OS 上)C++ 单元测试场景?
注意:我查看了
AndroidStudio不支持主机开发。
如果您想针对您的主机构建测试并 运行 它们在那里,您必须从 Android Studio 外部调用 CMake(命令行是一个选项,Visual Studio).