20

I checked a previous answer about unit test for Android, where it is suggested Googletest as a good option. However, I got a look into the Google C++ Testing Framework - Googletest. About platforms, I don't see anything mentioning support to Android. Could someone tell anything, if there is some way of using it with Android devices - e.g. steps to build a toolchain, etc?

Community
  • 1
  • 1
Linda Lowela
  • 393
  • 1
  • 3
  • 10
  • 1
    In new NDK you can use cmake so its now much more simple: `TARGET_LINK_LIBRARIES(${PROJECT_NAME} gtest)` – Gelldur Aug 13 '16 at 15:23
  • @DawidDrozd you can use `target_link_libraries`, but there is no script that builds **gtest**. You need [few more lines in CMakeLists.txt](https://stackoverflow.com/a/46448545/192373), don't you? – Alex Cohn Sep 27 '17 at 13:01
  • Yes there is. I mean gtest has already CMakeLists you need only add subdirectory – Gelldur Sep 27 '17 at 13:30

4 Answers4

32

GoogleTest is now distributed with the NDK (mainly because it's used by the NDK test suite itself).

It's very easy to use in your own projects, see $NDK/sources/third_party/googletest/README.NDK for usage examples.

Digit
  • 2,073
  • 1
  • 13
  • 10
  • But seem like it is rather a old one (older than 1.6.0). Probably now, fetching the sources and building is the right way as in the accepted answer. – Ananth Pattabiraman May 11 '18 at 07:50
12

You need to built Googletest for Android to be able to run it with your toolchain, as you working with cross-compilation.

Download source code of googletest

$ mkdir googletest
$ cd googletest
$ svn checkout http://googletest.googlecode.com/svn/trunk/ .

Copy jni directory to googletest directory

$ cd /path/to/this/git
$ cp -r jni googletest/

run ndk-build script

$ cd googletest/ $ ndk-build 

You can find libgtest.a in googletest/obj/local/armeabi/libgtest.a

Source: sfuku7 / googletest_android_ndk-build - github

2

jni/Android.mk:

  LOCAL_PATH := $(call my-dir)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo
  LOCAL_SRC_FILES := foo.cpp
  include $(BUILD_SHARED_LIBRARY)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo_unittest
  LOCAL_SRC_FILES := foo_unittest.cpp
  LOCAL_SHARED_LIBRARIES := foo
  LOCAL_STATIC_LIBRARIES := googletest_main
  include $(BUILD_EXECUTABLE)

  $(call import-module,third_party/googletest)
KunMing Xie
  • 1,613
  • 17
  • 15
-1

This gradle plugin https://github.com/fsbarata/ndktest-plugin will help you get the Googletest working. Still worth to look at the Googletest framework though.