Skip to content

Building

gershnik edited this page Oct 17, 2020 · 10 revisions

Building C++ library

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

Apple Clang version 12.0.0 (clang-1200.0.32.2) from Xcode 12 Android Clang version (6454773 based on r365631c2) clang version 9.0.8 from NDK 21.3.6528147 MSVC 19.27.29112 from Visual Studio 16.7.6 GCC 9.3.0

Android

Smjni 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 platfrom C++17 mode is required.

CMake

Clone this repository somewhere

Add the following to your CMakeLists.txt

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

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

target_link_libraries(your_target_name smjni ${JAVA_JVM_LIBRARY})

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 ..
make
cd java
java -jar smjnitests.jar

On Linux you might need to modify the last command as follows

java -Djava.library.path=. -jar smjnitests.jar