How to use googletest for testing C++ code that calls into java on android?

I am working on a rather complicated C++ library that I plan to test properly using googletest for Android NDK.

So far I follow the google test example and structure the project like this:

  • method called after release() exception unable to resume with android camera
  • Implementation of Google design guidelines for Sliders
  • How to debug javascript in webview in android
  • What is the `Authorization` part of the http post request of Google's Firebase Downstream message?
  • How to add controls to a Tab Layout in Android?
  • Detecting Webview Error and Show Message

      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_STATIC_LIBRARIES := googletest_main
      include $(BUILD_EXECUTABLE)
      $(call import-module,third_party/googletest)

    I build and call the test using a script file:

    adb push libs/armeabi/ //data/local/tmp/
    adb push libs/armeabi/ //data/local/tmp/
    adb push libs/armeabi/foo_unittest //data/local/tmp/
    adb shell chmod 775 //data/local/tmp/foo_unittest
    adb shell "LD_LIBRARY_PATH=//data/local/tmp //data/local/tmp/foo_unittest"

    This works fine with any pure C++ code that doesn’t have many references but a lot of my code actually relies on java/jni calls. How can I run googletest with a complete apk file that comes not just with C++ code but also java and resources?

  • Android listview with checkbox problem
  • Sending Bundle with Fragment Transaction
  • Receiving a build error when upgrading to gradle 3.3 and android build tools 2.3.0
  • Vuforia vs. Metaio SDK vs. ARKit vs. D'Fusion Mobile vs. Layar SDK
  • How to connect a network printer over Android?
  • ExpandableListView - keep selected child in a “pressed” state
  • One Solution collect form web for “How to use googletest for testing C++ code that calls into java on android?”

    Inside your test program, you will have a main() function which looks something like this:

    int main(int argc, char **argv) {
      ::testing::InitGoogleTest(&argc, argv);
      return RUN_ALL_TESTS();

    This allows you to invoke the test program like any other: just type the name of the executable in a shell.

    More info on:

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.