3

Google includes support for google test inside of the Android NDK (see https://android.googlesource.com/platform/ndk.git/+/master/sources/third_party/googletest/README.NDK). This works surprisingly well. You can actually create and run native C++ executables on an Android device this way.

The problem is that there appears to be no way to exercise JNI code. The Android NDK disallows these highly useful functions:

/*
 * VM initialization functions.
 *
 * Note these are the only symbols exported for JNI by the VM.
 */
#if 0  /* In practice, these are not exported by the NDK so don't declare them */
jint JNI_GetDefaultJavaVMInitArgs(void*);
jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*);
jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*);
#endif

The only other way I see in jni.h to get a JavaVM is through JNI_OnLoad, but that only gets called for shared libraries that are loaded from Java.

Is there any way around this?

In my case I don't want to call my Google Tests through the Android test framework. I want to use Google Test as the driver for all of my cross-platform unit tests.

See similar: Unit testing on Android NDK

Community
  • 1
  • 1
syvex
  • 7,518
  • 9
  • 43
  • 47
  • I'm not too familar with Android, so excuse my lack of understanding here. When you say you want to "exercise JNI code", do you mean that you want to test your Java code or you want to test your C++ code that is invoked by JNI? – legalize Dec 15 '15 at 22:14
  • There are bits of Java that are called from C++, so I want my C++ code to be able to call Java when needed. I do not want Java to call C++ in this case. – syvex Dec 16 '15 at 16:39
  • I would treat the Java code as a collaborator to be mocked by my C++ tests. Then I would unit test my Java code with JUnit (or whatever) tests. I would integration test the two systems together by integration testing the entire application using [FitNesse](http://fitnesse.org/). – legalize Dec 16 '15 at 17:38
  • Syvex, I know exactly what you want. I to want to test an extensive C/C++ SDK through its JNI as though an application would use the SDK. We're talking application to native library testing. Have you found a solution yet? I want to use CMake Google Tests but first CMake doesn't support Android Studio project generation and Java doesn't have a notion of macros as does the C/C++ preprocessor which Google Tests depends on. The closest thing I guess would have CMake install and build a static Android Studio project that uses the Android Studio test framework - whatever that is. Any better ideas? – BoiseBaked Sep 07 '16 at 01:03
  • Any solution to this mixed C++/Java testing problem ? – Lothar Jul 31 '19 at 01:35

0 Answers0