android-ndk-swig-example icon indicating copy to clipboard operation
android-ndk-swig-example copied to clipboard

Add example of running junit tests calling native code

Open sureshjoshi opened this issue 7 years ago • 1 comments

A problem with running native code is that you often need to use the instrumentation tests to run unit tests.

Actually, in reality, you just need to compile your libraries for your platform (as junit runs on your computer's native platform).

sureshjoshi avatar Nov 14 '17 08:11 sureshjoshi

Rough CMake is

if (APPLE)
  find_package(JNI REQUIRED)

  find_package(SWIG REQUIRED)
  include(${SWIG_USE_FILE})

  include_directories(${JAVA_INCLUDE_PATH})
  set_property(SOURCE SeePlusPlus.i PROPERTY CPLUSPLUS ON)
  swig_add_library(SeePlusPlus_Wrapper LANGUAGE java SOURCES SeePlusPlus.i)
  swig_link_libraries(SeePlusPlus_Wrapper SeePlusPlus)
  add_library(protobuf-lite SHARED IMPORTED)
  set_target_properties(protobuf-lite PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/osx/libprotobuf-lite.dylib)
  target_link_libraries( # Specifies the target library.
                         SeePlusPlus
                         protobuf-lite)

And then

    @Test
    fun addition_isCorrect() {
        val wrapperLib = File("app/src/main/jniLibs/osx/libSeePlusPlus_Wrapper.jnilib")
        System.load(wrapperLib.absolutePath)
        var mCpp = SeePlusPlus()
        val result = mCpp.Multiply(5, 1)
        Assert.assertEquals(5, result)
    }

sureshjoshi avatar Nov 14 '17 08:11 sureshjoshi