android_plugins

How to Create KiriKiri Z Plugins for Android

The android_plugins repository provides a base project for building KiriKiri Z plugins for Android. This project is intended for compiling and generating .so files and cannot be used for debugging. If you want to debug a plugin, copy the plugin project into the main KiriKiri Z project and add the plugin’s directory location to the root CMakeList.txt (since tp_stub.h/.cpp are not in the Android project root in the main project, they need to be copied). Details are provided later.

Limitations

In the Android version of the plugin, only V2Link/V2Unlink on the plugin side are imported, and other functions cannot be used. Plugin names must start with “lib”. Also, names must be all lowercase. In other words, the name will be something like libxxx.so. Although the “lib” and “.so” parts differ from the Windows version, even if you specify XXX.dll in TJS2, it will be automatically loaded as libxxx.so at runtime (if the “lib” prefix is missing, it is added; .dll is converted to .so; uppercase is converted to lowercase). This allows the same plugin to be accessed with common TJS2 scripts. Additionally, as expected, Windows APIs cannot be used.

Work Required for Android Support

Add the directory of the plugin you want to build under “plugins” in android_plugins. Add the added directory to plugins/CMakeList.txt. Specifically, write add_subdirectory( directory_name ) at the end. Since plugin source code might be in a separate GitHub repository, it is recommended to add it as a submodule. The kagparser plugin is added as a sample, so the following explanation will refer to it.

Files Required for Android Support

The following two files are newly required.

version-script

This file describes the functions to be exported. It corresponds to the .def file in Windows. The version_script.map file in the KAGParser plugin repository corresponds to this. The content can be exactly the same, so simply copy the file.

CMakeList.txt

This file is required for building with cmake. It describes the library name to build, include directories, and source code files. It is easy to copy and rewrite the CMakeList.txt from the KAGParser plugin repository. Points to rewrite: Include directories are listed inside include_directories(), so if necessary, add a newline before the final “)” and append include directories. List the target library name and source code in add_library(). Change “kagparser” to the target plugin name, and list source code filenames after the empty line following SHARED. Since ../tp_stub.cpp is likely common, rewrite up to that point. Write the target library name in target_link_libraries(). Write the same name as used in add_library. Basically, these three points should be sufficient, but if you want to add debug options or reference other libraries, search for cmake usage or refer to the KiriKiri main body’s CMakeList.txt.

Building the Plugin

Open the android_plugins directory in Android Studio. Select “release” in Build Variant. Build like any other Android app. CPU-specific .so files will be generated under android_plugins/app/build/intermediates/cmake/release/obj.

Debugging the Plugin

The method above is simply for building the plugin to use with KiriKiri. If it works fine on Windows and is not environment-dependent, you can probably build and use it as is, but if you want to perform source code debugging, you need to build it along with the main body. Create a folder named “plugins” or similar under krkrz/android and copy tp_stub.h/.cpp into it. Copy the plugin source code directory into “plugins”. In the root CMakeList.txt, where “add_subdirectory” is listed under “# external library” near the bottom, add the copied folder name like add_subdirectory(android/plugins/xxx). Since debug information is stripped in plugins, modify it so it is not stripped. Specifically, in the line starting with “set( CMAKE_SHARED_LINKER_FLAGS” in CMakeList.txt, delete the part near the end that says “-Wl,–strip-debug -Wl,–discard-all”. If you build the KiriKiri Z main body, the plugin source code will be built simultaneously, so you can set breakpoints and debug as needed.

Other Source Code Notes

Items that will likely need modification in the source code for Android support:

Using Plugins in the Android Version

Copy the directory containing the plugin binaries (so files) (armeabi/armeabi-v7a/arm64-v8a etc.) into the directory where the main project’s so files are placed (krkrz/android/app/src/main/jniLibs). Build APK and generate an apk, and the plugin will be included in the apk. After that, you can use it by calling Plugin.link(filename) from a TJS2 script as usual.