code coverage for native code using Android NDK

2019-07-08 10:37发布

问题:

I am using Android NDK r16 to build my native code for my Android project. I would like to be able to get the code coverage for my native unit tests. Is this possible?

From my research, it seems that this isn't provided by default in Android and that we need to use custom compiler/linker flags (-fprofile-arcs -ftest-coverage) with Clang.

I found the following article which seems promising. http://logan.tw/posts/2015/04/28/check-code-coverage-with-clang-and-lcov/

However, it isn't really Android related. I was really hoping for an article that was specific to Android and proven to work with the latest NDK/CMake tools.

Is anyone aware of such article?

回答1:

[treat this as a comment I'll delete it after clarification to adhere to site guidelines]

Could I get a follow up to the exact implementation of this? I'm trying to do the exact same process but with ndk 14b and CMake. We have existing unit tests for the native layer but aren't able to pull any coverage metrics from the tests run.

setting the described flags in the cmakeList.txt file as follows unfortunately introduced undefined reference errors for a previously building project.

CMAKE

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage -fprofile-arcs -ftest-coverage")

ERROR:

[3/3] Linking CXX shared library ../../../../build/intermediates/cmake/project/debug/obj/armeabi/libscanhwl_camif.so
  FAILED: : && /home/user/tools/android-ndk-r14b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  --target=armv5te-none-linux-androideabi --gcc-toolchain=/home/user/tools/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/user/tools/android-ndk-r14b/sysroot -fPIC -isystem /home/user/tools/android-ndk-r14b/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=19 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -D__STDC_FORMAT_MACROS  -fexceptions -frtti --coverage -fprofile-arcs -ftest-coverage -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /home/user/tools/android-ndk-r14b/platforms/android-19/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libscanhwl_camif.so -o <This was a list of files> -latomic -lm "/home/user/tools/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" && :
  out/stage2/soong/ndk/sysroot/usr/include/string.h:348: error: undefined reference to '__strncpy_chk2'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.

Do these flags need to be defined elsewhere? OR am I completely off base somewhere?