How to add Kotlin files to CMake Build system

Currently, in our project we are suing java files for Android development. But we want to move to Kotlin files. Kotlin is similar to java only. And thus we though add_jar would be able to work fine with kotlin (.kt) files. But it failed with below error

ninja: error: FindFirstFileExA(CMakeFiles/TallyWorldMain_JAR.dir/D:/TW/Workspace/TallyWorld/FoundationalPlatform/TallyWorldMain.Source/TWMainAndroidKernel): The filename, directory name, or volume label syntax is incorrect.

I have .kit files under D:/TW/Workspace/TallyWorld/FoundationalPlatform/TallyWorldMain.Source/TWMainAndroidKernel folder.

Please let me know how to add kt files in our cmake build system. We are using Ninja Multi-Config generator.

Regards, Mohsin Siddiqui

Can you post the CMake code for this? Ideally, a self-contained example of what you are trying to do.

Hi @bill.hoffman

Please find the attachment for sample project. It has below three sample source files.

  1. SampleCpp.cpp ( to generate static library)
  2. SampleKotlin.kt
  3. SampleJava.kt
    Workspace.zip (2.3 MB)

If I generate jar using ajj_jar, it wroks fine, but when when I add .kt also in jar source file list, it gives me below error

D:\TW\Workspace>cmake --build --preset=Android_Phone-x64-Debug --target TWDevSupport
ninja: error: FindFirstFileExA(CMakeFiles/Test_Jar.dir/D:/TW/Workspace/TallyWorld/DevSupportPlatform/TWDevSupport.Source): The filename, directory name, or volume label syntax is incorrect.

So, the source for add_jar is here: CMake/UseJava.cmake at master · Kitware/CMake · GitHub. I assume .kt files need to be compiled with something other than the normal java compiler? Or is it that the same javac can be used for .kt and .java files. I guess add_jar needs to learn how to handle .kt files and treat them like .java files. You could try to edit this line: CMake/UseJava.cmake at master · Kitware/CMake · GitHub. to include .kt and java. That will only work if the javac can compile both .kt and .java.

Well… kotlin uses kotlin compiler to compile .kt files.

Then you would need to modify UseJava to recognize .kt files and use the kotlin compiler instead of javac to compile them. And put the result into the jar.

Yup. Thats exactly what we are doing.

Feel free to contribute this to CMake.

1 Like