Skip to content

Building an Android library out of a JVM project using GraalJS #514

Open
@brocollie08

Description

@brocollie08

Follow up from https://graalvm.slack.com/archives/CN9L59P61/p1634770859026100

A bit more background, I'm not using native image.
I've taken a look at Gluon maven plugin as suggested here, I'm not sure this is what I need.
I'm not trying to create an APK to run on Android, but a JAR for an existing Android app to consume.

I've tried to get the minimal reproducible steps, I was able to get the same stack trace by just trying to build a Context in a vanilla Android app.

MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  Context
    .newBuilder(JS)
    .allowAllAccess(true)
    .allowExperimentalOptions(true)
    .option("js.experimental-foreign-object-prototype", "true")
    .build()
}

Producing

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.demo, PID: 9622
    java.lang.NoSuchMethodError: No virtual method ensureClassInitialized(Ljava/lang/Class;)V in class Lsun/misc/Unsafe; or its super classes (declaration of 'sun.misc.Unsafe' appears in /apex/com.android.runtime/javalib/core-oj.jar)
        at com.oracle.truffle.api.library.LibraryFactory.ensureLibraryInitialized(LibraryFactory.java:384)
        at com.oracle.truffle.api.library.LibraryFactory.getUncached(LibraryFactory.java:364)
        at com.oracle.truffle.api.library.LibraryFactory.<init>(LibraryFactory.java:210)
        at com.oracle.truffle.api.interop.InteropLibraryGen.<init>(InteropLibraryGen.java:170)
        at com.oracle.truffle.api.interop.InteropLibraryGen.<clinit>(InteropLibraryGen.java:161)
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at com.oracle.truffle.api.library.LibraryFactory.loadGeneratedClass(LibraryFactory.java:771)
        at com.oracle.truffle.api.library.LibraryFactory.resolveImpl(LibraryFactory.java:734)
        at com.oracle.truffle.api.library.LibraryFactory.resolve(LibraryFactory.java:727)
        at com.oracle.truffle.api.interop.InteropLibrary.<clinit>(InteropLibrary.java:2875)
        at com.oracle.truffle.api.interop.InteropLibrary.getFactory(InteropLibrary.java:2839)
        at com.oracle.truffle.polyglot.PolyglotValueDispatch.<clinit>(PolyglotValueDispatch.java:156)
        at com.oracle.truffle.polyglot.PolyglotValueDispatch.createHostNull(PolyglotValueDispatch.java:1604)
        at com.oracle.truffle.polyglot.PolyglotImpl.initialize(PolyglotImpl.java:143)
        at org.graalvm.polyglot.impl.AbstractPolyglotImpl.setConstructors(AbstractPolyglotImpl.java:233)
        at org.graalvm.polyglot.Engine$1.loadAndValidateProviders(Engine.java:821)
        at org.graalvm.polyglot.Engine$1.run(Engine.java:794)
        at org.graalvm.polyglot.Engine$1.run(Engine.java:768)
        at java.security.AccessController.doPrivileged(AccessController.java:43)
        at org.graalvm.polyglot.Engine.initEngineImpl(Engine.java:768)
        at org.graalvm.polyglot.Engine.access$000(Engine.java:109)
        at org.graalvm.polyglot.Engine$ImplHolder.<clinit>(Engine.java:133)
        at org.graalvm.polyglot.Engine$ImplHolder.access$100(Engine.java:132)
        at org.graalvm.polyglot.Engine.getImpl(Engine.java:320)
        at org.graalvm.polyglot.Engine$Builder.build(Engine.java:559)
        at org.graalvm.polyglot.Context$Builder.build(Context.java:1725)

Which suggests GraalJS can't be used on the Android VM

These are my dependencies in the Android app/JVM project if relevant

implementation("org.graalvm.js", "js", "21.2.0")
implementation("org.graalvm.js", "js-scriptengine", "21.2.0")
implementation("org.graalvm.sdk", "graal-sdk", "21.2.0")

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions