Search code examples
gradlegwtlibgdxgwt-jsinterop

JsInterop "com is not defined"


Trying to communicate with LibGDX project per Javascript with JsInterop. I am following the "Exporting a Java type to JavaScript" example here. It does not work: Uncaught ReferenceError 'com' is not defined. I am not getting any errors with gradle though.

I have already:

  1. checked that generateJsInteropExports is enabled:

My GdxDefinition.gwt.xml:

<module rename-to="html">
  <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
  <inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt' />
  <inherits name='myapp' />
  <entry-point class='com.mypackage.myapp.client.HtmlLauncher' />
  <set-configuration-property name="gdx.assetpath" value="../android/assets" />
  <set-configuration-property name='xsiframe.failIfScriptTag' value='FALSE'/>
  <set-configuration-property name='generateJsInteropExports' value='true'/>
  <set-property name="user.agent" value="safari"/>
</module>

I was thinking, maybe the entry point HtmlLauncher should be also a @JsType, but this did not work either.

  1. Also checked that generateJsInteropExports is enabled in GdxDefinitionSuperdev.gwt.xml

  2. Accessing the class in the browser console in different ways like:

.

new com.mypackage.myapp.client.Test();

new Test(); // when setting namespace to global

$wnd.Test(); // JSNI syntax

I am compiling like that:

gradlew.bat html:dist --daemon -generateJsInteropExports=true

My class (Right in the html module, also tried in core module, still does not work) looks like that:

package com.mypackage.myapp.client;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

@JsType(namespace = JsPackage.GLOBAL)
public class Test {

    public String name;

    public Test(String name) {
        this.name = name;
    }


    public void sayHello() {
        return "Hello" + this.name;
    }
}

I am running out of ideas. Can somenody help me figuring out what to do so that it works.

Some information that might be useful:

Code from my html.gradle:

gwt {
    gwtVersion='2.8.0' // Should match the gwt version used for building the gwt backend
   //...
    modules 'com.mypackage.myapp.GdxDefinition'
    devModules 'com.mypackage.myapp.GdxDefinitionSuperdev'
    project.webAppDirName = 'webapp'

    compiler {
        strict = true;
        disableCastChecking = true;
    }
}

import org.wisepersist.gradle.plugins.gwt.GwtSuperDev
//...
task superDev (type: GwtSuperDev) {
    dependsOn startHttpServer
    doFirst {
        gwt.modules = gwt.devModules
    }
}
//...

Code from my project gradle

buildscript {
//...
dependencies {
    classpath 'org.wisepersist:gwt-gradle-plugin:1.0.6'
   //...
    }
}

project(":html") {
    apply plugin: "gwt"
    apply plugin: "war"

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
        compile "com.google.jsinterop:jsinterop-annotations:1.0.1"
    }
}

project(":core") {
    apply plugin: "java"
    dependencies {
       //...
        compile "com.google.jsinterop:jsinterop-annotations:1.0.1"

    }
}

//...

UPDATE 1

I checked Colin Alworth's answer and the links he posted. It still does not work. I changed:

html.gradle


gwt {
gwtVersion='2.8.0' // Should match the gwt version used for building the gwt backend
maxHeapSize="1G" // Default 256m is not enough for gwt compiler. GWT is HUNGRY
minHeapSize="1G"

src = files(file("src/")) // Needs to be in front of "modules" below.
modules 'com.mycompany.myapp.GdxDefinition'
devModules 'com.mycompany.myapp.GdxDefinitionSuperdev'
project.webAppDirName = 'webapp'

compiler {
    strict = true;
    disableCastChecking = true;
}

jsInteropExports {
    shouldGenerate = true
    includePatterns = ['com.mycompany.myapp.client.*']
}
}

Like it says here.

I call like: gradlew.bat html:dist --daemon and I removed the property generateJsInteropExports from GdxDefinition files since it seems wrong.

Now I get following compilation error:

 Task :html:compileGwt FAILED
Unknown argument: -includeJsInteropExports

Why is that?


Solution

  • Big thanks to @Colin Alworth, I found out how to get it work.

    html.gradle
    
    gwt {
      gwtVersion='2.8.0' // Should match the gwt version used for building the gwt backend
      maxHeapSize="1G" // Default 256m is not enough for gwt compiler. GWT is HUNGRY
      minHeapSize="1G"
    
      src = files(file("src/")) // Needs to be in front of "modules" below.
      modules 'com.mycompany.myapp.GdxDefinition'
      devModules 'com.mycompany.myapp.GdxDefinitionSuperdev'
      project.webAppDirName = 'webapp'
    
      compiler {
        strict = true;
        disableCastChecking = true;
      }
    
      // important part:
      jsInteropExports {
        shouldGenerate = true
        // breaks if I use includePatterns
      }
    }
    

    And also

    • remove <set-configuration-property name='generateJsInteropExports' value='true'/> from Gdx definition files
    • Not use same name in global namespace for exported classes (stupid mistake, I know)
    • Compile call like gradlew.bat html:dist --daemon

    And the perfect result:

    enter image description here