问题描述
尝试使用JsInterop通过Javascript与LibGDX项目进行通信.我正在此处的示例中.它不起作用:未定义的ReferenceError'com'未定义.我的gradle
没有任何错误.
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.
我已经:
- 检查是否启用了generateJsInteropExports:
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>
我在想,也许入口点HtmlLauncher
也应该是@JsType
,但这也不起作用.
I was thinking, maybe the entry point HtmlLauncher
should be also a @JsType
, but this did not work either.
-
还检查了
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
我正在这样编译:
gradlew.bat html:dist --daemon -generateJsInteropExports=true
我的类(在html模块中,也已在核心模块中尝试过,仍然无法正常工作)看起来像这样:
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.
一些可能有用的信息:
我的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
}
}
//...
我的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"
}
}
//...
更新1
我检查了科林·阿尔沃思(Colin Alworth)的回答和他发布的链接.它仍然不起作用.我改变了:
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.*']
}
}
就像它在此处所述.
我这样称呼:gradlew.bat html:dist --daemon
,由于它似乎是错误的,我从GdxDefinition文件中删除了属性generateJsInteropExports
.
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
那是为什么?
推荐答案
非常感谢@Colin Alworth,我发现了如何使它工作.
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
}
}
还有
- 从Gdx定义文件中删除
<set-configuration-property name='generateJsInteropExports' value='true'/>
- 在导出的类的全局名称空间中不要使用相同的名称(我知道这是愚蠢的错误)
- 像
gradlew.bat html:dist --daemon
一样进行编译调用
- 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
完美的结果:
这篇关于JsInterop“未定义com".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!