Skip to content

Building

Eugene Gershnik edited this page Nov 6, 2023 · 9 revisions

Building C++ library

SimpleJNI comes with a portable CMake script that should work everywhere. It has been tested with

  • Xcode 13 and above
  • Android NDK 23.0.7599858 and above
  • Visual Studio 16.11.2 and above
  • GCC 9.3.0 and above

Android

SimpleJNI requires to be built with RTTI and exceptions enabled. It should work with GNU STL or LLVM libc++. It might or might not work with STLPort. As on every other platform C++17 mode is required.

CMake

Easy method

First have CMake fetch SimpleJNI library (replace the GIT_TAG value with the version you want to fetch.)

include(FetchContent)
FetchContent_Declare(SimpleJNI
        GIT_REPOSITORY git@github.com:gershnik/SimpleJNI.git
        GIT_TAG 3.8
        GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(SimpleJNI)

Then declare dependency on SimpleJNI in your library or executable

#The library name is smjni for backward copatibility
target_link_libraries(your_target_name smjni::smjni)

Manual method

Clone this repository somewhere

Add the following to your CMakeLists.txt

add_subdirectory("/path/to/SimpleJNI" ${CMAKE_CURRENT_BINARY_DIR}/SimpleJNI)

#set relevant C++17 flag for your compiler here
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

#The library name is smjni for backward copatibility
target_link_libraries(your_target_name smjni::smjni)

Other build systems

You will need to

  • Build all *.cpp files under src directory and link them with your code
  • Add inc directory to your include path

Build requires C++17 and a modern standard library.

Building JniGen Code Generator

The code generator uses Gradle build system. The easiest way to build it is to open jnigen folder in Android Studio or IntelliJ and build from there. Alternatively you can use command line

cd jnigen
./gradlew assemble

The jnigen.jar and jnigen-annotations.jar will be produced in jnigen/build folder.

Building and running tests

Tests build and execution is driven from C++ and uses CMake. CMake build invokes Gradle to build Java parts as necessary. You should have CMake in your PATH for this to work.

cd tests
mkdir build
cd build
cmake ..
#this runs native functionality tests on Desktop
cmake --build . --config Debug --target run-jar
#this runs JniGen code generator tests
cmake --build . --config Debug --target run-java-tests 
#this runs native functionality tests on Android 
#you will need to configure cmake with Android toolchain and 
#have exactly 1 emulator or device running
cmake --build . --config Debug --target run-dex