# How to add Kotlin files to CMake Build system

**URL:** https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901
**Category:** Usage
**Tags:** gen:ninja
**Created:** [April 13, 2023, 7:02am UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901 "2023-04-13T07:02:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![siddmohsin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@siddmohsin](https://discourse.cmake.org/u/siddmohsin)
#### Post date: [April 13, 2023, 7:02am UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/1 "2023-04-13T07:02:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [April 13, 2023, 7:32pm UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/2 "2023-04-13T19:32:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![siddmohsin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@siddmohsin](https://discourse.cmake.org/u/siddmohsin)
#### Post date: [April 17, 2023, 9:04am UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/3 "2023-04-17T09:04:21Z")

</div>

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](https://discourse.cmake.org/uploads/short-url/j1o65DaLvzvifUqMz4NrR5glhjb.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.

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [April 18, 2023, 7:23pm UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/4 "2023-04-18T19:23:39Z")

</div>

So, the source for add\_jar is here: [CMake/UseJava.cmake at master · Kitware/CMake · GitHub](https://github.com/Kitware/CMake/blob/master/Modules/UseJava.cmake). 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](https://github.com/Kitware/CMake/blob/master/Modules/UseJava.cmake#L796). to include .kt and java. That will only work if the javac can compile both .kt and .java.

---

<div class="post-metadata">

### Author: ![siddmohsin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@siddmohsin](https://discourse.cmake.org/u/siddmohsin)
#### Post date: [July 13, 2023, 8:42am UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/5 "2023-07-13T08:42:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [July 13, 2023, 2:42pm UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/6 "2023-07-13T14:42:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![siddmohsin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@siddmohsin](https://discourse.cmake.org/u/siddmohsin)
#### Post date: [July 14, 2023, 5:49am UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/7 "2023-07-14T05:49:18Z")

</div>

Yup. Thats exactly what we are doing.

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [July 18, 2023, 6:30pm UTC](https://discourse.cmake.org/t/how-to-add-kotlin-files-to-cmake-build-system/7901/8 "2023-07-18T18:30:18Z")

</div>

Feel free to contribute this to CMake.
