Help with embedding KotlinSctipt into Java Android app


#1

I am trying to embed KotlinSctipt into Java Android app.

Seems org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase can’t be found, but I can’t figure out which modules are missing.

I don’t understand much about java, android and kotlin and have problem with tracking what’s wrong.
Any suggestions?

app/build.gradle:

apply plugin: 'com.android.application'

dependencies {
    implementation      'com.sun.phobos:jsr223-api:1.0'
    implementation      'org.jetbrains.kotlin:kotlin-script-util:1.3.11'
    implementation      'org.jetbrains.kotlin:kotlin-script-runtime:1.3.11'
    implementation      'org.jetbrains.kotlin:kotlin-compiler-client-embeddable:1.3.11'
    implementation      'org.jetbrains.kotlin:kotlin-compiler:1.3.11'
    annotationProcessor 'org.jetbrains.kotlin:kotlin-compiler:1.3.11'
    implementation      'org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11'
    annotationProcessor 'org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11'

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

configurations {
    cleanedAnnotations
    compile.exclude group: 'org.jetbrains', module: 'annotations'

    cleanedAnnotations
    compile.exclude group: 'org.jetbrains.kotlin', module : 'kotlin-compiler-embeddable'

    cleanedAnnotations
    compile.exclude group: 'org.jetbrains.kotlin', module : 'kotlin-compiler'

    cleanedAnnotations
    compile.exclude group: 'org.jetbrains.kotlin', module:'kotlin-compiler-client-embeddable'
    
    // the above eliminates Error: Program type already present: org.jetbrains.annotations.Nls
    //                      Error: Program type already present: org.jetbrains.kotlin.daemon.common.BoolPropMapper$1
    // and other errors like that
}

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "org.example.aktstest2"
        minSdkVersion 25
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

org/example/aktstest2/MainActivity.java:

ScriptEngine se = new ScriptEngineManager().getEngineByExtension("kts");
Log.d(LOG_TAG, "kts scriptengine = " + String.valueOf(se));

app/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory

org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory

unzip -l app/build/outputs/apk/debug/app-debug.apk | grep "META-INF/services/"

 75  00-00-1980 00:00   META-INF/services/javax.script.ScriptEngineFactory
 90  00-00-1980 00:00   META-INF/services/kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader
235  00-00-1980 00:00   META-INF/services/kotlin.reflect.jvm.internal.impl.resolve.ExternalOverridabilityCondition

Log (logcat) records:

Rejecting re-init on previously-failed class java.lang.Class<org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory>: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineFactoryBase;
    at java.lang.Class java.lang.Class.classForName!(java.lang.String, boolean, java.lang.ClassLoader) (Class.java:-2)
    at java.lang.Class java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (Class.java:400)
    at java.lang.Object sun.misc.Service$LazyIterator.next() (Service.java:289)
    at void javax.script.ScriptEngineManager.initEngines(java.lang.ClassLoader) (ScriptEngineManager.java:109)
    at void javax.script.ScriptEngineManager.access$000(javax.script.ScriptEngineManager, java.lang.ClassLoader) (ScriptEngineManager.java:37)
    at java.lang.Object javax.script.ScriptEngineManager$1.run() (ScriptEngineManager.java:80)
    at java.lang.Object java.security.AccessController.doPrivileged(java.security.PrivilegedAction) (AccessController.java:41)
    at void javax.script.ScriptEngineManager.init(java.lang.ClassLoader) (ScriptEngineManager.java:78)
    at void javax.script.ScriptEngineManager.<init>() (ScriptEngineManager.java:51)
    at void org.example.aktstest2.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:18)
    at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6684)
    at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1119)
    at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2637)
    at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2751)
    at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
    at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1496)
    at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
    at void android.os.Looper.loop() (Looper.java:154)
    at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6186)
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:889)
    at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:779)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase" on path: DexPathList[[zip file "/data/app/org.example.aktstest2-1/base.apk"],nativeLibraryDirectories=[/data/app/org.example.aktstest2-1/lib/arm, /system/lib, /vendor/lib]]
    at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
    at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
    at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
    at java.lang.Class java.lang.Class.classForName!(java.lang.String, boolean, java.lang.ClassLoader) (Class.java:-2)
    at java.lang.Class java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (Class.java:400)
    at java.lang.Object sun.misc.Service$LazyIterator.next() (Service.java:289)
    at void javax.script.ScriptEngineManager.initEngines(java.lang.ClassLoader) (ScriptEngineManager.java:109)
    at void javax.script.ScriptEngineManager.access$000(javax.script.ScriptEngineManager, java.lang.ClassLoader) (ScriptEngineManager.java:37)
    at java.lang.Object javax.script.ScriptEngineManager$1.run() (ScriptEngineManager.java:80)
    at java.lang.Object java.security.AccessController.doPrivileged(java.security.PrivilegedAction) (AccessController.java:41)
    at void javax.script.ScriptEngineManager.init(java.lang.ClassLoader) (ScriptEngineManager.java:78)
    at void javax.script.ScriptEngineManager.<init>() (ScriptEngineManager.java:51)
    at void org.example.aktstest2.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:18)
    at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6684)
    at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1119)
    at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2637)
    at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2751)
    at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
    at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1496)
    at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
    at void android.os.Looper.loop() (Looper.java:154)
    at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6186)
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:889)
    at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:779)
m.err: ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory not found
est_main_act: kts scriptengine = null