https://kotlinlang.org logo
#android
Title
# android
l

Lavish Swarnkar

03/18/2024, 12:57 PM
Namaste everyone, I'm Unable to run KotlinScriptEngine on Android app I am trying to build an Android app, in which user would write a Kotlin expression involving 2 input Ints, a & b. The expression written by user will then be embedded into a Kotlin script (in String form) and executed by passing in custom inputs to get the desired output. (See screenshot attached) Simplified code representation :
Copy code
val expression = // Input expression by user
val a, b = // Input Ints by user
val script = "fun x(a: Int, b: Int) = $expression; x($a, $b) 
// Run script using KotlinScriptEngine
While running this app, I get a runtime error :
Copy code
Accessing hidden field Lsun/security/util/SecurityConstants;->GET_CLASSLOADER_PERMISSION:Ljava/lang/RuntimePermission; (unsupported, linking, allowed)
W  Accessing hidden method Lsun/misc/Unsafe;->getAndAddInt(Ljava/lang/Object;JI)I (max-target-r, reflection, denied)
W  Accessing hidden method Lsun/misc/Unsafe;->getAndAddInt(Ljava/lang/Object;JI)I (max-target-r, reflection, denied)
W  javax.script.ScriptException: ERROR Unable to initialize repl compiler:
W    ERROR java.lang.NoSuchMethodException: getAndAddInt [class java.lang.Object, long, int]: java.lang.Error: java.lang.NoSuchMethodException: getAndAddInt [class java.lang.Object, long, int]: java.lang.IllegalStateException: Unable to initialize repl compiler:
W    ERROR java.lang.NoSuchMethodException: getAndAddInt [class java.lang.Object, long, int]: java.lang.Error: java.lang.NoSuchMethodException: getAndAddInt [class java.lang.Object, long, int]
W  	at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.asJsr223EvalResult(KotlinJsr223JvmScriptEngineBase.kt:104)
• After research I found out that starting A12 several such Non-SDK references
getAndAddInt()
have been blocked (Reference) • I think the Kotlin Compiler is not working on Android. In fact, I found an OPEN Kotlin issue raised for this. But people have succeeded in doing this. Here are 2 Android apps that compile and execute Kotlin code on device : 1. Offline Kotlin IDE 2. Code Assist (Android Studio for Android) Code Info • I’m using 34 targetSdk, so non-SDK ref blocked in A12 (SDK 30) won’t work. But one of the apps that work - Code Assist (mentioned above), uses 32 targetSdk, still it works for them (reference). • Gradle Dependencies used : (Complete code)
Copy code
implementation("io.apisense:rhino-android:1.0")
implementation(kotlin("scripting-jsr223"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable")
• Simplified code that executes the script : (Complete code)
Copy code
import javax.script.ScriptEngineManager

val engineManager = ScriptEngineManager()
val engine = engineManager.getEngineByExtension("kts")
engine.eval(kotlinCode)
This is just a simplified example, if this works - I have bigger usecase in mind. Any help in making this work is highly appreciated. Thank you for your time!
🧵 1
3 Views