0

Is there any way to import .kt file in python 3.7 using pyjnius module or any other modules/ways except Jython because Jython is only supported till Python 2.7? If yes then what is the procedure?

AakashD
  • 11
  • 1
  • 1
  • Does this answer your question? [Pyjnius import jar file](https://stackoverflow.com/questions/21506319/pyjnius-import-jar-file) – Animesh Sahu Jun 30 '20 at 17:21
  • Note: All file/classes are mapped camel cases and appended Kt in the name, for example `main.kt` will be `MainKt`, `Test` will be `TestKt` – Animesh Sahu Jun 30 '20 at 17:22
  • I did look at that link before but could not understand the procedure if you could help me with that I'll appreciate it – AakashD Jun 30 '20 at 18:42
  • Correct me if I am wrong. According to that link I have to mention the class path of jar file so in my case i have to mention the path of .kt file and use autoclass as normal, right? – AakashD Jun 30 '20 at 18:50

1 Answers1

0

The Kotlin code actually is transpiled to the Java Bytecode, so at first you have to compile it and make a fat-jar with all the dependencies to make the jar run properly.

You can make one by adding the shadow jar plugin in your build.gradle file:

plugins {
    id "com.github.johnrengelman.shadow" version "5.0.0"
}

After importing the project run the shadowJar task with gradle (either by CLI as below or from IDE gradle panel):

// Linux
chmod +x gradlew     // optional
./gradlew shadowJar

// Windows
gradlew shadowJar

Then you'll see a jar file in the build/libs folder, copy that where-ever you like and then use that to specify its path in your python file:

import os
os.environ['CLASSPATH'] = "path/to/your.jar"

from jnius import autoclass


Bla = autoclass('bla.bla.BlaClass')
Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49