问题
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:
- 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.
Also checked that generateJsInteropExports is enabled in
GdxDefinitionSuperdev.gwt.xml
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?
回答1:
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:
回答2:
<set-configuration-property name='generateJsInteropExports' value='true'/>
This definitely will not work to generate those exports
gradlew.bat html:dist --daemon -generateJsInteropExports=true
I don't know gradle terrible well, but I'm all but certain this is wrong too (at least it needs a -P
prefix, but I don't see that property being used in the gradle file you shared).
Instead, you need to need to pass it to your gradle plugin, both for Super Dev Mode and for the production compile task.
From a quick glance at the docs for org.wisepersist:gwt-gradle-plugin
, it looks like the task will take a GwtJsInteropExportsOptions arg (working from http://gwt-gradle-plugin.documentnode.io/javadoc/org/wisepersist/gradle/plugins/gwt/GwtJsInteropExportsOptions.html and http://gwt-gradle-plugin.documentnode.io/javadoc/org/wisepersist/gradle/plugins/gwt/AbstractGwtActionTask.html#setJsInteropExports-org.wisepersist.gradle.plugins.gwt.GwtJsInteropExportsOptions-), which in my limited gradle experience will end up something like
jsInteropExports {
generate = true
}
It looks like this can go in the gwt {}
block, alongside compiler {}
.
Here's an issue on that plugin's tracker which discusses how to do this https://github.com/jiakuan/gwt-gradle-plugin/issues/19.
来源:https://stackoverflow.com/questions/54836324/jsinterop-com-is-not-defined