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?
You can use CMake or ndk-build (Android.mk), not both. The gtest pieces of the NDK are not plumbed through for CMake. https://github.com/android-ndk/ndk/issues/500
Just to add to Alex's excellent answer, you can also deploy and run the resulting test binary using
adb
by adding the following to yourCMakeLists.txt
:Note that in the above example
footest
is dependent on the shared librarynative-lib
which is why we push that. The path tonative-lib
is specified by setting theLD_LIBRARY_PATH
environment variable.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:
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:
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: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 underapp/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:
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 ofSHARED
, or simply add foo.cpp to footest executable:To piggyback everyone's answers... Not all the solutions here worked 100%, but I did combine all the answers here to get something that worked for me. I'm building our libraries in CMake, whose build is generated by the Android Studio plugin. I've gotten our GoogleTests running directly via
bash
andadb
.Caveats:
add_executable(UnitTest "")
and link your stuff there.${AS_STUDIO_LIBRARY_ROOT}/build/intermediates/cmake/${release|debug}/obj/${ARCH}
houses your compiled source. This should include shared libraries and other libs as well as the unit test executable. This executable won't make it to your final APK, so no worries there./data/local/tmp/<PROJECT_NAME>
directly thenchmod 777
ing everything will not work for some reason, especially on the Pixel 2 and the emulator:adb push
ing your resources, libraries, and googletest executable to the/sdcard/<PROJECT_NAME>
folder firstadb shell mv /sdcard/<PROJECT_NAME> /data/local/tmp/.
chmod 777 -R /data/local/tmp/<PROJECT_NAME>
After this is all done, you should be able to run your googletest like this:
I also got remote debugging working via
gdbserver
andgdb
through Visual Studio Code. I'd prefer to uselldb
instead but I haven't figured it out yet. This topic to get full debugging to work will require multiple paragraphs, so feel free to PM me if you gotlldb
working with Visual Studio Code or are curious how I solved this issue.Don't forget to remove the files after running the unit tests since they'll stay on your device otherwise.