Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note: When a Java native method is called before the implementation is loaded it leads to a crash!

1. Java: Create a nativ method

...

Code Block
languagejava
public static native void SetBackgroundColor( int color );


2. C++: Create a matching implementation

...

Code Block
JNIEXPORT void JNICALL Java_com_graebert_aressimplified_JNI_SetBackgroundColor( JNIEnv * env, jclass clazz, jint color ){...}


3. Store the C++ implementation inside a shared library


See CFx/AndroidSimplified_JNI/app/CMakeLists.txt
See CFx/CMakeLists.txt

4. Compile the shared library with CMake

...

Code Block
...externalNativeBuild {
   cmake {
       arguments "-DANDROID_STL=c++_shared", "-DVIEWER=1"
       cppFlags "-std=c++11 -frtti -fexceptions"
   }
}
...
externalNativeBuild {
   cmake {
       path file('../../CMakeLists.txt')
   }
}
...

5. Load the shared library with System.LoadLibrary()

...