
In this article, I will only focus on the use of this method on Android and with Kotlin language however, in the future I intend to teach you how to make this connection with Java code as well as with Swift code for iOS. Today I’m going to show you how you can connect your project made in Flutter with Android native code that uses Kotlin as its main language.įor those who still don’t know, it is possible to use native codes coming from the Android operating system (using Java or Kotlin languages) or iOS (using Swift or Objective-C languages) and connect with your Flutter project.įor this, we will use a functionality provided by the Flutter documentation called Method Channel, which is nothing more than an API for specific platforms that provide us the means to connect with the native code using Flutter’s Dart language. Note that the code you provide would be generated as a nested class (inside the script).Illustration made by: Lucas Rosa -> everyone!



Here's a Java project which demonstrates this (code would be cleaner in Kotlin): to generate a jar file that can then be loaded). KotlinScript can be used to compile Kotlin source code (e.g. I'm not aware of a clean solution for this, Kotlin was not designed to be run like like PHP / JavaScript / Python which just interprets text dynamically, it has to compile to bytecode first before it can do anything on the JVM so in each scenario, you will need to compile that code first in one way or another, whether to bytecode or to javascript and in both cases load it into you application using the Java Classloader or Rhino. Runtime.getRuntime().exec(""" "kotlin code here" > blah.kts | sh""") So once again, you'll have an eval(kotlin-code-string-here) method which will dump the content to a blah.kt file, then you would use a Kotlin2JS compiler to compile it to JavaScript and directly execute the JavaScript inside Kotlin using Rhino which is not great either.Īnother option is to make use of Kotlin Scripting or an external Kotlin compiler (in both cases, the Kotlin compiler will have to start up) and doing something like this will also allow you to execute dynamically, albeit, only on Unix systems. You might want to look at Kotlin Scripting, see Īlternatively, you'll need to write your own eval(kotlin-code-string-here) method which will dump the text inside blah.kt file for example, compile it using an external Kotlin compiler into blah.class then dynamically load those classes into the runtime using the Java Classloader doing something like this: ("圜lass")Īnother no so great option is to make use of Rhino and run JavaScript inside your Kotlin code.
