JsInterop& quot;未定义& quot; [英] JsInterop "com is not defined"

查看:82
本文介绍了JsInterop& quot;未定义& quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用JsInterop通过Javascript与LibGDX项目进行通信.我正在

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?

解决方案

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:

这篇关于JsInterop&amp; quot;未定义&amp; quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆